Sha256: 89db21f72ccac95dfd9e211c2a1003cd22aba5f49a3ea8f6253904b23f63ab4f

Contents?: true

Size: 832 Bytes

Versions: 13

Compression:

Stored size: 832 Bytes

Contents

module Superstore
  module Associations
    class BelongsTo < Association
      def reader
        unless loaded?
          self.target = get_record
        end

        target
      end

      def writer(record)
        self.target = record
        owner.send("#{reflection.foreign_key}=", record.try(reflection.primary_key))
        if reflection.polymorphic?
          owner.send("#{reflection.polymorphic_column}=", record.class.name)
        end
      end

      private

        def get_record
          record_id = owner.send(reflection.foreign_key).presence
          return unless record_id

          if reflection.default_primary_key?
            association_class.find_by_id(record_id)
          else
            association_class.find_by(reflection.primary_key => record_id)
          end
        end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
superstore-2.4.4 lib/superstore/associations/belongs_to.rb
superstore-2.4.3 lib/superstore/associations/belongs_to.rb
superstore-2.4.2 lib/superstore/associations/belongs_to.rb
superstore-2.4.1 lib/superstore/associations/belongs_to.rb
superstore-2.4.0 lib/superstore/associations/belongs_to.rb
superstore-2.3.0 lib/superstore/associations/belongs_to.rb
superstore-2.2.0 lib/superstore/associations/belongs_to.rb
superstore-2.1.3 lib/superstore/associations/belongs_to.rb
superstore-2.1.2 lib/superstore/associations/belongs_to.rb
superstore-2.1.1 lib/superstore/associations/belongs_to.rb
superstore-2.1.0 lib/superstore/associations/belongs_to.rb
superstore-2.0.1 lib/superstore/associations/belongs_to.rb
superstore-2.0.0 lib/superstore/associations/belongs_to.rb