forked from mongodb/mongoid
/
dirty.rb
287 lines (261 loc) · 7.33 KB
/
dirty.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# encoding: utf-8
module Mongoid #:nodoc:
module Dirty #:nodoc:
extend ActiveSupport::Concern
# Get the changed attributes for the document.
#
# @example Get the changed attributes.
# model.changed
#
# @return [ Array<String> ] The changed attributes.
#
# @since 2.4.0
def changed
changed_attributes.keys
end
# Has the document changed?
#
# @example Has the document changed?
# model.changed?
#
# @return [ true, false ] If the document is changed.
#
# @since 2.4.0
def changed?
changed_attributes.any? || children_changed?
end
# Have any children (embedded documents) of this document changed?
#
# @example Have any children changed?
# model.children_changed?
#
# @return [ true, false ] If any children have changed.
#
# @since 2.4.1
def children_changed?
_children.any? do |child|
child.changed?
end
end
# Get the attribute changes.
#
# @example Get the attribute changes.
# model.changed_attributes
#
# @return [ Hash<String, Object> ] The attribute changes.
#
# @since 2.4.0
def changed_attributes
@changed_attributes ||= {}
end
# Get all the changes for the document.
#
# @example Get all the changes.
# model.changes
#
# @return [ Hash<String, Array<Object, Object> ] The changes.
#
# @since 2.4.0
def changes
changed.inject({}.with_indifferent_access) do |changes, attr|
changes.tap do |hash|
hash[attr] = attribute_change(attr)
end
end
end
# Call this method after save, so the changes can be properly switched.
#
# This will unset the memoized children array, set new record to
# false, set the document as validated, and move the dirty changes.
#
# @example Move the changes to previous.
# person.move_changes
#
# @since 2.1.0
def move_changes
@_children = nil
@previous_changes = changes
Atomic::UPDATES.each do |update|
send(update).clear
end
changed_attributes.clear
end
# Get the previous changes on the document.
#
# @example Get the previous changes.
# model.previous_changes
#
# @return [ Hash<String, Array<Object, Object> ] The previous changes.
#
# @since 2.4.0
def previous_changes
@previous_changes
end
# Remove a change from the dirty attributes hash. Used by the single field
# atomic updators.
#
# @example Remove a flagged change.
# model.remove_change(:field)
#
# @param [ Symbol, String ] name The name of the field.
#
# @since 2.1.0
def remove_change(name)
changed_attributes.delete(name.to_s)
end
# Gets all the new values for each of the changed fields, to be passed to
# a MongoDB $set modifier.
#
# @todo: Durran: Refactor 3.0
#
# @example Get the setters for the atomic updates.
# person = Person.new(:title => "Sir")
# person.title = "Madam"
# person.setters # returns { "title" => "Madam" }
#
# @return [ Hash ] A +Hash+ of atomic setters.
def setters
{}.tap do |modifications|
changes.each_pair do |name, changes|
if changes
old, new = changes
field = fields[name]
key = embedded? ? "#{atomic_position}.#{name}" : name
if field && field.resizable?
field.add_atomic_changes(
self,
name,
key,
modifications,
new,
old
)
else
modifications[key] = new
end
end
end
end
end
private
# Get the old and new value for the provided attribute.
#
# @example Get the attribute change.
# model.attribute_change("name")
#
# @param [ String ] attr The name of the attribute.
#
# @return [ Array<Object> ] The old and new values.
#
# @since 2.1.0
def attribute_change(attr)
[changed_attributes[attr], attributes[attr]] if attribute_changed?(attr)
end
# Determine if a specific attribute has changed.
#
# @example Has the attribute changed?
# model.attribute_changed?("name")
#
# @param [ String ] attr The name of the attribute.
#
# @return [ true, false ] Whether the attribute has changed.
#
# @since 2.1.6
def attribute_changed?(attr)
return false unless changed_attributes.has_key?(attr)
changed_attributes[attr] != attributes[attr]
end
# Get the previous value for the attribute.
#
# @example Get the previous value.
# model.attribute_was("name")
#
# @param [ String ] attr The attribute name.
#
# @since 2.4.0
def attribute_was(attr)
attribute_changed?(attr) ? changed_attributes[attr] : attributes[attr]
end
# Flag an attribute as going to change.
#
# @example Flag the attribute.
# model.attribute_will_change!("name")
#
# @param [ String ] attr The name of the attribute.
#
# @return [ Object ] The old value.
#
# @since 2.3.0
def attribute_will_change!(attr)
unless changed_attributes.has_key?(attr)
changed_attributes[attr] = read_attribute(attr)._deep_copy
end
end
# Set the attribute back to it's old value.
#
# @example Reset the attribute.
# model.reset_attribute!("name")
#
# @param [ String ] attr The name of the attribute.
#
# @return [ Object ] The old value.
#
# @since 2.4.0
def reset_attribute!(attr)
attributes[attr] = changed_attributes[attr] if attribute_changed?(attr)
end
module ClassMethods #:nodoc:
private
# Generate all the dirty methods needed for the attribute.
#
# @example Generate the dirty methods.
# Model.create_dirty_methods("name", "name")
#
# @param [ String ] name The name of the field.
# @param [ String ] name The name of the accessor.
#
# @return [ Module ] The fields module.
#
# @since 2.4.0
def create_dirty_methods(name, meth)
generated_methods.module_eval do
if meth =~ /\W/
define_method("#{meth}_change") do
attribute_change(name)
end
define_method("#{meth}_changed?") do
attribute_changed?(name)
end
define_method("#{meth}_was") do
attribute_was(name)
end
define_method("#{meth}_will_change!") do
attribute_will_change!(name)
end
define_method("reset_#{meth}!") do
reset_attribute!(name)
end
else
class_eval <<-EOM
def #{meth}_change
attribute_change(#{name.inspect})
end
def #{meth}_changed?
attribute_changed?(#{name.inspect})
end
def #{meth}_was
attribute_was(#{name.inspect})
end
def #{meth}_will_change!
attribute_will_change!(#{name.inspect})
end
def reset_#{meth}!
reset_attribute!(#{name.inspect})
end
EOM
end
end
end
end
end
end