b92cbbdeab5616b8aeaeae076836f28483d4487b
3 class HasOneAssociation
< BelongsToAssociation
#:nodoc:
4 def initialize(owner
, reflection
)
9 def create(attrs
= {}, replace_existing
= true)
10 new_record(replace_existing
) do |reflection
|
11 reflection
.create_association(attrs
)
15 def create
!(attrs
= {}, replace_existing
= true)
16 new_record(replace_existing
) do |reflection
|
17 reflection
.create_association
!(attrs
)
21 def build(attrs
= {}, replace_existing
= true)
22 new_record(replace_existing
) do |reflection
|
23 reflection
.build_association(attrs
)
27 def replace(obj
, dont_save
= false)
30 unless @target.nil? || @target == obj
31 if dependent
? && !dont_save
32 case @reflection.options
[:dependent]
34 @target.delete
unless @target.new_record
?
35 @owner.clear_association_cache
37 @target.destroy
unless @target.new_record
?
38 @owner.clear_association_cache
40 @target[@reflection.primary_key_name
] = nil
41 @target.save
unless @owner.new_record
? || @target.new_record
?
44 @target[@reflection.primary_key_name
] = nil
45 @target.save
unless @owner.new_record
? || @target.new_record
?
52 raise_on_type_mismatch(obj
)
53 set_belongs_to_association_for(obj
)
54 @target = (AssociationProxy
=== obj
? obj
.target
: obj
)
59 unless @owner.new_record
? or obj
.nil? or dont_save
60 return (obj
.save
? self : false)
62 return (obj
.nil? ? nil : self)
68 if @reflection.options
[:primary_key]
69 @owner.class.quote_value(@owner.send(@reflection.options
[:primary_key]))
77 @reflection.klass
.find(:first,
78 :conditions => @finder_sql,
79 :select => @reflection.options
[:select],
80 :order => @reflection.options
[:order],
81 :include => @reflection.options
[:include],
82 :readonly => @reflection.options
[:readonly]
88 when @reflection.options
[:as]
90 "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_id = #{owner_quoted_id} AND " +
91 "#{@reflection.quoted_table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
93 @finder_sql = "#{@reflection.quoted_table_name}.#{@reflection.primary_key_name} = #{owner_quoted_id}"
95 @finder_sql << " AND (#{conditions})" if conditions
100 set_belongs_to_association_for(create_scoping
)
101 { :create => create_scoping
}
104 def new_record(replace_existing
)
105 # Make sure we load the target first, if we plan on replacing the existing
106 # instance. Otherwise, if the target has not previously been loaded
107 # elsewhere, the instance we create will get orphaned.
108 load_target
if replace_existing
109 record
= @reflection.klass
.send(:with_scope, :create => construct_scope
[:create]) do
114 replace(record
, true)
116 record
[@reflection.primary_key_name
] = @owner.id
unless @owner.new_record
?