Module: AutotaskRuby::Association
- Included in:
- Entity
- Defined in:
- lib/autotask_ruby/association.rb
Overview
handles loading associated entities.
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ Object
loads parent entity.
-
#has_many(name, options = {}) ⇒ Object
loads child entities.
-
#has_one(name, options = {}) ⇒ Object
Example, an appointment can have one contact.
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
loads parent entity. Thanks to scoop for this example.
8 9 10 11 12 13 14 15 |
# File 'lib/autotask_ruby/association.rb', line 8 def belongs_to(name, = {}) name = name.to_s klass = ([:class_name] || name).to_s.classify.to_s foreign_key = name.foreign_key define_singleton_method(name) do find(klass, send(foreign_key)) end end |
#has_many(name, options = {}) ⇒ Object
loads child entities. Example: A project can have many tasks. Thanks to scoop for this example.
31 32 33 34 35 36 37 38 |
# File 'lib/autotask_ruby/association.rb', line 31 def has_many(name, = {}) name = name.to_s .reverse_merge! foreign_key: to_s.foreign_key.camelize klass = ([:class_name] || name).to_s.classify.to_s define_singleton_method(name) do query(klass, [:foreign_key], id) end end |
#has_one(name, options = {}) ⇒ Object
Example, an appointment can have one contact. Thanks to scoop for this example.
19 20 21 22 23 24 25 26 |
# File 'lib/autotask_ruby/association.rb', line 19 def has_one(name, = {}) name = name.to_s .reverse_merge! foreign_key: to_s.foreign_key.camelize klass = ([:class_name] || name).to_s.classify.to_s define_singleton_method(name) do find(klass, [:foreign_key], id).first end end |