-
Notifications
You must be signed in to change notification settings - Fork 878
/
base.rb
320 lines (291 loc) · 11.1 KB
/
base.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# frozen_string_literal: true
module PaperTrail
module Events
# We refer to times in the lifecycle of a record as "events". There are
# three events:
#
# - create
# - `after_create` we call `RecordTrail#record_create`
# - update
# - `after_update` we call `RecordTrail#record_update`
# - `after_touch` we call `RecordTrail#record_update`
# - `RecordTrail#save_with_version` calls `RecordTrail#record_update`
# - `RecordTrail#update_columns` is also referred to as an update, though
# it uses `RecordTrail#record_update_columns` rather than
# `RecordTrail#record_update`
# - destroy
# - `before_destroy` or `after_destroy` we call `RecordTrail#record_destroy`
#
# The value inserted into the `event` column of the versions table can also
# be overridden by the user, with `paper_trail_event`.
#
# @api private
class Base
RAILS_GTE_5_1 = ::ActiveRecord.gem_version >= ::Gem::Version.new("5.1.0.beta1")
# @api private
def initialize(record, in_after_callback)
@record = record
@in_after_callback = in_after_callback
end
# Determines whether it is appropriate to generate a new version
# instance. A timestamp-only update (e.g. only `updated_at` changed) is
# considered notable unless an ignored attribute was also changed.
#
# @api private
def changed_notably?
if ignored_attr_has_changed?
timestamps = @record.send(:timestamp_attributes_for_update_in_model).map(&:to_s)
(notably_changed - timestamps).any?
else
notably_changed.any?
end
end
private
# Rails 5.1 changed the API of `ActiveRecord::Dirty`. See
# https://github.com/paper-trail-gem/paper_trail/pull/899
#
# @api private
def attribute_changed_in_latest_version?(attr_name)
if @in_after_callback && RAILS_GTE_5_1
@record.saved_change_to_attribute?(attr_name.to_s)
else
@record.attribute_changed?(attr_name.to_s)
end
end
# @api private
def nonskipped_attributes_before_change(is_touch)
cache_changed_attributes do
record_attributes = @record.attributes.except(*@record.paper_trail_options[:skip])
record_attributes.each_key do |k|
if @record.class.column_names.include?(k)
record_attributes[k] = attribute_in_previous_version(k, is_touch)
end
end
end
end
# Rails 5.1 changed the API of `ActiveRecord::Dirty`.
# @api private
def cache_changed_attributes
if RAILS_GTE_5_1
# Everything works fine as it is
yield
else
# Any particular call to `changed_attributes` produces the huge memory allocation.
# Lets use the generic AR workaround for that.
@record.send(:cache_changed_attributes) { yield }
end
end
# Rails 5.1 changed the API of `ActiveRecord::Dirty`. See
# https://github.com/paper-trail-gem/paper_trail/pull/899
#
# Event can be any of the three (create, update, destroy).
#
# @api private
def attribute_in_previous_version(attr_name, is_touch)
if RAILS_GTE_5_1
if @in_after_callback && !is_touch
# For most events, we want the original value of the attribute, before
# the last save.
@record.attribute_before_last_save(attr_name.to_s)
else
# We are either performing a `record_destroy` or a
# `record_update(is_touch: true)`.
@record.attribute_in_database(attr_name.to_s)
end
else
@record.attribute_was(attr_name.to_s)
end
end
# @api private
def changed_and_not_ignored
ignore = @record.paper_trail_options[:ignore].dup
# Remove Hash arguments and then evaluate whether the attributes (the
# keys of the hash) should also get pushed into the collection.
ignore.delete_if do |obj|
obj.is_a?(Hash) &&
obj.each { |attr, condition|
ignore << attr if condition.respond_to?(:call) && condition.call(@record)
}
end
skip = @record.paper_trail_options[:skip]
(changed_in_latest_version - ignore) - skip
end
# @api private
def changed_in_latest_version
# Memoized to reduce memory usage
@changed_in_latest_version ||= changes_in_latest_version.keys
end
# Rails 5.1 changed the API of `ActiveRecord::Dirty`. See
# https://github.com/paper-trail-gem/paper_trail/pull/899
#
# @api private
def changes_in_latest_version
# Memoized to reduce memory usage
@changes_in_latest_version ||= begin
if @in_after_callback && RAILS_GTE_5_1
@record.saved_changes
else
@record.changes
end
end
end
# An attributed is "ignored" if it is listed in the `:ignore` option
# and/or the `:skip` option. Returns true if an ignored attribute has
# changed.
#
# @api private
def ignored_attr_has_changed?
ignored = @record.paper_trail_options[:ignore] + @record.paper_trail_options[:skip]
ignored.any? && (changed_in_latest_version & ignored).any?
end
# PT 10 has a new optional column, `item_subtype`
#
# @api private
def merge_item_subtype_into(data)
if @record.class.paper_trail.version_class.columns_hash.key?("item_subtype")
data.merge!(item_subtype: @record.class.name)
end
end
# Updates `data` from the model's `meta` option and from `controller_info`.
# Metadata is always recorded; that means all three events (create, update,
# destroy) and `update_columns`.
#
# @api private
def merge_metadata_into(data)
merge_metadata_from_model_into(data)
merge_metadata_from_controller_into(data)
end
# Updates `data` from `controller_info`.
#
# @api private
def merge_metadata_from_controller_into(data)
data.merge(PaperTrail.request.controller_info || {})
end
# Updates `data` from the model's `meta` option.
#
# @api private
def merge_metadata_from_model_into(data)
@record.paper_trail_options[:meta].each do |k, v|
data[k] = model_metadatum(v, data[:event])
end
end
# Given a `value` from the model's `meta` option, returns an object to be
# persisted. The `value` can be a simple scalar value, but it can also
# be a symbol that names a model method, or even a Proc.
#
# @api private
def model_metadatum(value, event)
if value.respond_to?(:call)
value.call(@record)
elsif value.is_a?(Symbol) && @record.respond_to?(value, true)
# If it is an attribute that is changing in an existing object,
# be sure to grab the current version.
if event != "create" &&
@record.has_attribute?(value) &&
attribute_changed_in_latest_version?(value)
attribute_in_previous_version(value, false)
else
@record.send(value)
end
else
value
end
end
# @api private
def notable_changes
changes_in_latest_version.delete_if { |k, _v|
!notably_changed.include?(k)
}
end
# @api private
def notably_changed
# Memoized to reduce memory usage
@notably_changed ||= begin
only = @record.paper_trail_options[:only].dup
# Remove Hash arguments and then evaluate whether the attributes (the
# keys of the hash) should also get pushed into the collection.
only.delete_if do |obj|
obj.is_a?(Hash) &&
obj.each { |attr, condition|
only << attr if condition.respond_to?(:call) && condition.call(@record)
}
end
only.empty? ? changed_and_not_ignored : (changed_and_not_ignored & only)
end
end
# Returns hash of attributes (with appropriate attributes serialized),
# omitting attributes to be skipped.
#
# @api private
def object_attrs_for_paper_trail(is_touch)
attrs = nonskipped_attributes_before_change(is_touch)
AttributeSerializers::ObjectAttribute.new(@record.class).serialize(attrs)
attrs
end
# @api private
def prepare_object_changes(changes)
changes = serialize_object_changes(changes)
changes = recordable_object_changes(changes)
changes
end
# Returns an object which can be assigned to the `object_changes`
# attribute of a nascent version record. If the `object_changes` column is
# a postgres `json` column, then a hash can be used in the assignment,
# otherwise the column is a `text` column, and we must perform the
# serialization here, using `PaperTrail.serializer`.
#
# @api private
# @param changes HashWithIndifferentAccess
def recordable_object_changes(changes)
if PaperTrail.config.object_changes_adapter&.respond_to?(:diff)
# We'd like to avoid the `to_hash` here, because it increases memory
# usage, but that would be a breaking change because
# `object_changes_adapter` expects a plain `Hash`, not a
# `HashWithIndifferentAccess`.
changes = PaperTrail.config.object_changes_adapter.diff(changes.to_hash)
end
if @record.class.paper_trail.version_class.object_changes_col_is_json?
changes
else
PaperTrail.serializer.dump(changes)
end
end
# Returns a boolean indicating whether to store serialized version diffs
# in the `object_changes` column of the version record.
#
# @api private
def record_object_changes?
@record.class.paper_trail.version_class.column_names.include?("object_changes")
end
# Returns a boolean indicating whether to store the original object during save.
#
# @api private
def record_object?
@record.class.paper_trail.version_class.column_names.include?("object")
end
# Returns an object which can be assigned to the `object` attribute of a
# nascent version record. If the `object` column is a postgres `json`
# column, then a hash can be used in the assignment, otherwise the column
# is a `text` column, and we must perform the serialization here, using
# `PaperTrail.serializer`.
#
# @api private
def recordable_object(is_touch)
if @record.class.paper_trail.version_class.object_col_is_json?
object_attrs_for_paper_trail(is_touch)
else
PaperTrail.serializer.dump(object_attrs_for_paper_trail(is_touch))
end
end
# @api private
def serialize_object_changes(changes)
AttributeSerializers::ObjectChangesAttribute.
new(@record.class).
serialize(changes)
# We'd like to convert this `HashWithIndifferentAccess` to a plain
# `Hash`, but we don't, to save memory.
changes
end
end
end
end