-
Notifications
You must be signed in to change notification settings - Fork 130
/
trackable.rb
539 lines (461 loc) · 21.2 KB
/
trackable.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
module Mongoid
module History
module Trackable
extend ActiveSupport::Concern
module ClassMethods
def track_history(options = {})
extend RelationMethods
history_options = Mongoid::History::Options.new(self, options)
field history_options.options[:version_field].to_sym, type: Integer
unless history_options.options[:modifier_field].nil?
belongs_to_modifier_options = { class_name: Mongoid::History.modifier_class_name }
belongs_to_modifier_options[:inverse_of] = history_options.options[:modifier_field_inverse_of] if history_options.options.key?(:modifier_field_inverse_of)
belongs_to_modifier_options[:optional] = true if history_options.options[:modifier_field_optional] && Mongoid::Compatibility::Version.mongoid6_or_newer?
belongs_to history_options.options[:modifier_field].to_sym, belongs_to_modifier_options
end
include MyInstanceMethods
extend SingletonMethods
delegate :history_trackable_options, to: 'self.class'
delegate :track_history?, to: 'self.class'
callback_options = history_options.options.slice(:if, :unless)
around_update :track_update, callback_options if history_options.options[:track_update]
around_create :track_create, callback_options if history_options.options[:track_create]
around_destroy :track_destroy, callback_options if history_options.options[:track_destroy]
unless respond_to? :mongoid_history_options
class_attribute :mongoid_history_options, instance_accessor: false
end
self.mongoid_history_options = history_options
end
def history_settings(options = {})
options = Mongoid::History.default_settings.merge(options.symbolize_keys)
options = options.slice(*Mongoid::History.default_settings.keys)
options[:paranoia_field] = aliased_fields[options[:paranoia_field].to_s] || options[:paranoia_field].to_s
Mongoid::History.trackable_settings ||= {}
Mongoid::History.trackable_settings[name.to_sym] = options
end
def track_history?
Mongoid::History.enabled? && Mongoid::History.store[track_history_flag] != false
end
def dynamic_enabled?
Mongoid::Compatibility::Version.mongoid3? || (self < Mongoid::Attributes::Dynamic).present?
end
def disable_tracking(&_block)
Mongoid::History.store[track_history_flag] = false
yield
ensure
Mongoid::History.store[track_history_flag] = true
end
def track_history_flag
"mongoid_history_#{name.underscore}_trackable_enabled".to_sym
end
def tracker_class
klass = history_trackable_options[:tracker_class_name] || Mongoid::History.tracker_class_name
klass.is_a?(Class) ? klass : klass.to_s.camelize.constantize
end
end
module MyInstanceMethods
def history_tracks
@history_tracks ||= self.class.tracker_class.where(
scope: related_scope,
association_chain: association_hash
).asc(:version)
end
# undo :from => 1, :to => 5
# undo 4
# undo :last => 10
def undo(modifier = nil, options_or_version = nil)
versions = get_versions_criteria(options_or_version).to_a
versions.sort! { |v1, v2| v2.version <=> v1.version }
versions.each do |v|
undo_attr = v.undo_attr(modifier)
if Mongoid::Compatibility::Version.mongoid3? # update_attributes! not bypassing rails 3 protected attributes
assign_attributes(undo_attr, without_protection: true)
else # assign_attributes with 'without_protection' option does not work with rails 4/mongoid 4
self.attributes = undo_attr
end
end
end
# undo! :from => 1, :to => 5
# undo! 4
# undo! :last => 10
def undo!(modifier = nil, options_or_version = nil)
undo(modifier, options_or_version)
save!
end
def redo!(modifier = nil, options_or_version = nil)
versions = get_versions_criteria(options_or_version).to_a
versions.sort! { |v1, v2| v1.version <=> v2.version }
versions.each do |v|
redo_attr = v.redo_attr(modifier)
if Mongoid::Compatibility::Version.mongoid3?
assign_attributes(redo_attr, without_protection: true)
save!
else
update_attributes!(redo_attr)
end
end
end
def _get_relation(name)
send(self.class.relation_alias(name))
end
def _create_relation(name, value)
send("create_#{self.class.relation_alias(name)}!", value)
end
private
def get_versions_criteria(options_or_version)
if options_or_version.is_a? Hash
options = options_or_version
if options[:from] && options[:to]
lower = options[:from] >= options[:to] ? options[:to] : options[:from]
upper = options[:from] < options[:to] ? options[:to] : options[:from]
versions = history_tracks.where(:version.in => (lower..upper).to_a)
elsif options[:last]
versions = history_tracks.limit(options[:last])
else
raise 'Invalid options, please specify (:from / :to) keys or :last key.'
end
else
options_or_version = options_or_version.to_a if options_or_version.is_a?(Range)
version_field_name = history_trackable_options[:version_field]
version = options_or_version || attributes[version_field_name] || attributes[version_field_name.to_s]
version = [version].flatten
versions = history_tracks.where(:version.in => version)
end
versions.desc(:version)
end
def related_scope
scope = history_trackable_options[:scope]
# Use top level document if its name is specified in the scope
root_document_name = traverse_association_chain.first['name'].singularize.underscore.tr('/', '_').to_sym
if scope.is_a?(Array) && scope.include?(root_document_name)
scope = root_document_name
else
scope = _parent.collection_name.to_s.singularize.to_sym if scope.is_a?(Array)
if Mongoid::Compatibility::Version.mongoid3?
scope = metadata.inverse_class_name.tableize.singularize.to_sym if metadata.present? && scope == metadata.as
elsif Mongoid::Compatibility::Version.mongoid6_or_older?
scope = relation_metadata.inverse_class_name.tableize.singularize.to_sym if relation_metadata.present? && scope == relation_metadata.as
elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
scope = _association.inverse_class_name.tableize.singularize.to_sym if _association.present? && scope == _association.as
end
end
scope
end
def traverse_association_chain(node = self)
list = node._parent ? traverse_association_chain(node._parent) : []
list << association_hash(node)
list
end
def association_hash(node = self)
# We prefer to look up associations through the parent record because
# we're assured, through the object creation, it'll exist. Whereas we're not guaranteed
# the child to parent (embedded_in, belongs_to) relation will be defined
if node._parent
meta = node._parent.relations.values.find do |relation|
if Mongoid::Compatibility::Version.mongoid3?
relation.class_name == node.metadata.class_name.to_s && relation.name == node.metadata.name
elsif Mongoid::Compatibility::Version.mongoid6_or_older?
relation.class_name == node.relation_metadata.class_name.to_s &&
relation.name == node.relation_metadata.name
elsif Mongoid::Compatibility::Version.mongoid7_or_newer?
relation.class_name == node._association.class_name.to_s &&
relation.name == node._association.name
end
end
end
# if root node has no meta, and should use class name instead
name = meta ? meta.key.to_s : node.class.name
ActiveSupport::OrderedHash['name', name, 'id', node.id]
end
# Returns a Hash of field name to pairs of original and modified values
# for each tracked field for a given action.
#
# @param [ String | Symbol ] action The modification action (:create, :update, :destroy)
#
# @return [ Hash<String, Array<Object>> ] the pairs of original and modified
# values for each field
def modified_attributes_for_action(action)
case action.to_sym
when :destroy then modified_attributes_for_destroy
when :create then modified_attributes_for_create
else modified_attributes_for_update
end
end
def modified_attributes_for_update
@modified_attributes_for_update ||= Mongoid::History::Attributes::Update.new(self).attributes
end
def modified_attributes_for_create
@modified_attributes_for_create ||= Mongoid::History::Attributes::Create.new(self).attributes
end
def modified_attributes_for_destroy
@modified_attributes_for_destroy ||= Mongoid::History::Attributes::Destroy.new(self).attributes
end
def history_tracker_attributes(action)
return @history_tracker_attributes if @history_tracker_attributes
modifier_field = history_trackable_options[:modifier_field]
@history_tracker_attributes = {
association_chain: traverse_association_chain,
scope: related_scope
}
@history_tracker_attributes[:modifier] = send(modifier_field) if modifier_field
original, modified = transform_changes(modified_attributes_for_action(action))
@history_tracker_attributes[:original] = original
@history_tracker_attributes[:modified] = modified
@history_tracker_attributes
end
def track_create(&block)
track_history_for_action(:create, &block)
end
def track_update(&block)
track_history_for_action(:update, &block)
end
def track_destroy(&block)
track_history_for_action(:destroy, &block) unless destroyed?
end
def clear_trackable_memoization
@history_tracker_attributes = nil
@modified_attributes_for_create = nil
@modified_attributes_for_update = nil
@history_tracks = nil
end
def transform_changes(changes)
original = {}
modified = {}
changes.each_pair do |k, v|
o, m = v
original[k] = o unless o.nil?
modified[k] = m unless m.nil?
end
[original, modified]
end
def increment_current_version
current_version = (send(history_trackable_options[:version_field]) || 0) + 1
send("#{history_trackable_options[:version_field]}=", current_version)
current_version
end
protected
def track_history_for_action?(action)
track_history? && !(action.to_sym == :update && modified_attributes_for_update.blank?)
end
def track_history_for_action(action)
if track_history_for_action?(action)
current_version = increment_current_version
last_track = self.class.tracker_class.create!(history_tracker_attributes(action.to_sym).merge(version: current_version, action: action.to_s, trackable: self))
end
clear_trackable_memoization
begin
yield
rescue => e
if track_history_for_action?(action)
send("#{history_trackable_options[:version_field]}=", current_version - 1)
last_track.destroy
end
raise e
end
end
end
module RelationMethods
# Returns a relation class for the given field.
#
# @param [ String | Symbol ] field The name of the field.
#
# @return [ nil | Constant ] Class being related.
def relation_class_of(field)
meta = meta_of(field)
return meta.class_name.constantize if meta
end
# Indicates whether there is an Embedded::One relation for the given embedded field.
#
# @param [ String | Symbol ] embed The name of the embedded field.
#
# @return [ Boolean ] true if there is an Embedded::One relation for the given embedded field.
def embeds_one?(field)
relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
Mongoid::Association::Embedded::EmbedsOne::Proxy
else
Mongoid::Relations::Embedded::One
end
end
# Indicates whether there is an Embedded::Many relation for the given embedded field.
#
# @param [ String | Symbol ] field The name of the embedded field.
#
# @return [ Boolean ] true if there is an Embedded::Many relation for the given embedded field.
def embeds_many?(field)
relation_of(field) == if Mongoid::Compatibility::Version.mongoid7_or_newer?
Mongoid::Association::Embedded::EmbedsMany::Proxy
else
Mongoid::Relations::Embedded::Many
end
end
# Retrieves the database representation of an embedded field name, in case the :store_as option is used.
#
# @param [ String | Symbol ] embed The name or alias of the embedded field.
#
# @return [ String ] The database name of the embedded field.
def relation_alias(embed)
relation_aliases[embed]
end
protected
# Return the reflected metadata for a relation.
#
# @param [ String ] field The database field name for a relation.
#
# @return [ nil | Mongoid::Relations::Metadata ]
def meta_of(field)
@meta_of ||= {}
return @meta_of[field] if @meta_of.key?(field)
@meta_of[field] = reflect_on_association(relation_alias(field))
end
# Returns a relation for the given field.
#
# @param [ String | Symbol ] field The name of the field.
#
# @return [ nil | Constant ] Type of relation.
def relation_of(field)
meta = meta_of(field)
meta ? meta.relation : nil
end
# Retrieves the memoized hash of embedded aliases and their associated database representations.
#
# @return [ Hash < String, String > ] hash of embedded aliases (keys) to database representations (values)
def relation_aliases
@relation_aliases ||= relations.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
store_as = Mongoid::Compatibility::Version.mongoid7_or_newer? ? v.store_as : v[:store_as]
h[store_as || k] = k
h
end
end
end
module SingletonMethods
# Whether or not the field or embedded relation should be tracked.
#
# @param [ String | Symbol ] field_or_relation The name or alias of the field OR the name of embedded relation
# @param [ String | Symbol ] action The optional action name (:create, :update, or :destroy)
#
# @return [ Boolean ] whether or not the field or embedded relation is tracked for the given action
def tracked?(field_or_relation, action = :update)
tracked_field?(field_or_relation, action) || tracked_relation?(field_or_relation)
end
# Whether or not the field should be tracked.
#
# @param [ String | Symbol ] field The name or alias of the field
# @param [ String | Symbol ] action The optional action name (:create, :update, or :destroy)
#
# @return [ Boolean ] whether or not the field is tracked for the given action
def tracked_field?(field, action = :update)
dynamic_field?(field) || tracked_fields_for_action(action).include?(database_field_name(field))
end
# Checks if field is dynamic.
#
# @param [ String | Symbol ] field The name of the dynamic field
#
# @return [ Boolean ] whether or not the field is dynamic
def dynamic_field?(field)
dynamic_enabled? &&
!fields.keys.include?(database_field_name(field)) &&
!embedded_relations.map { |_, v| v.key }.include?(database_field_name(field))
end
def field_format(field)
field_formats[database_field_name(field)]
end
# Retrieves the list of tracked fields for a given action.
#
# @param [ String | Symbol ] action The action name (:create, :update, or :destroy)
#
# @return [ Array < String > ] the list of tracked fields for the given action
def tracked_fields_for_action(action)
case action.to_sym
when :destroy then tracked_fields + reserved_tracked_fields
else tracked_fields
end
end
# Retrieves the memoized base list of tracked fields, excluding reserved fields.
#
# @return [ Array < String > ] the base list of tracked database field names
def tracked_fields
@tracked_fields ||= history_trackable_options[:fields] + history_trackable_options[:dynamic]
end
# Retrieves the memoized list of reserved tracked fields, which are only included for certain actions.
#
# @return [ Array < String > ] the list of reserved database field names
def reserved_tracked_fields
@reserved_tracked_fields ||= begin
fields = ['_id', history_trackable_options[:version_field].to_s]
modifier_field = history_trackable_options[:modifier_field]
fields << "#{modifier_field}_id" if modifier_field
fields
end
end
def field_formats
@field_formats ||= history_trackable_options[:format]
end
# Whether or not the relation should be tracked.
#
# @param [ String | Symbol ] relation The name of the relation
#
# @return [ Boolean ] whether or not the relation is tracked
def tracked_relation?(relation)
tracked_embeds_one?(relation) || tracked_embeds_many?(relation)
end
# Whether or not the embeds_one relation should be tracked.
#
# @param [ String | Symbol ] relation The name of the embeds_one relation
#
# @return [ Boolean ] whether or not the embeds_one relation is tracked
def tracked_embeds_one?(relation)
tracked_embeds_one.include?(database_field_name(relation))
end
# Retrieves the memoized list of tracked embeds_one relations
#
# @return [ Array < String > ] the list of tracked embeds_one relations
def tracked_embeds_one
@tracked_embeds_one ||= begin
reflect_on_all_associations(:embeds_one)
.map(&:key)
.select { |rel| history_trackable_options[:relations][:embeds_one].include? rel }
end
end
def tracked_embeds_one_attributes(relation)
history_trackable_options[:relations][:embeds_one][database_field_name(relation)]
end
# Whether or not the embeds_many relation should be tracked.
#
# @param [ String | Symbol ] relation The name of the embeds_many relation
#
# @return [ Boolean ] whether or not the embeds_many relation is tracked
def tracked_embeds_many?(relation)
tracked_embeds_many.include?(database_field_name(relation))
end
# Retrieves the memoized list of tracked embeds_many relations
#
# @return [ Array < String > ] the list of tracked embeds_many relations
def tracked_embeds_many
@tracked_embeds_many ||= begin
reflect_on_all_associations(:embeds_many)
.map(&:key)
.select { |rel| history_trackable_options[:relations][:embeds_many].include? rel }
end
end
def tracked_embeds_many_attributes(relation)
history_trackable_options[:relations][:embeds_many][database_field_name(relation)]
end
def trackable_scope
collection_name.to_s.singularize.to_sym
end
def history_trackable_options
@history_trackable_options ||= mongoid_history_options.prepared
end
def clear_trackable_memoization
@reserved_tracked_fields = nil
@history_trackable_options = nil
@trackable_settings = nil
@tracked_fields = nil
@tracked_embeds_one = nil
@tracked_embeds_many = nil
@obfuscated_fields = nil
end
end
end
end
end