diff options
Diffstat (limited to 'activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb new file mode 100644 index 0000000000..9ae452e7a1 --- /dev/null +++ b/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -0,0 +1,36 @@ +# 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 + + def target_changed? + super || owner.saved_change_to_attribute?(reflection.foreign_type) + end + + private + def replace_keys(record) + super + owner[reflection.foreign_type] = record ? record.class.polymorphic_name : nil + 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 |