Permalink
Browse files

support multiple resources in :through option of load_resource, this …

…makes polymorphic associations possible - closes #73
  • Loading branch information...
1 parent 961b8c2 commit 6998e8bdd1a38ea885016db9b7bc95f98ae26f7d @ryanb committed Aug 6, 2010
Showing with 11 additions and 1 deletion.
  1. +1 −1 lib/cancan/controller_resource.rb
  2. +10 −0 spec/cancan/controller_resource_spec.rb
View
2 lib/cancan/controller_resource.rb
@@ -83,7 +83,7 @@ def resource_base
# 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
View
10 spec/cancan/controller_resource_spec.rb
@@ -156,6 +156,16 @@
@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 }

0 comments on commit 6998e8b

Please sign in to comment.