renaming :class option to :resource for load_and_authorize_resource which now supports a symbol for non models - closes #45

This commit is contained in:
Ryan Bates 2010-04-15 14:14:22 -07:00
parent f2a1695636
commit 23a5888fe0
6 changed files with 39 additions and 11 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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