Class: Trust::Controller::Resource
- Inherits:
-
Object
- Object
- Trust::Controller::Resource
- Defined in:
- lib/trust/controller/resource.rb
Overview
Trust::Controller::Resource
Collects information about the current resource and relations. Handles the loading of the resource and its possible parent, i.e. setting the relevant instance variables It assumes the name of the resource is built on the controllers name, but this can be overridden in your controller by setting the model
Examples:
# controller name AccountsController
resource.instance # => @account
# controller name Customer::AccountsController
resource.instance # => @customer_account
Defined Under Namespace
Classes: Info, ParentInfo, ResourceInfo
Instance Attribute Summary collapse
-
#action ⇒ Object
readonly
Returns the value of attribute action.
-
#info ⇒ Object
readonly
Returns the value of attribute info.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#parent_info ⇒ Object
readonly
Returns the value of attribute parent_info.
-
#properties ⇒ Object
readonly
Returns the value of attribute properties.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
-
#association_name ⇒ Object
Returns the association name with the parent.
-
#collection(instance = nil) ⇒ Object
Returns a collection that can be used for index, new and creation actions.
-
#initialize(controller, properties, action_name, params, request) ⇒ Resource
constructor
nodoc.
-
#instance ⇒ Object
Returns the instance variable in the controller.
-
#instance=(instance) ⇒ Object
Sets the instance variable.
-
#instance_name ⇒ Object
Returns the name of the instance for the resource.
-
#instance_params ⇒ Object
Returns the parameters for the instance.
-
#instances ⇒ Object
Returns the cinstance variable for ollection.
-
#instances=(instances) ⇒ Object
Sets the instance variable for collection.
-
#instantiated ⇒ Object
Returns either the instances or the instance.
-
#klass ⇒ Object
Returns the class for the resource.
-
#load ⇒ Object
Loads the resource.
-
#parent ⇒ Object
Returns the parents instance variable when you use
belongs_to
for nested routes. -
#parent=(instance) ⇒ Object
Sets the parent instance variable.
-
#parent_name ⇒ Object
Returns the name of the parent resource.
-
#plural_instance_name ⇒ Object
Returns the plural name of the instance for the resource.
Constructor Details
#initialize(controller, properties, action_name, params, request) ⇒ Resource
nodoc
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/trust/controller/resource.rb', line 48 def initialize(controller, properties, action_name, params, request) # nodoc @action = action_name.to_sym @controller, @properties, @params = controller, properties, params @info = extract_resource_info(properties.model, params) if properties.has_associations? @parent_info = extract_parent_info(properties.associations, params, request) end @relation = @info.relation(@parent_info) end |
Instance Attribute Details
#action ⇒ Object (readonly)
Returns the value of attribute action.
45 46 47 |
# File 'lib/trust/controller/resource.rb', line 45 def action @action end |
#info ⇒ Object (readonly)
Returns the value of attribute info.
46 47 48 |
# File 'lib/trust/controller/resource.rb', line 46 def info @info end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
45 46 47 |
# File 'lib/trust/controller/resource.rb', line 45 def params @params end |
#parent_info ⇒ Object (readonly)
Returns the value of attribute parent_info.
46 47 48 |
# File 'lib/trust/controller/resource.rb', line 46 def parent_info @parent_info end |
#properties ⇒ Object (readonly)
Returns the value of attribute properties.
45 46 47 |
# File 'lib/trust/controller/resource.rb', line 45 def properties @properties end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
46 47 48 |
# File 'lib/trust/controller/resource.rb', line 46 def relation @relation end |
Instance Method Details
#association_name ⇒ Object
Returns the association name with the parent
181 182 183 |
# File 'lib/trust/controller/resource.rb', line 181 def association_name parent_info && info.association_name(parent_info) end |
#collection(instance = nil) ⇒ Object
Returns a collection that can be used for index, new and creation actions
See Trust::Controller::ResourceInfo.collection which controls the behavior of this method.
123 124 125 |
# File 'lib/trust/controller/resource.rb', line 123 def collection(instance = nil) @info.collection(@parent_info, instance) end |
#instance ⇒ Object
Returns the instance variable in the controller
60 61 62 |
# File 'lib/trust/controller/resource.rb', line 60 def instance @controller.instance_variable_get(:"@#{instance_name}") end |
#instance=(instance) ⇒ Object
Sets the instance variable
Normally set by load
. You can access this method from the resource object.
Example
resource.instance = Account.find_by_number(123456)
72 73 74 |
# File 'lib/trust/controller/resource.rb', line 72 def instance=(instance) @controller.instance_variable_set(:"@#{instance_name}", instance) end |
#instance_name ⇒ Object
Returns the name of the instance for the resource
Example
# in AccountsController
resource.instance_name # => :account
156 157 158 |
# File 'lib/trust/controller/resource.rb', line 156 def instance_name info.name end |
#instance_params ⇒ Object
Returns the parameters for the instance
Example
# in AccountsController
resource.instance_params # same as params[:account]
82 83 84 |
# File 'lib/trust/controller/resource.rb', line 82 def instance_params info.params end |
#instances ⇒ Object
Returns the cinstance variable for ollection
97 98 99 |
# File 'lib/trust/controller/resource.rb', line 97 def instances @controller.instance_variable_get(:"@#{plural_instance_name}") end |
#instances=(instances) ⇒ Object
Sets the instance variable for collection
You may want to set this variable in your index action, we do not yet support loading of collections
104 105 106 |
# File 'lib/trust/controller/resource.rb', line 104 def instances=(instances) @controller.instance_variable_set(:"@#{plural_instance_name}", instances) end |
#instantiated ⇒ Object
Returns either the instances or the instance.
We have found that this can be useful in some implementation patterns
111 112 113 |
# File 'lib/trust/controller/resource.rb', line 111 def instantiated instances || instance end |
#klass ⇒ Object
Returns the class for the resource
116 117 118 |
# File 'lib/trust/controller/resource.rb', line 116 def klass info.klass end |
#load ⇒ Object
Loads the resource
See Trust::Controller::Properties which controls the behavior of this method.
It will normally find the instance variable for existing object or initialize them as new. If using nested resources and belongs_to
has been declared in the controller it will use the parent relation if found.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/trust/controller/resource.rb', line 135 def load self.parent = parent_info.object if parent_info if properties.new_actions.include?(action) # logger.debug "Trust.load: Setting new: class: #{klass} info.params: #{info.params.inspect}" self.instance ||= relation.new(info.params) @controller.send(:build, action) if @controller.respond_to?(:build) elsif properties.member_actions.include?(action) # logger.debug "Trust.load: Finding parent: #{parent.inspect}, relation: #{relation.inspect}" self.instance ||= relation.find(params[:id] || params["#{relation.name.underscore}_id".to_sym]) @controller.send(:build, action) if @controller.respond_to?(:build) else # other outcome would be collection actions # logger.debug "Trust.load: Parent is: #{parent.inspect}, collection or unknown action." end end |
#parent ⇒ Object
Returns the parents instance variable when you use belongs_to
for nested routes
87 88 89 |
# File 'lib/trust/controller/resource.rb', line 87 def parent @controller.instance_variable_get(:"@#{parent_name}") end |
#parent=(instance) ⇒ Object
Sets the parent instance variable
92 93 94 |
# File 'lib/trust/controller/resource.rb', line 92 def parent=(instance) @controller.instance_variable_set(:"@#{parent_name}", instance) end |
#parent_name ⇒ Object
Returns the name of the parent resource
Example
# in AccountsController where belongs_to :customer has been declared
resource.parent_name # => :customer
176 177 178 |
# File 'lib/trust/controller/resource.rb', line 176 def parent_name parent_info && parent_info.name end |
#plural_instance_name ⇒ Object
Returns the plural name of the instance for the resource
Example
# in AccountsController
resource.plural_instance_name # => :accounts
166 167 168 |
# File 'lib/trust/controller/resource.rb', line 166 def plural_instance_name info.plural_name end |