Class: Morpheus::Mixins::Associations::HasManyAssociation
- Inherits:
-
Association
- Object
- ActiveSupport::BasicObject
- Association
- Morpheus::Mixins::Associations::HasManyAssociation
- Defined in:
- lib/morpheus/mixins/associations/has_many_association.rb
Instance Method Summary collapse
-
#<<(one_of_many) ⇒ Object
The append operator is used to append new resources to the association.
-
#initialize(owner, association, settings = {}) ⇒ HasManyAssociation
constructor
The initializer calls out to the superclass’ initializer and then sets the options particular to itself.
- #limit(amount) ⇒ Object
-
#load_target! ⇒ Object
When loading the target, the primary key is first checked.
- #page(page_number) ⇒ Object
-
#where(query_attributes) ⇒ Object
The where, limit, and page methods delegate to a Relation object.
- #with_filter(filter) ⇒ Object
Methods inherited from Association
#id, #includes, #nil?, #to_param, #try
Constructor Details
#initialize(owner, association, settings = {}) ⇒ HasManyAssociation
The initializer calls out to the superclass’ initializer and then sets the options particular to itself.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 8 def initialize(owner, association, settings = {}) super # The foreign key is used to generate the url for the association # request when the association is transformed into a relation. # The default is to use the class of the owner object with an '_id' # suffix. @options[:foreign_key] ||= "#{@owner.class.to_s.underscore}_id".to_sym # The primary key is used in the generated url for the target. It # defaults to :id. @options[:primary_key] ||= :id # @association_class stores the class of the association, constantized # from the named association (i.e. Automobile, Car, CarClub) @association_class = @options[:class_name].constantize end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Morpheus::Mixins::Associations::Association
Instance Method Details
#<<(one_of_many) ⇒ Object
The append operator is used to append new resources to the association.
64 65 66 67 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 64 def <<(one_of_many) @target ||= [] @target << one_of_many end |
#limit(amount) ⇒ Object
50 51 52 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 50 def limit(amount) transform_association_into_relation.limit(amount) end |
#load_target! ⇒ Object
When loading the target, the primary key is first checked. If the key is nil, then an empty array is returned. Otherwise, the target is requested at the generated url. For a has_many :meetings association on a class called Course, the generated url might look like this: /meetings?course_id=1, where the 1 is the primary key.
35 36 37 38 39 40 41 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 35 def load_target! if primary_key = @owner.send(@options[:primary_key]) Relation.new(@association.to_s.classify.constantize).where(@options[:foreign_key] => primary_key).all else [] end end |
#page(page_number) ⇒ Object
54 55 56 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 54 def page(page_number) transform_association_into_relation.page(page_number) end |
#where(query_attributes) ⇒ Object
The where, limit, and page methods delegate to a Relation object. The association generates a relation, and then calls the very same where, limit, or page method on that relation object.
46 47 48 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 46 def where(query_attributes) transform_association_into_relation.where(query_attributes) end |
#with_filter(filter) ⇒ Object
26 27 28 |
# File 'lib/morpheus/mixins/associations/has_many_association.rb', line 26 def with_filter(filter) HasManyAssociation.new(@owner, @association, :target => @target, :filters => @filters.dup.push(filter), :options => @options) end |