X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=vendor%2Frails%2Factiverecord%2Flib%2Factive_record%2Fassociations%2Fbelongs_to_polymorphic_association.rb;fp=vendor%2Frails%2Factiverecord%2Flib%2Factive_record%2Fassociations%2Fbelongs_to_polymorphic_association.rb;h=d8146daa54ee4a1693e4a055ce9c3d4c7ada8eac;hb=d115f2e23823271635bad69229a42cd8ac68debe;hp=0000000000000000000000000000000000000000;hpb=37cb670bf3ddde90b214e591f100ed4446469484;p=depot.git diff --git a/vendor/rails/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb b/vendor/rails/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb new file mode 100644 index 0000000..d8146da --- /dev/null +++ b/vendor/rails/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb @@ -0,0 +1,49 @@ +module ActiveRecord + module Associations + class BelongsToPolymorphicAssociation < AssociationProxy #:nodoc: + def replace(record) + if record.nil? + @target = @owner[@reflection.primary_key_name] = @owner[@reflection.options[:foreign_type]] = nil + else + @target = (AssociationProxy === record ? record.target : record) + + @owner[@reflection.primary_key_name] = record.id + @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s + + @updated = true + end + + loaded + record + end + + def updated? + @updated + end + + private + def find_target + return nil if association_class.nil? + + if @reflection.options[:conditions] + association_class.find( + @owner[@reflection.primary_key_name], + :select => @reflection.options[:select], + :conditions => conditions, + :include => @reflection.options[:include] + ) + else + association_class.find(@owner[@reflection.primary_key_name], :select => @reflection.options[:select], :include => @reflection.options[:include]) + end + end + + def foreign_key_present + !@owner[@reflection.primary_key_name].nil? + end + + def association_class + @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil + end + end + end +end