Class: Restful::Association
- Inherits:
-
Object
- Object
- Restful::Association
- Includes:
- ActionController::UrlWriter, UrlForHelpers
- 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.
Instance Method Summary collapse
- #collective_href ⇒ Object
- #href ⇒ Object
-
#initialize(subject, subject_klass, association_name, name = nil) ⇒ Association
constructor
A new instance of Association.
- #singular? ⇒ Boolean
Methods included from UrlForHelpers
Constructor Details
#initialize(subject, subject_klass, association_name, name = nil) ⇒ Association
Returns a new instance of Association.
140 141 142 143 144 145 146 |
# File 'lib/restful/serializer.rb', line 140 def initialize(subject, subject_klass, association_name, name = nil) self.subject = subject self.subject_klass = subject_klass self.association_name = association_name 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.
138 139 140 |
# File 'lib/restful/serializer.rb', line 138 def association @association end |
#association_name ⇒ Object
Returns the value of attribute association_name.
138 139 140 |
# File 'lib/restful/serializer.rb', line 138 def association_name @association_name end |
#name ⇒ Object
Returns the value of attribute name.
138 139 140 |
# File 'lib/restful/serializer.rb', line 138 def name @name end |
#subject ⇒ Object
Returns the value of attribute subject.
138 139 140 |
# File 'lib/restful/serializer.rb', line 138 def subject @subject end |
#subject_klass ⇒ Object
Returns the value of attribute subject_klass.
138 139 140 |
# File 'lib/restful/serializer.rb', line 138 def subject_klass @subject_klass end |
Instance Method Details
#collective_href ⇒ Object
161 162 163 164 165 166 167 |
# File 'lib/restful/serializer.rb', line 161 def collective_href # try url_for nested resources first unless href = get_url_for(:resources => [subject_klass, association_name], :args => subject.id) href = get_url_for(:resources => association_name) end return href end |
#href ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/restful/serializer.rb', line 152 def href if singular? href = get_url_for(:resources => association_name, :args => subject.send(association.name).id) else href = collective_href end return href end |
#singular? ⇒ Boolean
148 149 150 |
# File 'lib/restful/serializer.rb', line 148 def singular? [:belongs_to, :has_one].include?(association.macro) end |