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

Don't load parent without id_param. #955

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
6 changes: 4 additions & 2 deletions lib/cancan/controller_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ def authorization_action
end

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

value && value.to_s
end

def member_action?
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 @@ -247,6 +247,14 @@ class Section
@controller.instance_variable_get(:@project).should == project
end

it "should not load parent resource if the id param isn't given" do
project = Project.create!
@params.merge!(:controller => "categories", :action => "index")
resource = CanCan::ControllerResource.new(@controller, :project)
resource.load_resource
@controller.instance_variable_get(:@project).should be_nil
end

it "should load resource through the association of another parent resource using instance variable" do
@params.merge!(:action => "show", :id => "123")
category = Object.new
Expand Down