Froze rails gems
[depot.git] / vendor / rails / activerecord / lib / active_record / associations / belongs_to_polymorphic_association.rb
1 module ActiveRecord
2 module Associations
3 class BelongsToPolymorphicAssociation < AssociationProxy #:nodoc:
4 def replace(record)
5 if record.nil?
6 @target = @owner[@reflection.primary_key_name] = @owner[@reflection.options[:foreign_type]] = nil
7 else
8 @target = (AssociationProxy === record ? record.target : record)
9
10 @owner[@reflection.primary_key_name] = record.id
11 @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
12
13 @updated = true
14 end
15
16 loaded
17 record
18 end
19
20 def updated?
21 @updated
22 end
23
24 private
25 def find_target
26 return nil if association_class.nil?
27
28 if @reflection.options[:conditions]
29 association_class.find(
30 @owner[@reflection.primary_key_name],
31 :select => @reflection.options[:select],
32 :conditions => conditions,
33 :include => @reflection.options[:include]
34 )
35 else
36 association_class.find(@owner[@reflection.primary_key_name], :select => @reflection.options[:select], :include => @reflection.options[:include])
37 end
38 end
39
40 def foreign_key_present
41 !@owner[@reflection.primary_key_name].nil?
42 end
43
44 def association_class
45 @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
46 end
47 end
48 end
49 end