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:
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/trust/controller/resource.rb', line 252 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
307 308 309 310 |
# File 'lib/trust/controller/resource.rb', line 307 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
299 300 301 302 303 304 305 |
# File 'lib/trust/controller/resource.rb', line 299 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
264 265 266 |
# File 'lib/trust/controller/resource.rb', line 264 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
278 279 280 281 282 283 284 |
# File 'lib/trust/controller/resource.rb', line 278 def relation(associated_resource) if associated_resource && associated_resource.object associated_resource.object.send(association_name(associated_resource)) else klass end end |