blob: 7503130e8c9bd9642c75c780a6c2ecf2e4679068 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
module ActiveRecord
# = Active Record Belongs To Has One Association
module Associations
class HasOneAssociation < AssociationProxy #:nodoc:
def create(attributes = {})
new_record(:create_association, attributes)
end
def create!(attributes = {})
new_record(:create_association!, attributes)
end
def build(attributes = {})
new_record(:build_association, attributes)
end
def replace(obj, save = true)
load_target
unless @target.nil? || @target == obj
if @reflection.options[:dependent] && save
case @reflection.options[:dependent]
when :delete
@target.delete if @target.persisted?
when :destroy
@target.destroy if @target.persisted?
when :nullify
@target[@reflection.foreign_key] = nil
@target.save if @owner.persisted? && @target.persisted?
end
else
@target[@reflection.foreign_key] = nil
@target.save if @owner.persisted? && @target.persisted?
end
end
if obj.nil?
@target = nil
else
raise_on_type_mismatch(obj)
set_owner_attributes(obj)
@target = (AssociationProxy === obj ? obj.target : obj)
end
set_inverse_instance(obj)
loaded
unless !@owner.persisted? || obj.nil? || !save
return (obj.save ? self : false)
else
return (obj.nil? ? nil : self)
end
end
private
def find_target
scoped.first.tap { |record| set_inverse_instance(record) }
end
def association_scope
super.order(@reflection.options[:order])
end
alias creation_attributes construct_owner_attributes
def new_record(method, attributes)
record = scoped.scoping { @reflection.send(method, attributes) }
replace(record, false)
record
end
end
end
end
|