/
belongs_to_association.rb
137 lines (111 loc) · 3.87 KB
/
belongs_to_association.rb
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# frozen_string_literal: true
module ActiveRecord
module Associations
# = Active Record Belongs To Association
class BelongsToAssociation < SingularAssociation #:nodoc:
def handle_dependency
return unless load_target
case options[:dependent]
when :destroy
raise ActiveRecord::Rollback unless target.destroy
when :destroy_async
id = owner.public_send(reflection.foreign_key.to_sym)
primary_key_column = reflection.active_record_primary_key.to_sym
enqueue_destroy_association(
owner_model_name: owner.class.to_s,
owner_id: owner.id,
association_class: reflection.klass.to_s,
association_ids: [id],
association_primary_key_column: primary_key_column,
ensuring_owner_was_method: options.fetch(:ensuring_owner_was, nil)
)
else
target.public_send(options[:dependent])
end
end
def inversed_from(record)
replace_keys(record)
super
end
def default(&block)
writer(owner.instance_exec(&block)) if reader.nil?
end
def reset
super
@updated = false
end
def updated?
@updated
end
def decrement_counters
update_counters(-1)
end
def increment_counters
update_counters(1)
end
def decrement_counters_before_last_save
if reflection.polymorphic?
model_was = owner.attribute_before_last_save(reflection.foreign_type)&.constantize
else
model_was = klass
end
foreign_key_was = owner.attribute_before_last_save(reflection.foreign_key)
if foreign_key_was && model_was < ActiveRecord::Base
update_counters_via_scope(model_was, foreign_key_was, -1)
end
end
def target_changed?
owner.saved_change_to_attribute?(reflection.foreign_key)
end
private
def replace(record)
if record
raise_on_type_mismatch!(record)
set_inverse_instance(record)
@updated = true
end
replace_keys(record, force: true)
self.target = record
end
def update_counters(by)
if require_counter_update? && foreign_key_present?
if target && !stale_target?
target.increment!(reflection.counter_cache_column, by, touch: reflection.options[:touch])
else
update_counters_via_scope(klass, owner._read_attribute(reflection.foreign_key), by)
end
end
end
def update_counters_via_scope(klass, foreign_key, by)
scope = klass.unscoped.where!(primary_key(klass) => foreign_key)
scope.update_counters(reflection.counter_cache_column => by, touch: reflection.options[:touch])
end
def find_target?
!loaded? && foreign_key_present? && klass
end
def require_counter_update?
reflection.counter_cache_column && owner.persisted?
end
def replace_keys(record, force: false)
target_key = record ? record._read_attribute(primary_key(record.class)) : nil
if force || owner[reflection.foreign_key] != target_key
owner[reflection.foreign_key] = target_key
end
end
def primary_key(klass)
reflection.association_primary_key(klass)
end
def foreign_key_present?
owner._read_attribute(reflection.foreign_key)
end
def invertible_for?(record)
inverse = inverse_reflection_for(record)
inverse && (inverse.has_one? || ActiveRecord::Base.has_many_inversing)
end
def stale_state
result = owner._read_attribute(reflection.foreign_key) { |n| owner.send(:missing_attribute, n, caller) }
result && result.to_s
end
end
end
end