Sha256: af653b2bde565d5b206079fa12425a61b5b9fa58ada87835c8bf3b813d025362

Contents?: true

Size: 1.08 KB

Versions: 16

Compression:

Stored size: 1.08 KB

Contents

# Backport https://github.com/remiprev/her/pull/343/files
# Fix belongs_to when foreign key is nil
module Her
  module Model
    module Associations
      class BelongsToAssociation < Association
        # @private
        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
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
mno-enterprise-core-3.4.0 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.3.3 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.3.2 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.2.1 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.3.1 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.3.0 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.2.0 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.1.4 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.0.7 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-2.0.9 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.1.3 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.0.6 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-2.0.8 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.1.2 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-3.0.5 lib/her_extension/model/associations/belongs_to_association.rb
mno-enterprise-core-2.0.7 lib/her_extension/model/associations/belongs_to_association.rb