Class: Trust::Controller::Resource::ResourceInfo
- Defined in:
- lib/trust/controller/resource.rb
Overview
Resource::ResorceInfo
Resolves the resource in subject (see #Resource::Info)
Instance Attribute Summary
Attributes inherited from Info
#klass, #name, #params, #path, #real_class
Instance Method Summary collapse
-
#association_name(associated_resource) ⇒ Object
:nodoc.
-
#collection(associated_resource, instance = nil) ⇒ Object
Returns a collection that can be used for index, new and creation actions.
-
#initialize(model, params) ⇒ ResourceInfo
constructor
:nodoc:.
-
#plural_name ⇒ Object
Returns the plural name of the resource.
-
#relation(associated_resource) ⇒ Object
Returns an accessor for association.
Constructor Details
#initialize(model, params) ⇒ ResourceInfo
:nodoc:
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/trust/controller/resource.rb', line 320 def initialize(model, params) #:nodoc: @path, params = model, params @klass = model.to_s.classify.constantize @name = model.to_s.singularize.underscore.gsub('/','_').to_sym ptr = @klass.descendants.detect do |c| params.key? var_name(c) end || @klass @real_class = ptr @data = params[var_name(ptr)] end |
Instance Method Details
#association_name(associated_resource) ⇒ Object
:nodoc
375 376 377 378 |
# File 'lib/trust/controller/resource.rb', line 375 def association_name(associated_resource) # :nodoc name = associated_resource.as || plural_name associated_resource.object.class.reflect_on_association(name) ? name : klass.to_s.demodulize.underscore.pluralize end |
#collection(associated_resource, instance = nil) ⇒ Object
Returns a collection that can be used for index, new and creation actions.
If specifying an instance, returns the full path for that instance. Can be used when not using shallow routes
Example
Assumption
resource is instance of Lottery::Package #1 (@lottery_package)
association is Lottery::Prizes
if association is named lottery_prizes, then [@lottery_package, :lottery_prizes] is returned
if association is named prizes, then [@lottery_package, :prizes] is returned
if you specify an instance, then [@lottery_package, @prize] is returned
367 368 369 370 371 372 373 |
# File 'lib/trust/controller/resource.rb', line 367 def collection(associated_resource, instance = nil) if associated_resource && associated_resource.object [associated_resource.object, instance || association_name(associated_resource)] else klass end end |
#plural_name ⇒ Object
Returns the plural name of the resource
332 333 334 |
# File 'lib/trust/controller/resource.rb', line 332 def plural_name @plural_name ||= path.underscore.tr('/','_').to_sym end |
#relation(associated_resource) ⇒ Object
Returns an accessor for association. Tries with full name association first, and if that does not match, tries the demodularized association.
Explanation
Assuming
resource is instance of Lottery::Package #1 (@lottery_package)
association is Lottery::Prizes
if association is named lottery_prizes, then that association is returned
if association is named prizes, then that association is returned
346 347 348 349 350 351 352 |
# File 'lib/trust/controller/resource.rb', line 346 def relation(associated_resource) if associated_resource && associated_resource.object associated_resource.object.send(association_name(associated_resource)) else klass end end |