Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/tags/rel_2-0-2/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb

Revision 5018, 1.4 kB (checked in by david, 2 years ago)

Backed out of new_record? to new? transformation as it would screw up existing models that did boolean calls on "new" attributes [DHH]

Line 
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           unless record.new_record?
11             @owner[@reflection.primary_key_name] = record.id
12             @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
13           end
14
15           @updated = true
16         end
17
18         loaded
19         record
20       end
21
22       def updated?
23         @updated
24       end
25
26       private
27         def find_target
28           return nil if association_class.nil?
29
30           if @reflection.options[:conditions]
31             association_class.find(
32               @owner[@reflection.primary_key_name],
33               :conditions => conditions,
34               :include    => @reflection.options[:include]
35             )
36           else
37             association_class.find(@owner[@reflection.primary_key_name], :include => @reflection.options[:include])
38           end
39         end
40
41         def foreign_key_present
42           !@owner[@reflection.primary_key_name].nil?
43         end
44
45         def association_class
46           @owner[@reflection.options[:foreign_type]] ? @owner[@reflection.options[:foreign_type]].constantize : nil
47         end
48     end
49   end
50 end
Note: See TracBrowser for help on using the browser.