diff --git a/lib/cancan/controller_additions.rb b/lib/cancan/controller_additions.rb index 96eb021..eefedd6 100644 --- a/lib/cancan/controller_additions.rb +++ b/lib/cancan/controller_additions.rb @@ -71,6 +71,10 @@ module CanCan # [:+through+] # Load this resource through another one. This should match the name of the parent instance variable or method. # + # [:+through_association+] + # The name of the association to fetch the child records through the parent resource. This is normally not needed + # because it defaults to the pluralized resource name. + # # [:+shallow+] # Pass +true+ to allow this resource to be loaded directly when parent is +nil+. Defaults to +false+. # diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 74bf71f..b9a5d9c 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -136,7 +136,7 @@ module CanCan def resource_base if @options[:through] if parent_resource - @options[:singleton] ? parent_resource : parent_resource.send(name.to_s.pluralize) + @options[:singleton] ? parent_resource : parent_resource.send(@options[:through_association] || name.to_s.pluralize) elsif @options[:shallow] resource_class else diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 84998ea..2c8fe72 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -174,6 +174,16 @@ describe CanCan::ControllerResource do @controller.instance_variable_get(:@project).should == :some_project end + it "should load resource through the custom association name" do + @params.merge!(:action => "show", :id => 123) + category = Object.new + @controller.instance_variable_set(:@category, category) + stub(category).custom_projects.stub!.find(123) { :some_project } + resource = CanCan::ControllerResource.new(@controller, :through => :category, :through_association => :custom_projects) + resource.load_resource + @controller.instance_variable_get(:@project).should == :some_project + end + it "should load resource through the association of another parent resource using method" do @params.merge!(:action => "show", :id => 123) category = Object.new