From 6998e8bdd1a38ea885016db9b7bc95f98ae26f7d Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 6 Aug 2010 10:35:42 -0700 Subject: [PATCH] support multiple resources in :through option of load_resource, this makes polymorphic associations possible - closes #73 --- lib/cancan/controller_resource.rb | 2 +- spec/cancan/controller_resource_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index 1b8a337..b7fd98c 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -83,7 +83,7 @@ module CanCan # The object to load this resource through. def through_resource - @options[:through] && @controller.instance_variable_get("@#{@options[:through]}") + @options[:through] && [@options[:through]].flatten.map { |i| @controller.instance_variable_get("@#{i}") }.compact.first end def name diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index 6ea5895..9cd8c0a 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -156,6 +156,16 @@ describe CanCan::ControllerResource do @controller.instance_variable_get(:@ability).should == :some_ability end + it "should load through first matching if multiple are given" do + @params.merge!(:action => "show", :id => 123) + person = Object.new + @controller.instance_variable_set(:@person, person) + stub(person).abilities.stub!.find(123) { :some_ability } + resource = CanCan::ControllerResource.new(@controller, :through => [:thing, :person]) + resource.load_resource + @controller.instance_variable_get(:@ability).should == :some_ability + end + it "should only authorize :read action on parent resource" do @params.merge!(:action => "new", :person_id => 123) stub(Person).find(123) { :some_person }