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

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