From 2a3dd85a185b0f27d308633db499fddb182216dc Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 21 May 2010 14:20:45 -0700 Subject: [PATCH] adding :name option to load_and_authorize_resource if it does not match controller - closes #65 --- CHANGELOG.rdoc | 2 ++ lib/cancan/controller_additions.rb | 10 ++++++++++ lib/cancan/resource_authorization.rb | 2 +- spec/cancan/resource_authorization_spec.rb | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 828453c..38ffc70 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,5 @@ +* Adding :name option to load_and_authorize_resource if it does not match controller - see issue #65 + * Fixing issue when using accessible_by with nil can conditions (thanks jrallison) - see issue #66 * Pluralize table name for belongs_to associations in can conditions hash (thanks logandk) - see issue #62 diff --git a/lib/cancan/controller_additions.rb b/lib/cancan/controller_additions.rb index 4b78b25..28f3bb4 100644 --- a/lib/cancan/controller_additions.rb +++ b/lib/cancan/controller_additions.rb @@ -59,6 +59,11 @@ module CanCan # # load_resource :nested => [:publisher, :author] # + # [:+name+] + # The name of the resource if it cannot be determined from controller (string or symbol). + # + # load_resource :name => :article + # # [:+resource+] # The class to use for the model (string or constant). # @@ -102,6 +107,11 @@ module CanCan # [:+except+] # Does not apply before filter to given actions. # + # [:+name+] + # The name of the resource if it cannot be determined from controller (string or symbol). + # + # load_resource :name => :article + # # [:+resource+] # The class to use for the model (string or constant). Alternatively pass a symbol # to represent a resource which does not have a class. diff --git a/lib/cancan/resource_authorization.rb b/lib/cancan/resource_authorization.rb index ef9a9b2..0b7fcec 100644 --- a/lib/cancan/resource_authorization.rb +++ b/lib/cancan/resource_authorization.rb @@ -54,7 +54,7 @@ module CanCan end def model_name - @params[:controller].sub("Controller", "").underscore.split('/').last.singularize + @options[:name] || @params[:controller].sub("Controller", "").underscore.split('/').last.singularize end def collection_actions diff --git a/spec/cancan/resource_authorization_spec.rb b/spec/cancan/resource_authorization_spec.rb index 4d9a88e..d561fed 100644 --- a/spec/cancan/resource_authorization_spec.rb +++ b/spec/cancan/resource_authorization_spec.rb @@ -114,4 +114,11 @@ describe CanCan::ResourceAuthorization do authorization.load_resource @controller.instance_variable_get(:@ability).should == :some_resource end + + it "should use :name option to determine resource name" do + stub(Ability).find(123) { :some_resource } + authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "foo", :action => "show", :id => 123}, {:name => :ability}) + authorization.load_resource + @controller.instance_variable_get(:@ability).should == :some_resource + end end