Sha256: bf84420ef857e6c2ff0fa8c321b43b1c7069f5cd9dacbfce2040781b15943196

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module Associations
    # = Active Record Belongs To Polymorphic Association
    class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
      def klass
        type = owner[reflection.foreign_type]
        type.presence && type.constantize
      end

      private

        def replace_keys(record)
          super
          owner[reflection.foreign_type] = record.class.base_class.name
        end

        def remove_keys
          super
          owner[reflection.foreign_type] = nil
        end

        def different_target?(record)
          super || record.class != klass
        end

        def inverse_reflection_for(record)
          reflection.polymorphic_inverse_of(record.class)
        end

        def raise_on_type_mismatch!(record)
          # A polymorphic association cannot have a type mismatch, by definition
        end

        def stale_state
          foreign_key = super
          foreign_key && [foreign_key.to_s, owner[reflection.foreign_type].to_s]
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
activerecord-5.2.0.beta2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-5.2.0.beta1 lib/active_record/associations/belongs_to_polymorphic_association.rb
ruby-on-quails-0.1.0 activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb