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.
-
#params_handler ⇒ Object
Returns the value of attribute params_handler.
-
#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.
-
#collection_action? ⇒ Boolean
true if action is a collection action.
-
#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 (Rails 3).
-
#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.
-
#member_action? ⇒ Boolean
true if action is a collection action.
-
#nested ⇒ Object
Returns a nested resource if parent is set.
-
#new_action? ⇒ Boolean
true if action is a new action.
-
#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.
-
#strong_params(new_action = new_action?) ) ⇒ Object
Returns strong parameters for the instance (Rails 4) This call will take advantage of the spesified in permissions.
Constructor Details
#initialize(controller, properties, action_name, params, request) ⇒ Resource
nodoc
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/trust/controller/resource.rb', line 49 def initialize(controller, properties, action_name, params, request) # nodoc @action = action_name.to_sym @params_handler = {} @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) self.parent = parent_info.object if parent_info 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 |
#params_handler ⇒ Object
Returns the value of attribute params_handler.
47 48 49 |
# File 'lib/trust/controller/resource.rb', line 47 def params_handler @params_handler 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
249 250 251 |
# File 'lib/trust/controller/resource.rb', line 249 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.
163 164 165 |
# File 'lib/trust/controller/resource.rb', line 163 def collection(instance = nil) @info.collection(@parent_info, instance) end |
#collection_action? ⇒ Boolean
true if action is a collection action
168 169 170 |
# File 'lib/trust/controller/resource.rb', line 168 def collection_action? @collection_action ||= properties.collection_action?(action) end |
#instance ⇒ Object
Returns the instance variable in the controller
62 63 64 |
# File 'lib/trust/controller/resource.rb', line 62 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)
74 75 76 |
# File 'lib/trust/controller/resource.rb', line 74 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
214 215 216 |
# File 'lib/trust/controller/resource.rb', line 214 def instance_name info.name end |
#instance_params ⇒ Object
Returns the parameters for the instance (Rails 3)
Example
# in AccountsController
resource.instance_params # same as params[:account]
84 85 86 |
# File 'lib/trust/controller/resource.rb', line 84 def instance_params info.params end |
#instances ⇒ Object
Returns the cinstance variable for ollection
137 138 139 |
# File 'lib/trust/controller/resource.rb', line 137 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
144 145 146 |
# File 'lib/trust/controller/resource.rb', line 144 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
151 152 153 |
# File 'lib/trust/controller/resource.rb', line 151 def instantiated instances || instance end |
#klass ⇒ Object
Returns the class for the resource
156 157 158 |
# File 'lib/trust/controller/resource.rb', line 156 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.
194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/trust/controller/resource.rb', line 194 def load if new_action? # logger.debug "Trust.load: Setting new: class: #{klass} strong_params: #{strong_params.inspect}" self.instance ||= relation.new(strong_params) @controller.send(:build, action) if @controller.respond_to?(:build, true) 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,true) else # other outcome would be collection actions # logger.debug "Trust.load: Parent is: #{parent.inspect}, collection or unknown action." end end |
#member_action? ⇒ Boolean
true if action is a collection action
173 174 175 |
# File 'lib/trust/controller/resource.rb', line 173 def member_action? @member_action ||= properties.member_action?(action) end |
#nested ⇒ Object
Returns a nested resource if parent is set
178 179 180 |
# File 'lib/trust/controller/resource.rb', line 178 def nested parent ? [parent, instance] : [instance] end |
#new_action? ⇒ Boolean
true if action is a new action
183 184 185 |
# File 'lib/trust/controller/resource.rb', line 183 def new_action? @new_action ||= properties.new_action?(action) end |
#parent ⇒ Object
Returns the parents instance variable when you use belongs_to
for nested routes
127 128 129 |
# File 'lib/trust/controller/resource.rb', line 127 def parent parent_name && @controller.instance_variable_get(:"@#{parent_name}") end |
#parent=(instance) ⇒ Object
Sets the parent instance variable
132 133 134 |
# File 'lib/trust/controller/resource.rb', line 132 def parent=(instance) @controller.instance_variable_set(:"@#{parent_name}", instance) if parent_name 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
244 245 246 |
# File 'lib/trust/controller/resource.rb', line 244 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
234 235 236 |
# File 'lib/trust/controller/resource.rb', line 234 def plural_instance_name info.plural_name end |
#strong_params(new_action = new_action?) ) ⇒ Object
Returns strong parameters for the instance (Rails 4) This call will take advantage of the spesified in permissions. If no such permissions is defined, it will fall back to instance_params
Example
# assume the following permissions defined
class Account < Default
require :account
permit :number, :amount
end
# in AccountsController
resource.strong_params # same as params.require(:account).permit(:number, :amount)
# as a new action
resource.strong_params(true) # same as params.fetch(:account, {}).permit(:number, :amount)
106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/trust/controller/resource.rb', line 106 def strong_params(new_action = new_action?) if params_handler.size > 0 if params_handler[:require] new_action ? params.fetch(params_handler[:require], {}).permit(params_handler[:permit]) : params.require(params_handler[:require]).permit(params_handler[:permit]) else params.permit(params_handler[:permit]) end else instance_params end end |