Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
:id_param option to load_resource allows specification of the param n…
…ame to find members
  • Loading branch information
geoff-parsons committed Jul 20, 2011
1 parent 600a3e1 commit 7937a28
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/cancan/controller_resource.rb
Expand Up @@ -121,11 +121,15 @@ def authorization_action
end

def id_param
@params[parent? ? :"#{name}_id" : :id]
if @options[:id_param]
@params[@options[:id_param]]
else
@params[parent? ? :"#{name}_id" : :id]
end
end

def member_action?
new_actions.include?(@params[:action].to_sym) || @options[:singleton] || (@params[:id] && !collection_actions.include?(@params[:action].to_sym))
new_actions.include?(@params[:action].to_sym) || @options[:singleton] || ( (@params[:id] || @params[@options[:id_param]]) && !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
10 changes: 9 additions & 1 deletion spec/cancan/controller_resource_spec.rb
Expand Up @@ -327,7 +327,15 @@
lambda { resource.load_and_authorize_resource }.should raise_error(CanCan::AccessDenied)
@controller.instance_variable_get(:@custom_project).should == project
end


it "should load resource using custom ID param" 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

0 comments on commit 7937a28

Please sign in to comment.