From 0951ade113444f407ae8082c3bf58e2114509741 Mon Sep 17 00:00:00 2001 From: Paul Martensen Date: Thu, 7 Dec 2017 17:20:01 +0100 Subject: [PATCH 1/6] Fixed indentation. --- spec/lib/param_group_spec.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spec/lib/param_group_spec.rb b/spec/lib/param_group_spec.rb index 9db23c3bd..f816a7686 100644 --- a/spec/lib/param_group_spec.rb +++ b/spec/lib/param_group_spec.rb @@ -51,10 +51,9 @@ expect(Apipie["overridden_concern_resources#create"].params.has_key?(:user)).to eq(false) end -it "shouldn't replace name of a parameter defined in the controller" do + it "shouldn't replace name of a parameter defined in the controller" do expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:concern)).to eq(true) expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:user)).to eq(false) end - end From 24d35666b10ef658bde395f28a1761e118646647 Mon Sep 17 00:00:00 2001 From: Paul Martensen Date: Thu, 7 Dec 2017 17:30:06 +0100 Subject: [PATCH 2/6] Added failing test. --- .../app/controllers/api/v1/architectures_controller.rb | 1 + spec/dummy/app/controllers/api/v1/base_controller.rb | 5 +++++ spec/lib/param_group_spec.rb | 6 ++++++ 3 files changed, 12 insertions(+) diff --git a/spec/dummy/app/controllers/api/v1/architectures_controller.rb b/spec/dummy/app/controllers/api/v1/architectures_controller.rb index 69949d841..0c8b70f53 100644 --- a/spec/dummy/app/controllers/api/v1/architectures_controller.rb +++ b/spec/dummy/app/controllers/api/v1/architectures_controller.rb @@ -3,6 +3,7 @@ module V1 class ArchitecturesController < V1::BaseController resource_description { name 'Architectures' } api :GET, "/architectures/", "List all architectures." + param_group :pagination def index end diff --git a/spec/dummy/app/controllers/api/v1/base_controller.rb b/spec/dummy/app/controllers/api/v1/base_controller.rb index ada0065e8..0114a0f21 100644 --- a/spec/dummy/app/controllers/api/v1/base_controller.rb +++ b/spec/dummy/app/controllers/api/v1/base_controller.rb @@ -6,6 +6,11 @@ class BaseController < Api::BaseController app_info 'Version 1.0 description' api_base_url '/api/v1' end + + def_param_group :pagination do + param :page, Integer, 'The page of the resource.' + param :items_per_page, Integer, 'The number of items per page.' + end end end end diff --git a/spec/lib/param_group_spec.rb b/spec/lib/param_group_spec.rb index f816a7686..a014ff686 100644 --- a/spec/lib/param_group_spec.rb +++ b/spec/lib/param_group_spec.rb @@ -55,5 +55,11 @@ expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:concern)).to eq(true) expect(Apipie["overridden_concern_resources#custom"].params.has_key?(:user)).to eq(false) end + + it 'should allow controllers to inherit param groups from their parents' do + expect(Apipie['1.0#architectures#index'].params.key?(:page)).to be true + expect(Apipie['1.0#architectures#index'].params.key?(:items_per_page)) + .to be true + end end From 90b9c6129e55a5916e5dd7d40e2a708c6e98f3d1 Mon Sep 17 00:00:00 2001 From: Paul Martensen Date: Thu, 7 Dec 2017 17:30:28 +0100 Subject: [PATCH 3/6] Added implementation of param_group inheritance. --- lib/apipie/application.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/apipie/application.rb b/lib/apipie/application.rb index adf0a91ca..b6ec490b0 100644 --- a/lib/apipie/application.rb +++ b/lib/apipie/application.rb @@ -143,6 +143,8 @@ def get_param_group(controller, name) key = "#{controller.name}##{name}" if @param_groups.has_key?(key) return @param_groups[key] + elsif controller.superclass != Object + get_param_group(controller.superclass, name) else raise "param group #{key} not defined" end From 4f19a76ec0d48c201b41dd092af89448683bd254 Mon Sep 17 00:00:00 2001 From: Paul Martensen Date: Thu, 7 Dec 2017 17:33:24 +0100 Subject: [PATCH 4/6] Added pending test for overriding. --- spec/lib/param_group_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/spec/lib/param_group_spec.rb b/spec/lib/param_group_spec.rb index a014ff686..0ce20e95a 100644 --- a/spec/lib/param_group_spec.rb +++ b/spec/lib/param_group_spec.rb @@ -61,5 +61,7 @@ expect(Apipie['1.0#architectures#index'].params.key?(:items_per_page)) .to be true end + + it 'should be able to override inherited param_groups' end From 21474bb20db1c1e7b0365f6b44dcc6870b1ef312 Mon Sep 17 00:00:00 2001 From: Paul Martensen Date: Thu, 7 Dec 2017 17:44:32 +0100 Subject: [PATCH 5/6] Added test for overriding. --- .../dummy/app/controllers/api/v1/architectures_controller.rb | 5 +++++ spec/dummy/app/controllers/api/v1/base_controller.rb | 4 ++++ spec/lib/param_group_spec.rb | 4 +++- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/spec/dummy/app/controllers/api/v1/architectures_controller.rb b/spec/dummy/app/controllers/api/v1/architectures_controller.rb index 0c8b70f53..29881ddff 100644 --- a/spec/dummy/app/controllers/api/v1/architectures_controller.rb +++ b/spec/dummy/app/controllers/api/v1/architectures_controller.rb @@ -8,9 +8,14 @@ def index end api :GET, "/architectures/:id/", "Show an architecture." + param_group :identifier def show end + def_param_group :identifier do + param :identifier, Hash, 'A hex based string to identify the resource.' + end + def_param_group :timestamps do param :created_at, String param :updated_at, String diff --git a/spec/dummy/app/controllers/api/v1/base_controller.rb b/spec/dummy/app/controllers/api/v1/base_controller.rb index 0114a0f21..9208109e2 100644 --- a/spec/dummy/app/controllers/api/v1/base_controller.rb +++ b/spec/dummy/app/controllers/api/v1/base_controller.rb @@ -11,6 +11,10 @@ class BaseController < Api::BaseController param :page, Integer, 'The page of the resource.' param :items_per_page, Integer, 'The number of items per page.' end + + def_param_group :identifier do + param :identifier, Integer, 'The identifier of the resource.' + end end end end diff --git a/spec/lib/param_group_spec.rb b/spec/lib/param_group_spec.rb index 0ce20e95a..1af757035 100644 --- a/spec/lib/param_group_spec.rb +++ b/spec/lib/param_group_spec.rb @@ -62,6 +62,8 @@ .to be true end - it 'should be able to override inherited param_groups' + it 'should be able to override inherited param_groups' do + expect(Apipie['1.0#architectures#show'].params[:identifier].validator.expected_type).to eq 'hash' + end end From ba6d381546f1ec1ba34765893f3c96cf32d9346d Mon Sep 17 00:00:00 2001 From: Paul Martensen Date: Thu, 7 Dec 2017 20:40:37 +0100 Subject: [PATCH 6/6] Added Documentation to README. --- README.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.rst b/README.rst index ea838d367..e88d8b979 100644 --- a/README.rst +++ b/README.rst @@ -334,7 +334,7 @@ params for ``create`` and ``update`` actions are shared between them. These params can be extracted with ``def_param_group`` and ``param_group`` keywords. -The definition is looked up in the scope of the controller. If the +The definition is looked up in the scope of the controller and its subclasses. If the group is defined in a different controller, it might be referenced by specifying the second argument.