Skip to content
This repository
Browse code

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

… it works with shallow nested routes - closes #14
  • Loading branch information...
commit e9f01300b677d3072e7d5cf24191dc6411754380 1 parent f7480d1
Ryan Bates authored
3  CHANGELOG.rdoc
Source Rendered
... ... @@ -1,3 +1,6 @@
  1 +* Don't fetch parent of nested resource if *_id parameter is missing so it works with shallow nested routes - see issue #14
  2 +
  3 +
1 4 1.0.0 (Dec 13, 2009)
2 5
3 6 * Don't set resource instance variable if it has been set already - see issue #13
9 lib/cancan/resource_authorization.rb
@@ -36,8 +36,13 @@ def resource
36 36 def parent_resource
37 37 parent = nil
38 38 [@options[:nested]].flatten.compact.each do |name|
39   - parent = ControllerResource.new(@controller, name, parent)
40   - parent.find(@params["#{name}_id".to_sym])
  39 + id = @params["#{name}_id".to_sym]
  40 + if id
  41 + parent = ControllerResource.new(@controller, name, parent)
  42 + parent.find(id)
  43 + else
  44 + parent = nil
  45 + end
41 46 end
42 47 parent
43 48 end
9 spec/cancan/resource_authorization_spec.rb
@@ -96,4 +96,13 @@
96 96 authorization.load_resource
97 97 @controller.instance_variable_get(:@ability).should == :some_ability
98 98 end
  99 +
  100 + it "should not load nested resource and build through this if *_id param isn't specified" do
  101 + stub(Person).find(456) { :some_person }
  102 + stub(Ability).new(nil) { :some_ability }
  103 + authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456}, {:nested => [:person, :behavior]})
  104 + authorization.load_resource
  105 + @controller.instance_variable_get(:@person).should == :some_person
  106 + @controller.instance_variable_get(:@ability).should == :some_ability
  107 + end
99 108 end

0 comments on commit e9f0130

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