Class: Restful::Association
- Inherits:
-
Object
- Object
- Restful::Association
- Defined in:
- lib/restful/serializer.rb
Overview
Handle for information about an ActiveRecord association.
Instance Attribute Summary collapse
-
#association ⇒ Object
Returns the value of attribute association.
-
#association_name ⇒ Object
Returns the value of attribute association_name.
-
#name ⇒ Object
Returns the value of attribute name.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#subject_klass ⇒ Object
Returns the value of attribute subject_klass.
-
#url_factory ⇒ Object
Returns the value of attribute url_factory.
Instance Method Summary collapse
- #collective_href ⇒ Object
- #href ⇒ Object
-
#initialize(subject, subject_klass, association_name, url_factory, name = nil) ⇒ Association
constructor
A new instance of Association.
- #singular? ⇒ Boolean
Constructor Details
#initialize(subject, subject_klass, association_name, url_factory, name = nil) ⇒ Association
Returns a new instance of Association.
264 265 266 267 268 269 270 271 |
# File 'lib/restful/serializer.rb', line 264 def initialize(subject, subject_klass, association_name, url_factory, name = nil) self.subject = subject self.subject_klass = subject_klass self.association_name = association_name self.url_factory = url_factory self.name = name || association_name self.association = subject.class.reflect_on_association(association_name) end |
Instance Attribute Details
#association ⇒ Object
Returns the value of attribute association.
262 263 264 |
# File 'lib/restful/serializer.rb', line 262 def association @association end |
#association_name ⇒ Object
Returns the value of attribute association_name.
262 263 264 |
# File 'lib/restful/serializer.rb', line 262 def association_name @association_name end |
#name ⇒ Object
Returns the value of attribute name.
262 263 264 |
# File 'lib/restful/serializer.rb', line 262 def name @name end |
#subject ⇒ Object
Returns the value of attribute subject.
262 263 264 |
# File 'lib/restful/serializer.rb', line 262 def subject @subject end |
#subject_klass ⇒ Object
Returns the value of attribute subject_klass.
262 263 264 |
# File 'lib/restful/serializer.rb', line 262 def subject_klass @subject_klass end |
#url_factory ⇒ Object
Returns the value of attribute url_factory.
262 263 264 |
# File 'lib/restful/serializer.rb', line 262 def url_factory @url_factory end |
Instance Method Details
#collective_href ⇒ Object
286 287 288 289 290 291 292 |
# File 'lib/restful/serializer.rb', line 286 def collective_href # try url_for nested resources first unless href = url_factory.create(:resources => [subject_klass, association_name], :args => subject.id) href = url_factory.create(:resources => association_name) end return href end |
#href ⇒ Object
277 278 279 280 281 282 283 284 |
# File 'lib/restful/serializer.rb', line 277 def href if singular? href = url_factory.create(:resources => association_name, :args => subject.send(association.name).id) else href = collective_href end return href end |
#singular? ⇒ Boolean
273 274 275 |
# File 'lib/restful/serializer.rb', line 273 def singular? [:belongs_to, :has_one].include?(association.macro) end |