aboutsummaryrefslogblamecommitdiffstats
path: root/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
blob: 4608ffad679cd129866123056e797261dac12c84 (plain) (tree)
1
2
3
4
5
6
7
8
9
                   
                                                      
                     
                                                                         

                                        
                                                                               
                                                                  
                                                      
                                                                   

                                                                 




               
             
 

                                
                                                                                   

           

                                               

           

                                                          

           
                        
                                                 
                                  

           

                                                                                



           
module ActiveRecord
  # = Active Record Belongs To Polymorphic Association
  module Associations
    class BelongsToPolymorphicAssociation < BelongsToAssociation #:nodoc:
      def stale_target?
        if @target && @target.persisted?
          target_id    = @target.send(@reflection.association_primary_key).to_s
          foreign_key  = @owner.send(@reflection.foreign_key).to_s
          target_type  = @target.class.base_class.name
          foreign_type = @owner.send(@reflection.foreign_type).to_s

          target_id != foreign_key || target_type != foreign_type
        else
          false
        end
      end

      private

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

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

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

        def target_klass
          type = @owner[@reflection.foreign_type]
          type && type.constantize
        end

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