Class: CanCan::ControllerResource
- Inherits:
-
Object
- Object
- CanCan::ControllerResource
- Defined in:
- lib/cancan/controller_resource.rb
Overview
Used internally to load and authorize a given controller resource. This manages finding or building an instance of the resource. If a parent is given it will go through the association.
Instance Method Summary collapse
-
#build(attributes) ⇒ Object
Build a new instance of this resource.
- #find(id) ⇒ Object
-
#initialize(controller, name, parent = nil, options = {}) ⇒ ControllerResource
constructor
:nodoc:.
-
#model_class ⇒ Object
Returns the class used for this resource.
- #model_instance ⇒ Object
- #model_instance=(instance) ⇒ Object
Constructor Details
#initialize(controller, name, parent = nil, options = {}) ⇒ ControllerResource
:nodoc:
6 7 8 9 10 11 12 |
# File 'lib/cancan/controller_resource.rb', line 6 def initialize(controller, name, parent = nil, = {}) raise ImplementationRemoved, "The :class option has been renamed to :resource for specifying the class in CanCan." if .has_key? :class @controller = controller @name = name @parent = parent @options = end |
Instance Method Details
#build(attributes) ⇒ Object
Build a new instance of this resource. If it is a class we just call “new” otherwise it’s an associaiton and “build” is used.
34 35 36 |
# File 'lib/cancan/controller_resource.rb', line 34 def build(attributes) self.model_instance ||= (base.kind_of?(Class) ? base.new(attributes) : base.build(attributes)) end |
#find(id) ⇒ Object
28 29 30 |
# File 'lib/cancan/controller_resource.rb', line 28 def find(id) self.model_instance ||= base.find(id) end |
#model_class ⇒ Object
Returns the class used for this resource. This can be overriden by the :resource option. Sometimes one will use a symbol as the resource if a class does not exist for it. In that case “find” and “build” should not be called on it.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cancan/controller_resource.rb', line 17 def model_class resource_class = @options[:resource] if resource_class.nil? @name.to_s.camelize.constantize elsif resource_class.kind_of? String resource_class.constantize else resource_class # could be a symbol end end |
#model_instance ⇒ Object
38 39 40 |
# File 'lib/cancan/controller_resource.rb', line 38 def model_instance @controller.instance_variable_get("@#{@name}") end |
#model_instance=(instance) ⇒ Object
42 43 44 |
# File 'lib/cancan/controller_resource.rb', line 42 def model_instance=(instance) @controller.instance_variable_set("@#{@name}", instance) end |