Allowing :nested option to accept an array for deep nesting

This commit is contained in:
Ryan Bates 2009-12-13 12:22:05 -08:00
parent 51fa61bbae
commit a75aee751b
4 changed files with 15 additions and 14 deletions

View File

@ -1,3 +1,5 @@
* Allowing :nested option to accept an array for deep nesting
* Adding :nested option to load resource method - see issue #10 * Adding :nested option to load resource method - see issue #10
* Pass :only and :except options to before filters for load/authorize resource methods. * Pass :only and :except options to before filters for load/authorize resource methods.

View File

@ -41,6 +41,10 @@ module CanCan
# #
# load_resource :nested => :author # load_resource :nested => :author
# #
# Deep nesting can be defined in an array.
#
# load_resource :nested => [:publisher, :author]
#
# [:+collection+] # [:+collection+]
# Specify which actions are resource collection actions in addition to :+index+. This # Specify which actions are resource collection actions in addition to :+index+. This
# is usually not necessary because it will try to guess depending on if an :+id+ # is usually not necessary because it will try to guess depending on if an :+id+

View File

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

View File

@ -84,22 +84,16 @@ describe CanCan::ResourceAuthorization do
end end
it "should load nested resource and fetch other resource through the association" do it "should load nested resource and fetch other resource through the association" do
person = Object.new stub(Person).find(456).stub!.abilities.stub!.find(123) { :some_ability }
stub(Person).find(456) { person }
stub(person).abilities.stub!.find(123) { :some_ability }
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123, :person_id => 456}, {:nested => :person}) authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123, :person_id => 456}, {:nested => :person})
authorization.load_resource authorization.load_resource
@controller.instance_variable_get(:@person).should == person
@controller.instance_variable_get(:@ability).should == :some_ability @controller.instance_variable_get(:@ability).should == :some_ability
end end
it "should load nested resource and fetch build resource through the association" do it "should load nested resource and build resource through a deep association" do
person = Object.new stub(Person).find(456).stub!.behaviors.stub!.find(789).stub!.abilities.stub!.build(nil) { :some_ability }
stub(Person).find(456) { person } authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456, :behavior_id => 789}, {:nested => [:person, :behavior]})
stub(person).abilities.stub!.build({:foo => :bar}) { :some_ability }
authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "new", :person_id => 456, :ability => {:foo => :bar}}, {:nested => :person})
authorization.load_resource authorization.load_resource
@controller.instance_variable_get(:@person).should == person
@controller.instance_variable_get(:@ability).should == :some_ability @controller.instance_variable_get(:@ability).should == :some_ability
end end
end end