Sha256: 83abf43c3c6088331a4c75cdd9cdd76ca73fed8aa25959a9251cf6f6449a5c97

Contents?: true

Size: 1.07 KB

Versions: 11

Compression:

Stored size: 1.07 KB

Contents

module ActiveForce
  module Association
    class BelongsToAssociation < Association
      def relationship_name
        options[:relationship_name] || default_relationship_name
      end

      private

      def loadable?(owner)
        foreign_key_value(owner).present?
      end

      def target(owner)
        relation_model.find(foreign_key_value(owner))
      end

      def default_relationship_name
        if !parent.custom_table? && !relation_model.custom_table?
          relation_model.table_name
        else
          parent.mappings[foreign_key].gsub(/__c\z/, '__r')
        end
      end

      def default_foreign_key
        infer_foreign_key_from_model relation_model
      end

      def foreign_key_value(owner)
        owner&.public_send(foreign_key)
      end

      def define_assignment_method
        association = self
        method_name = relation_name
        parent.send :define_method, "#{method_name}=" do |other|
          send "#{association.foreign_key}=", other&.id
          association_cache[method_name] = other
        end
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
active_force-0.24.0 lib/active_force/association/belongs_to_association.rb
active_force-0.23.0 lib/active_force/association/belongs_to_association.rb
active_force-0.22.1 lib/active_force/association/belongs_to_association.rb
active_force-0.22.0 lib/active_force/association/belongs_to_association.rb
active_force-0.21.0 lib/active_force/association/belongs_to_association.rb
active_force-0.20.1 lib/active_force/association/belongs_to_association.rb
active_force-0.20.0 lib/active_force/association/belongs_to_association.rb
active_force-0.19.0 lib/active_force/association/belongs_to_association.rb
active_force-0.18.0 lib/active_force/association/belongs_to_association.rb
active_force-0.17.0 lib/active_force/association/belongs_to_association.rb
active_force-0.16.0 lib/active_force/association/belongs_to_association.rb