Class: ReactiveResource::Association::HasManyAssociation
- Inherits:
-
Object
- Object
- ReactiveResource::Association::HasManyAssociation
- Defined in:
- lib/reactive_resource/association/has_many_association.rb
Overview
Represents and resolves a has_many association.
Instance Attribute Summary collapse
-
#attribute ⇒ Object
readonly
The attribute name this association represents.
-
#klass ⇒ Object
readonly
The class this association is attached to.
-
#options ⇒ Object
readonly
additional options passed in when the association was created.
Instance Method Summary collapse
-
#add_helper_methods(klass, attribute) ⇒ Object
Adds methods for has_many associations, to make dealing with these objects a bit more straightforward.
-
#associated_class ⇒ Object
Returns the class name of the target of the association.
-
#initialize(klass, attribute, options) ⇒ HasManyAssociation
constructor
Create a new has_many association.
-
#resolve_relationship(object) ⇒ Object
Called when this assocation is referenced.
Constructor Details
#initialize(klass, attribute, options) ⇒ HasManyAssociation
Create a new has_many association.
53 54 55 56 57 58 59 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 53 def initialize(klass, attribute, ) @klass = klass @attribute = attribute @options = add_helper_methods(klass, attribute) end |
Instance Attribute Details
#attribute ⇒ Object (readonly)
The attribute name this association represents
10 11 12 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 10 def attribute @attribute end |
#klass ⇒ Object (readonly)
The class this association is attached to
7 8 9 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 7 def klass @klass end |
#options ⇒ Object (readonly)
additional options passed in when the association was created
13 14 15 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 13 def @options end |
Instance Method Details
#add_helper_methods(klass, attribute) ⇒ Object
Adds methods for has_many associations, to make dealing with these objects a bit more straightforward. If the attribute name is lawyers
, it will add:
- lawyers
-
returns the associated lawyers
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 38 def add_helper_methods(klass, attribute) association = self klass.class_eval do # lawyer.addresses define_method(attribute) do unless instance_variable_get("@#{attribute}") object = association.resolve_relationship(self) instance_variable_set("@#{attribute}", object) end instance_variable_get("@#{attribute}") end end end |
#associated_class ⇒ Object
Returns the class name of the target of the association. Based off of attribute
unless class_name
was passed in the options
hash.
18 19 20 21 22 23 24 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 18 def associated_class if [:class_name] [:class_name].constantize else klass.relative_const_get(attribute.to_s.singularize.camelize) end end |
#resolve_relationship(object) ⇒ Object
Called when this assocation is referenced. Finds and returns the targets of this association.
28 29 30 31 |
# File 'lib/reactive_resource/association/has_many_association.rb', line 28 def resolve_relationship(object) id_attribute = "#{klass.name.split("::").last.underscore}_id" associated_class.find(:all, :params => object..merge(id_attribute => object.id)) end |