Class: Her::Model::Associations::BelongsToAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/her_extension/model/associations/belongs_to_association.rb

Instance Attribute Summary

Attributes inherited from Association

#params

Instance Method Summary collapse

Methods inherited from Association

#blank_relation, #build_request_path, #method_for, #method_missing, #new_collection, #reload, #request, #to_params

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Her::Model::Associations::Association

Instance Method Details

#fetchObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/her_extension/model/associations/belongs_to_association.rb', line 8

def fetch
  foreign_key_value = @parent.attributes[@opts[:foreign_key].to_sym]
  data_key_value = @parent.attributes[@opts[:data_key].to_sym]
  return @opts[:default].try(:dup) if (@parent.attributes.include?(@name) && @parent.attributes[@name].nil? && @params.empty?) || (foreign_key_value.blank? && data_key_value.blank?)

  return @cached_result unless @params.any? || @cached_result.nil?
  return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?

  path_params = @parent.attributes.merge(@params.merge(@klass.primary_key => foreign_key_value))
  path = build_association_path lambda { @klass.build_request_path(path_params) }
  @klass.get(path, @params).tap do |result|
    @cached_result = result if @params.blank?
  end
end