mongoid-history 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.document +5 -5
- data/.rspec +1 -1
- data/.rubocop.yml +75 -0
- data/.travis.yml +7 -5
- data/CHANGELOG.md +94 -76
- data/Gemfile +16 -12
- data/LICENSE.txt +20 -20
- data/README.md +309 -307
- data/Rakefile +37 -37
- data/VERSION +1 -1
- data/lib/mongoid/history/trackable.rb +337 -338
- data/lib/mongoid/history/tracker.rb +215 -220
- data/lib/mongoid/history.rb +25 -27
- data/lib/mongoid-history.rb +9 -10
- data/mongoid-history.gemspec +80 -68
- data/spec/integration/integration_spec.rb +788 -754
- data/spec/integration/multi_relation_spec.rb +53 -50
- data/spec/integration/nested_embedded_documents_spec.rb +77 -77
- data/spec/integration/subclasses_spec.rb +29 -0
- data/spec/spec_helper.rb +10 -11
- data/spec/support/mongoid.rb +11 -15
- data/spec/support/mongoid_history.rb +13 -15
- data/spec/trackable_spec.rb +240 -240
- data/spec/tracker_spec.rb +10 -10
- metadata +65 -30
- data/lib/mongoid/history/sweeper.rb +0 -44
@@ -1,220 +1,215 @@
|
|
1
|
-
module Mongoid::History
|
2
|
-
module Tracker
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
include Mongoid::Document
|
7
|
-
include Mongoid::Timestamps
|
8
|
-
attr_writer :trackable
|
9
|
-
|
10
|
-
field :association_chain, :
|
11
|
-
field :modified, :
|
12
|
-
field :original, :
|
13
|
-
field :version, :
|
14
|
-
field :action, :
|
15
|
-
field :scope, :
|
16
|
-
belongs_to :modifier, :
|
17
|
-
|
18
|
-
index(:
|
19
|
-
index(:
|
20
|
-
|
21
|
-
Mongoid::History.tracker_class_name =
|
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
|
-
undo_hash
|
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
|
-
h[:
|
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
|
-
hash
|
217
|
-
end
|
218
|
-
|
219
|
-
end
|
220
|
-
end
|
1
|
+
module Mongoid::History
|
2
|
+
module Tracker
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::Timestamps
|
8
|
+
attr_writer :trackable
|
9
|
+
|
10
|
+
field :association_chain, type: Array, default: []
|
11
|
+
field :modified, type: Hash
|
12
|
+
field :original, type: Hash
|
13
|
+
field :version, type: Integer
|
14
|
+
field :action, type: String
|
15
|
+
field :scope, type: String
|
16
|
+
belongs_to :modifier, class_name: Mongoid::History.modifier_class_name
|
17
|
+
|
18
|
+
index(scope: 1)
|
19
|
+
index(association_chain: 1)
|
20
|
+
|
21
|
+
Mongoid::History.tracker_class_name = name.tableize.singularize.to_sym
|
22
|
+
end
|
23
|
+
|
24
|
+
def undo!(modifier = nil)
|
25
|
+
if action.to_sym == :destroy
|
26
|
+
re_create
|
27
|
+
elsif action.to_sym == :create
|
28
|
+
re_destroy
|
29
|
+
else
|
30
|
+
trackable.update_attributes!(undo_attr(modifier), without_protection: true)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def redo!(modifier = nil)
|
35
|
+
if action.to_sym == :destroy
|
36
|
+
re_destroy
|
37
|
+
elsif action.to_sym == :create
|
38
|
+
re_create
|
39
|
+
else
|
40
|
+
trackable.update_attributes!(redo_attr(modifier), without_protection: true)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def undo_attr(modifier)
|
45
|
+
undo_hash = affected.easy_unmerge(modified)
|
46
|
+
undo_hash.easy_merge!(original)
|
47
|
+
modifier_field = trackable.history_trackable_options[:modifier_field]
|
48
|
+
undo_hash[modifier_field] = modifier
|
49
|
+
(modified.keys - undo_hash.keys).each do |k|
|
50
|
+
undo_hash[k] = nil
|
51
|
+
end
|
52
|
+
localize_keys(undo_hash)
|
53
|
+
end
|
54
|
+
|
55
|
+
def redo_attr(modifier)
|
56
|
+
redo_hash = affected.easy_unmerge(original)
|
57
|
+
redo_hash.easy_merge!(modified)
|
58
|
+
modifier_field = trackable.history_trackable_options[:modifier_field]
|
59
|
+
redo_hash[modifier_field] = modifier
|
60
|
+
localize_keys(redo_hash)
|
61
|
+
end
|
62
|
+
|
63
|
+
def trackable_root
|
64
|
+
@trackable_root ||= trackable_parents_and_trackable.first
|
65
|
+
end
|
66
|
+
|
67
|
+
def trackable
|
68
|
+
@trackable ||= trackable_parents_and_trackable.last
|
69
|
+
end
|
70
|
+
|
71
|
+
def trackable_parents
|
72
|
+
@trackable_parents ||= trackable_parents_and_trackable[0, -1]
|
73
|
+
end
|
74
|
+
|
75
|
+
def trackable_parent
|
76
|
+
@trackable_parent ||= trackable_parents_and_trackable[-2]
|
77
|
+
end
|
78
|
+
|
79
|
+
# Outputs a :from, :to hash for each affected field. Intentionally excludes fields
|
80
|
+
# which are not tracked, even if there are tracked values for such fields
|
81
|
+
# present in the database.
|
82
|
+
#
|
83
|
+
# @return [ HashWithIndifferentAccess ] a change set in the format:
|
84
|
+
# { field_1: {to: new_val}, field_2: {from: old_val, to: new_val} }
|
85
|
+
def tracked_changes
|
86
|
+
@tracked_changes ||= (modified.keys | original.keys).inject(HashWithIndifferentAccess.new) do |h, k|
|
87
|
+
h[k] = { from: original[k], to: modified[k] }.delete_if { |_, vv| vv.nil? }
|
88
|
+
h
|
89
|
+
end.delete_if { |k, v| v.blank? || !trackable_parent_class.tracked_field?(k) }
|
90
|
+
end
|
91
|
+
|
92
|
+
# Outputs summary of edit actions performed: :add, :modify, :remove, or :array.
|
93
|
+
# Does deep comparison of arrays. Useful for creating human-readable representations
|
94
|
+
# of the history tracker. Considers changing a value to 'blank' to be a removal.
|
95
|
+
#
|
96
|
+
# @return [ HashWithIndifferentAccess ] a change set in the format:
|
97
|
+
# { add: { field_1: new_val, ... },
|
98
|
+
# modify: { field_2: {from: old_val, to: new_val}, ... },
|
99
|
+
# remove: { field_3: old_val },
|
100
|
+
# array: { field_4: {add: ['foo', 'bar'], remove: ['baz']} } }
|
101
|
+
def tracked_edits
|
102
|
+
@tracked_edits ||= tracked_changes.inject(HashWithIndifferentAccess.new) do |h, (k, v)|
|
103
|
+
unless v[:from].blank? && v[:to].blank?
|
104
|
+
if v[:from].blank?
|
105
|
+
h[:add] ||= {}
|
106
|
+
h[:add][k] = v[:to]
|
107
|
+
elsif v[:to].blank?
|
108
|
+
h[:remove] ||= {}
|
109
|
+
h[:remove][k] = v[:from]
|
110
|
+
else
|
111
|
+
if v[:from].is_a?(Array) && v[:to].is_a?(Array)
|
112
|
+
h[:array] ||= {}
|
113
|
+
old_values = v[:from] - v[:to]
|
114
|
+
new_values = v[:to] - v[:from]
|
115
|
+
h[:array][k] = { add: new_values, remove: old_values }.delete_if { |_, vv| vv.blank? }
|
116
|
+
else
|
117
|
+
h[:modify] ||= {}
|
118
|
+
h[:modify][k] = v
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
h
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Similar to #tracked_changes, but contains only a single value for each
|
127
|
+
# affected field:
|
128
|
+
# - :create and :update return the modified values
|
129
|
+
# - :destroy returns original values
|
130
|
+
# Included for legacy compatibility.
|
131
|
+
#
|
132
|
+
# @deprecated
|
133
|
+
#
|
134
|
+
# @return [ HashWithIndifferentAccess ] a change set in the format:
|
135
|
+
# { field_1: value, field_2: value }
|
136
|
+
def affected
|
137
|
+
target = action.to_sym == :destroy ? :from : :to
|
138
|
+
@affected ||= tracked_changes.inject(HashWithIndifferentAccess.new) { |h, (k, v)|
|
139
|
+
h[k] = v[target]
|
140
|
+
h
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
# Returns the class of the trackable, irrespective of whether the trackable object
|
145
|
+
# has been destroyed.
|
146
|
+
#
|
147
|
+
# @return [ Class ] the class of the trackable
|
148
|
+
def trackable_parent_class
|
149
|
+
association_chain.first["name"].constantize
|
150
|
+
end
|
151
|
+
|
152
|
+
private
|
153
|
+
|
154
|
+
def re_create
|
155
|
+
association_chain.length > 1 ? create_on_parent : create_standalone
|
156
|
+
end
|
157
|
+
|
158
|
+
def re_destroy
|
159
|
+
trackable.destroy
|
160
|
+
end
|
161
|
+
|
162
|
+
def create_standalone
|
163
|
+
restored = trackable_parent_class.new(localize_keys(original))
|
164
|
+
restored.id = original["_id"]
|
165
|
+
restored.save!
|
166
|
+
end
|
167
|
+
|
168
|
+
def create_on_parent
|
169
|
+
name = association_chain.last["name"]
|
170
|
+
if trackable_parent.class.embeds_one?(name)
|
171
|
+
trackable_parent.create_embedded(name, localize_keys(original))
|
172
|
+
elsif trackable_parent.class.embeds_many?(name)
|
173
|
+
trackable_parent.get_embedded(name).create!(localize_keys(original))
|
174
|
+
else
|
175
|
+
raise "This should never happen. Please report bug!"
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def trackable_parents_and_trackable
|
180
|
+
@trackable_parents_and_trackable ||= traverse_association_chain
|
181
|
+
end
|
182
|
+
|
183
|
+
def traverse_association_chain
|
184
|
+
chain = association_chain.dup
|
185
|
+
doc = nil
|
186
|
+
documents = []
|
187
|
+
loop do
|
188
|
+
node = chain.shift
|
189
|
+
name = node['name']
|
190
|
+
doc = if doc.nil?
|
191
|
+
# root association. First element of the association chain
|
192
|
+
klass = name.classify.constantize
|
193
|
+
klass.where(_id: node['id']).first
|
194
|
+
elsif doc.class.embeds_one?(name)
|
195
|
+
doc.get_embedded(name)
|
196
|
+
elsif doc.class.embeds_many?(name)
|
197
|
+
doc.get_embedded(name).where(_id: node['id']).first
|
198
|
+
else
|
199
|
+
raise "This should never happen. Please report bug."
|
200
|
+
end
|
201
|
+
documents << doc
|
202
|
+
break if chain.empty?
|
203
|
+
end
|
204
|
+
documents
|
205
|
+
end
|
206
|
+
|
207
|
+
def localize_keys(hash)
|
208
|
+
klass = association_chain.first["name"].constantize
|
209
|
+
klass.localized_fields.keys.each do |name|
|
210
|
+
hash["#{name}_translations"] = hash.delete(name) if hash[name].present?
|
211
|
+
end if klass.respond_to?(:localized_fields)
|
212
|
+
hash
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
data/lib/mongoid/history.rb
CHANGED
@@ -1,27 +1,25 @@
|
|
1
|
-
module Mongoid
|
2
|
-
module History
|
3
|
-
GLOBAL_TRACK_HISTORY_FLAG = "mongoid_history_trackable_enabled"
|
4
|
-
|
5
|
-
mattr_accessor :tracker_class_name
|
6
|
-
mattr_accessor :trackable_class_options
|
7
|
-
mattr_accessor :modifier_class_name
|
8
|
-
mattr_accessor :current_user_method
|
9
|
-
|
10
|
-
def self.tracker_class
|
11
|
-
@tracker_class ||= tracker_class_name.to_s.classify.constantize
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.disable(&block)
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
end
|
1
|
+
module Mongoid
|
2
|
+
module History
|
3
|
+
GLOBAL_TRACK_HISTORY_FLAG = "mongoid_history_trackable_enabled"
|
4
|
+
|
5
|
+
mattr_accessor :tracker_class_name
|
6
|
+
mattr_accessor :trackable_class_options
|
7
|
+
mattr_accessor :modifier_class_name
|
8
|
+
mattr_accessor :current_user_method
|
9
|
+
|
10
|
+
def self.tracker_class
|
11
|
+
@tracker_class ||= tracker_class_name.to_s.classify.constantize
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.disable(&block)
|
15
|
+
Thread.current[GLOBAL_TRACK_HISTORY_FLAG] = false
|
16
|
+
yield
|
17
|
+
ensure
|
18
|
+
Thread.current[GLOBAL_TRACK_HISTORY_FLAG] = true
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.enabled?
|
22
|
+
Thread.current[GLOBAL_TRACK_HISTORY_FLAG] != false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/mongoid-history.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'easy_diff'
|
2
|
-
|
3
|
-
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history')
|
4
|
-
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/tracker')
|
5
|
-
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/trackable')
|
6
|
-
|
7
|
-
|
8
|
-
Mongoid::History.
|
9
|
-
Mongoid::History.
|
10
|
-
Mongoid::History.current_user_method ||= :current_user
|
1
|
+
require 'easy_diff'
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history')
|
4
|
+
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/tracker')
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + '/mongoid/history/trackable')
|
6
|
+
|
7
|
+
Mongoid::History.modifier_class_name = "User"
|
8
|
+
Mongoid::History.trackable_class_options = {}
|
9
|
+
Mongoid::History.current_user_method ||= :current_user
|