Adding :class option to load_resource so one can customize which class to use for the model - closes #17

This commit is contained in:
Ryan Bates
2009-12-14 08:31:49 -08:00
parent e9f01300b6
commit 021f33c9a0
6 changed files with 26 additions and 4 deletions

View File

@@ -59,6 +59,9 @@ module CanCan
#
# load_resource :nested => [:publisher, :author]
#
# [:+class+]
# The class to use for the model.
#
# [:+collection+]
# Specify which actions are resource collection actions in addition to :+index+. This
# is usually not necessary because it will try to guess depending on if an :+id+
@@ -72,7 +75,7 @@ module CanCan
# fetch one.
#
# 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 }
end
@@ -99,6 +102,9 @@ module CanCan
# [:+except+]
# Does not apply before filter to given actions.
#
# [:+class+]
# The class to use for the model.
#
def authorize_resource(options = {})
before_filter(options.slice(:only, :except)) { |c| ResourceAuthorization.new(c, c.params, options.except(:only, :except)).authorize_resource }
end

View File

@@ -1,13 +1,14 @@
module CanCan
class ControllerResource # :nodoc:
def initialize(controller, name, parent = nil)
def initialize(controller, name, parent = nil, options = {})
@controller = controller
@name = name
@parent = parent
@options = options
end
def model_class
@name.to_s.camelize.constantize
@options[:class] || @name.to_s.camelize.constantize
end
def find(id)

View File

@@ -30,7 +30,7 @@ module CanCan
private
def resource
@resource ||= ControllerResource.new(@controller, model_name, parent_resource)
@resource ||= ControllerResource.new(@controller, model_name, parent_resource, @options)
end
def parent_resource