Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - closes #14

This commit is contained in:
Ryan Bates 2009-12-14 08:18:08 -08:00
parent f7480d1f5a
commit e9f01300b6
3 changed files with 19 additions and 2 deletions

View File

@ -1,3 +1,6 @@
* Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
1.0.0 (Dec 13, 2009) 1.0.0 (Dec 13, 2009)
* Don't set resource instance variable if it has been set already - see issue #13 * Don't set resource instance variable if it has been set already - see issue #13

View File

@ -36,8 +36,13 @@ module CanCan
def parent_resource def parent_resource
parent = nil parent = nil
[@options[:nested]].flatten.compact.each do |name| [@options[:nested]].flatten.compact.each do |name|
id = @params["#{name}_id".to_sym]
if id
parent = ControllerResource.new(@controller, name, parent) parent = ControllerResource.new(@controller, name, parent)
parent.find(@params["#{name}_id".to_sym]) parent.find(id)
else
parent = nil
end
end end
parent parent
end end

View File

@ -96,4 +96,13 @@ describe CanCan::ResourceAuthorization do
authorization.load_resource authorization.load_resource
@controller.instance_variable_get(:@ability).should == :some_ability @controller.instance_variable_get(:@ability).should == :some_ability
end end
it "should not load nested resource and build through this if *_id param isn't specified" do
stub(Person).find(456) { :some_person }
stub(Ability).new(nil) { :some_ability }
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456}, {:nested => [:person, :behavior]})
authorization.load_resource
@controller.instance_variable_get(:@person).should == :some_person
@controller.instance_variable_get(:@ability).should == :some_ability
end
end end