Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
2 changes: 2 additions & 0 deletions lib/apipie/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/dummy/app/controllers/api/v1/architectures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@ module V1
class ArchitecturesController < V1::BaseController
resource_description { name 'Architectures' }
api :GET, "/architectures/", "List all architectures."
param_group :pagination
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
Expand Down
9 changes: 9 additions & 0 deletions spec/dummy/app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ 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

def_param_group :identifier do
param :identifier, Integer, 'The identifier of the resource.'
end
end
end
end
11 changes: 10 additions & 1 deletion spec/lib/param_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,19 @@
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

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

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