Sha256: 3be9b08a0f2864064e6ac39b7a55bde1b2ae677aeef9fd856c59680ef8cfab03

Contents?: true

Size: 1.14 KB

Versions: 34

Compression:

Stored size: 1.14 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.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[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
          foreign_key = super
          foreign_key && [foreign_key.to_s, owner[reflection.foreign_type].to_s]
        end
    end
  end
end

Version data entries

34 entries across 34 versions & 4 rubygems

Version Path
activerecord-6.1.7.10 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.9 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.8 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.7 lib/active_record/associations/belongs_to_polymorphic_association.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.6 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.5 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.4 lib/active_record/associations/belongs_to_polymorphic_association.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.3 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.7 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.6.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.6 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.5.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.5 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.4.7 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.4.6 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-6.1.4.5 lib/active_record/associations/belongs_to_polymorphic_association.rb