Skip to content
This repository has been archived by the owner on Dec 12, 2021. It is now read-only.

Accept :id_param as array #953

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions spec/cancan/controller_resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down