Sha256: 78846ab543c56af5922c6b516c6a3417fca8374597b7e4da90279dc69a9043f7

Contents?: true

Size: 1.39 KB

Versions: 60

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

Version data entries

60 entries across 57 versions & 7 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.5.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.0.8.7 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.5 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.4.2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.0.8.6 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.4.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.0.8.5 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.4 lib/active_record/associations/belongs_to_polymorphic_association.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.1.3.4/lib/active_record/associations/belongs_to_polymorphic_association.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8.4/lib/active_record/associations/belongs_to_polymorphic_association.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/belongs_to_polymorphic_association.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/belongs_to_polymorphic_association.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activerecord-7.0.5.1/lib/active_record/associations/belongs_to_polymorphic_association.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activerecord-7.1.3.4/lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.3.4 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.0.8.4 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.3.2 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.1.3.1 lib/active_record/associations/belongs_to_polymorphic_association.rb
activerecord-7.0.8.1 lib/active_record/associations/belongs_to_polymorphic_association.rb