diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 702fbcfb..7edb5cf0 100644 --- a/lib/cancan/controller_resource.rb +++ b/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. diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 03f16bfb..07ee35e8 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/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")