Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Accept :id_param as array #953

Open
wants to merge 1 commit into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 21, 2013
  1. @akelmanson

    Accept :id_param as array

    akelmanson authored
This page is out of date. Refresh to see the latest.
View
12 lib/cancan/controller_resource.rb
@@ -128,14 +128,22 @@ def authorization_action
def id_param
if @options[:id_param]
- @params[@options[:id_param]]
+ @params[id_param_name]
else
@params[parent? ? :"#{name}_id" : :id]
end.to_s
end
+ def id_param_name
+ if @options[:id_param].kind_of?Array
+ @options[:id_param].find { |id_param| @params.include?(id_param) }
+ else
+ @options[:id_param]
+ end
+ end
+
def member_action?
- new_actions.include?(@params[:action].to_sym) || @options[:singleton] || ( (@params[:id] || @params[@options[:id_param]]) && !collection_actions.include?(@params[:action].to_sym))
+ new_actions.include?(@params[:action].to_sym) || @options[:singleton] || ( (@params[:id] || @params[id_param_name]) && !collection_actions.include?(@params[:action].to_sym))
end
# Returns the class used for this resource. This can be overriden by the :class option.
View
8 spec/cancan/controller_resource_spec.rb
@@ -407,6 +407,14 @@ class Section
resource.send(:id_param).class.should == String
end
+ it "should load resource using the first valid ID param if id_param is Array" do
+ project = Project.create!
+ @params.merge!(:action => "show", :the_project => project.id)
+ resource = CanCan::ControllerResource.new(@controller, :id_param => [:the_project])
+ resource.load_resource
+ @controller.instance_variable_get(:@project).should == project
+ end
+
it "should load resource using custom find_by attribute" do
project = Project.create!(:name => "foo")
@params.merge!(:action => "show", :id => "foo")
Something went wrong with that request. Please try again.