2007-01-22 14:43:50 +01:00
|
|
|
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)
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
@owner[@reflection.primary_key_name] = record_id(record)
|
2008-06-02 08:35:38 +02:00
|
|
|
@owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
|
2007-01-22 14:43:50 +01:00
|
|
|
|
|
|
|
@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(
|
2008-06-02 08:35:38 +02:00
|
|
|
@owner[@reflection.primary_key_name],
|
|
|
|
:select => @reflection.options[:select],
|
2007-01-22 14:43:50 +01:00
|
|
|
:conditions => conditions,
|
|
|
|
:include => @reflection.options[:include]
|
|
|
|
)
|
|
|
|
else
|
2008-06-02 08:35:38 +02:00
|
|
|
association_class.find(@owner[@reflection.primary_key_name], :select => @reflection.options[:select], :include => @reflection.options[:include])
|
2007-01-22 14:43:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def foreign_key_present
|
|
|
|
!@owner[@reflection.primary_key_name].nil?
|
|
|
|
end
|
|
|
|
|
2009-08-04 17:16:03 +02:00
|
|
|
def record_id(record)
|
|
|
|
record.send(@reflection.options[:primary_key] || :id)
|
|
|
|
end
|
|
|
|
|
2007-01-22 14:43:50 +01:00
|
|
|
def association_class
|
|
|
|
@owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|