Skip to content

Commit

Permalink
Merge pull request #280 from tstrachota/concerns
Browse files Browse the repository at this point in the history
Name substitution for referenced param_groups defined in concerns
  • Loading branch information
iNecas committed Aug 22, 2014
2 parents 6364e7c + 9491a20 commit 4939594
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
16 changes: 15 additions & 1 deletion lib/apipie/dsl_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,13 @@ def param_group(name, scope_or_options = nil, options = {})
scope = scope_or_options
end
scope ||= _default_param_group_scope
@_current_param_group = {:scope => scope, :name => name, :options => options}

@_current_param_group = {
:scope => scope,
:name => name,
:options => options,
:from_concern => scope.apipie_concern?
}
self.instance_exec(&Apipie.get_param_group(scope, name))
ensure
@_current_param_group = nil
Expand Down Expand Up @@ -322,6 +328,10 @@ def _apipie_perform_concern_subst(string)
end
end

def apipie_concern?
false
end

# create method api and redefine newly added method
def method_added(method_name) #:doc:
super
Expand Down Expand Up @@ -363,6 +373,10 @@ def _apipie_concern_data
@_apipie_concern_data ||= []
end

def apipie_concern?
true
end

# create method api and redefine newly added method
def method_added(method_name) #:doc:
super
Expand Down
9 changes: 8 additions & 1 deletion lib/apipie/param_description.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def initialize(method_description, name, validator, desc_or_options = nil, optio

# we save options to know what was passed in DSL
@options = options
if @options[:param_group]
@from_concern = @options[:param_group][:from_concern]
end

@method_description = method_description
@name = concern_subst(name)
Expand Down Expand Up @@ -66,6 +69,10 @@ def initialize(method_description, name, validator, desc_or_options = nil, optio
end
end

def from_concern?
method_description.from_concern? || @from_concern
end

def validate(value)
return true if @allow_nil && value.nil?
if (!@allow_nil && value.nil?) || !@validator.valid?(value)
Expand Down Expand Up @@ -187,7 +194,7 @@ def action_awareness
end

def concern_subst(string)
return string if string.nil? or !method_description.from_concern?
return string if string.nil? or !from_concern?

original = string
string = ":#{original}" if original.is_a? Symbol
Expand Down
31 changes: 31 additions & 0 deletions spec/dummy/app/controllers/overridden_concerns_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class OverriddenConcernsController < ApplicationController

resource_description { resource_id 'overridden_concern_resources' }

apipie_concern_subst(:concern => 'user')
include ::Concerns::SampleController

def_param_group :concern do
param :concern, String
end

api :PUT, '/:resource_id/:id'
param :custom_parameter, String, "New parameter added by the overriding method"
param_group :concern, ::Concerns::SampleController
def update
super
end

api :POST, '/:resource_id', "Create a :concern"
param_group :concern
def create
super
end

api :GET, '/:resource_id/custom'
param :concern, String
def custom
render :text => "OK #{params.inspect}"
end

end
15 changes: 15 additions & 0 deletions spec/lib/param_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,20 @@

arch_v1_params.sort_by(&:to_s).should == arch_v2_params.sort_by(&:to_s)
end

it "should replace parameter name in a group when it comes from concern" do
Apipie["overridden_concern_resources#update"].params.has_key?(:user).should == true
end

it "shouldn't replace parameter name in a group redefined in the controller" do
Apipie["overridden_concern_resources#create"].params.has_key?(:concern).should == true
Apipie["overridden_concern_resources#create"].params.has_key?(:user).should == false
end

it "shouldn't replace name of a parameter defined in the controller" do
Apipie["overridden_concern_resources#custom"].params.has_key?(:concern).should == true
Apipie["overridden_concern_resources#custom"].params.has_key?(:user).should == false
end

end

0 comments on commit 4939594

Please sign in to comment.