Skip to content
Browse files

Don't fetch parent of nested resource if *_id parameter is missing so…

… it works with shallow nested routes - closes #14
  • Loading branch information...
1 parent f7480d1 commit e9f01300b677d3072e7d5cf24191dc6411754380 @ryanb committed Dec 14, 2009
Showing with 19 additions and 2 deletions.
  1. +3 −0 CHANGELOG.rdoc
  2. +7 −2 lib/cancan/resource_authorization.rb
  3. +9 −0 spec/cancan/resource_authorization_spec.rb
View
3 CHANGELOG.rdoc
@@ -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)
* Don't set resource instance variable if it has been set already - see issue #13
View
9 lib/cancan/resource_authorization.rb
@@ -36,8 +36,13 @@ def resource
def parent_resource
parent = nil
[@options[:nested]].flatten.compact.each do |name|
- parent = ControllerResource.new(@controller, name, parent)
- parent.find(@params["#{name}_id".to_sym])
+ id = @params["#{name}_id".to_sym]
+ if id
+ parent = ControllerResource.new(@controller, name, parent)
+ parent.find(id)
+ else
+ parent = nil
+ end
end
parent
end
View
9 spec/cancan/resource_authorization_spec.rb
@@ -96,4 +96,13 @@
authorization.load_resource
@controller.instance_variable_get(:@ability).should == :some_ability
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

0 comments on commit e9f0130

Please sign in to comment.
Something went wrong with that request. Please try again.