Class: Morpheus::Mixins::Associations::BelongsToAssociation
- Inherits:
-
Association
- Object
- ActiveSupport::BasicObject
- Association
- Morpheus::Mixins::Associations::BelongsToAssociation
- Defined in:
- lib/morpheus/mixins/associations/belongs_to_association.rb
Instance Method Summary collapse
-
#initialize(owner, association, settings = {}) ⇒ BelongsToAssociation
constructor
The initializer calls out to the superclass’ initializer, then will set the options particular to itself.
-
#load_target! ⇒ Object
When loading the target, the association will only be loaded if the foreign_key has been set.
- #with_filter(filter) ⇒ Object
Methods inherited from Association
#id, #includes, #nil?, #to_param, #try
Constructor Details
#initialize(owner, association, settings = {}) ⇒ BelongsToAssociation
The initializer calls out to the superclass’ initializer, then will set the options particular to itself.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/morpheus/mixins/associations/belongs_to_association.rb', line 8 def initialize(owner, association, settings = {}) super # The foreign key is used to generate the url for a request. The default uses # the association name with an '_id' suffix as the generated key. For example, # belongs_to :school, will have the foreign key :school_id. @options[:foreign_key] ||= "#{@association.to_s}_id".to_sym # The primary key defaults to :id. @options[:primary_key] ||= :id # Associations can be marked as polymorphic. These associations will use # the returned type to instantiate the associated object. @options[:polymorphic] = settings[:options][:polymorphic] || false # @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
#load_target! ⇒ Object
When loading the target, the association will only be loaded if the foreign_key has been set. Additionally, the class used to find the record will be inferred by calling the method which is the name of the association with a ‘_type’ suffix. Alternatively, the class name can be set by using the :class_name option.
36 37 38 39 40 41 42 |
# File 'lib/morpheus/mixins/associations/belongs_to_association.rb', line 36 def load_target! if association_id = @owner.send(@options[:foreign_key]) polymorphic_class = @options[:polymorphic] ? @owner.send("#{@association}_type".to_sym).constantize : @options[:class_name].constantize attributes = [UrlBuilder.belongs_to(polymorphic_class, association_id), nil, { :id => association_id }] polymorphic_class.find(association_id) end end |
#with_filter(filter) ⇒ Object
28 29 30 |
# File 'lib/morpheus/mixins/associations/belongs_to_association.rb', line 28 def with_filter(filter) BelongsToAssociation.new(@owner, @association, :target => @target, :filters => @filters.dup.push(filter), :options => @options) end |