diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 3621703..5f4c5e9 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,6 +1,10 @@ 1.1.0 (not released) -* Adding be_able_to RSpec matcher (thanks dchelimsky) - see issue #54 +* Renaming :class option to :resource for load_and_authorize_resource which now supports a symbol for non models - see issue #45 + +* Properly handle Admin::AbilitiesController in params[:controller] - see issue #46 + +* Adding be_able_to RSpec matcher (thanks dchelimsky), requires Ruby 1.8.7 or higher - see issue #54 * Support additional arguments to can? which get passed to the block - see issue #48 diff --git a/lib/cancan/controller_additions.rb b/lib/cancan/controller_additions.rb index 4d1f05b..55e6b3f 100644 --- a/lib/cancan/controller_additions.rb +++ b/lib/cancan/controller_additions.rb @@ -12,7 +12,7 @@ module CanCan # end # def load_and_authorize_resource(options = {}) - before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_and_authorize_resource } + ResourceAuthorization.add_before_filter(self, :load_and_authorize_resource, options) end # Sets up a before filter which loads the appropriate model resource into an instance variable. @@ -59,8 +59,8 @@ module CanCan # # load_resource :nested => [:publisher, :author] # - # [:+class+] - # The class to use for the model. + # [:+resource+] + # The class to use for the model (string or constant). # # [:+collection+] # Specify which actions are resource collection actions in addition to :+index+. This @@ -77,7 +77,7 @@ module CanCan # load_resource :new => :build # def load_resource(options = {}) - before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).load_resource } + ResourceAuthorization.add_before_filter(self, :load_resource, options) end # Sets up a before filter which authorizes the current resource using the instance variable. @@ -102,11 +102,12 @@ module CanCan # [:+except+] # Does not apply before filter to given actions. # - # [:+class+] - # The class to use for the model. + # [:+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. # def authorize_resource(options = {}) - before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).authorize_resource } + ResourceAuthorization.add_before_filter(self, :authorize_resource, options) end end diff --git a/lib/cancan/controller_resource.rb b/lib/cancan/controller_resource.rb index a14475a..9a08a6a 100644 --- a/lib/cancan/controller_resource.rb +++ b/lib/cancan/controller_resource.rb @@ -1,6 +1,7 @@ module CanCan class ControllerResource # :nodoc: def initialize(controller, name, parent = nil, options = {}) + raise "The :class option has been renamed to :resource for specifying the class in CanCan." if options.has_key? :class @controller = controller @name = name @parent = parent @@ -8,7 +9,13 @@ module CanCan end def model_class - @options[:class] || @name.to_s.camelize.constantize + if @options[:resource].nil? + @name.to_s.camelize.constantize + elsif @options[:resource].kind_of? String + @options[:resource].constantize + else + @options[:resource] + end end def find(id) diff --git a/lib/cancan/resource_authorization.rb b/lib/cancan/resource_authorization.rb index a2a8a4e..fe8d22d 100644 --- a/lib/cancan/resource_authorization.rb +++ b/lib/cancan/resource_authorization.rb @@ -2,6 +2,12 @@ module CanCan class ResourceAuthorization # :nodoc: attr_reader :params + def self.add_before_filter(controller_class, method, options = {}) + controller_class.before_filter(options.slice(:only, :except)) do |controller| + new(controller, controller.params, options.except(:only, :except)).send(method) + end + end + def initialize(controller, params, options = {}) @controller = controller @params = params diff --git a/spec/cancan/controller_resource_spec.rb b/spec/cancan/controller_resource_spec.rb index aba66f2..721cf46 100644 --- a/spec/cancan/controller_resource_spec.rb +++ b/spec/cancan/controller_resource_spec.rb @@ -43,7 +43,17 @@ describe CanCan::ControllerResource do it "should use the model class option if provided" do stub(Person).find(123) { :some_resource } - CanCan::ControllerResource.new(@controller, :ability, nil, :class => Person).find(123) + CanCan::ControllerResource.new(@controller, :ability, nil, :resource => Person).find(123) @controller.instance_variable_get(:@ability).should == :some_resource end + + it "should convert string to constant for resource" do + CanCan::ControllerResource.new(@controller, :ability, nil, :resource => "Person").model_class.should == Person + end + + it "should raise an exception when specifying :class option since it is no longer used" do + lambda { + CanCan::ControllerResource.new(@controller, :ability, nil, :class => Person) + }.should raise_error + end end diff --git a/spec/cancan/resource_authorization_spec.rb b/spec/cancan/resource_authorization_spec.rb index 976cee4..bb21fd6 100644 --- a/spec/cancan/resource_authorization_spec.rb +++ b/spec/cancan/resource_authorization_spec.rb @@ -115,7 +115,7 @@ describe CanCan::ResourceAuthorization do it "should load the model using a custom class" do stub(Person).find(123) { :some_resource } - authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123}, {:class => Person}) + authorization = CanCan::ResourceAuthorization.new(@controller, {:controller => "abilities", :action => "show", :id => 123}, {:resource => Person}) authorization.load_resource @controller.instance_variable_get(:@ability).should == :some_resource end