Sha256: c425ad5270890063a696bff3368a0d9c7b9d2249cafed2f054de31821956fcda

Contents?: true

Size: 1.39 KB

Versions: 24

Compression:

Stored size: 1.39 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 && owner.class.polymorphic_class_for(type)
      end

      def target_changed?
        super || owner.attribute_changed?(reflection.foreign_type)
      end

      def target_previously_changed?
        super || owner.attribute_previously_changed?(reflection.foreign_type)
      end

      def saved_change_to_target?
        super || owner.saved_change_to_attribute?(reflection.foreign_type)
      end

      private
        def replace_keys(record, force: false)
          super

          target_type = record ? record.class.polymorphic_name : nil

          if force || owner._read_attribute(reflection.foreign_type) != target_type
            owner[reflection.foreign_type] = target_type
          end
        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
          if foreign_key = super
            [foreign_key, owner[reflection.foreign_type]]
          end
        end
    end
  end
end

Version data entries

24 entries across 24 versions & 2 rubygems

Version Path
activerecord-8.0.2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-8.0.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-8.0.0.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.2.2.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-8.0.0 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.2.2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-8.0.0.rc2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.2.1.2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-8.0.0.rc1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.2.1.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-8.0.0.beta1 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha9 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha8 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha7 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha4 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha3 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha2 lib/active_record/associations/belongs_to_polymorphic_association.rb
omg-activerecord-8.0.0.alpha1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.2.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.2.0 lib/active_record/associations/belongs_to_polymorphic_association.rb