mongoid-history 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop_todo.yml +16 -3
  3. data/.travis.yml +23 -8
  4. data/CHANGELOG.md +20 -6
  5. data/Dangerfile +1 -0
  6. data/Gemfile +22 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +130 -18
  9. data/lib/mongoid/history.rb +32 -15
  10. data/lib/mongoid/history/attributes/base.rb +31 -0
  11. data/lib/mongoid/history/attributes/create.rb +52 -0
  12. data/lib/mongoid/history/attributes/destroy.rb +36 -0
  13. data/lib/mongoid/history/attributes/update.rb +43 -0
  14. data/lib/mongoid/history/options.rb +153 -0
  15. data/lib/mongoid/history/trackable.rb +170 -72
  16. data/lib/mongoid/history/tracker.rb +39 -23
  17. data/lib/mongoid/history/version.rb +1 -1
  18. data/mongoid-history.gemspec +0 -8
  19. data/spec/integration/embedded_in_polymorphic_spec.rb +8 -0
  20. data/spec/integration/integration_spec.rb +4 -0
  21. data/spec/integration/nested_embedded_documents_spec.rb +13 -6
  22. data/spec/integration/nested_embedded_polymorphic_documents_spec.rb +24 -24
  23. data/spec/spec_helper.rb +6 -0
  24. data/spec/unit/attributes/base_spec.rb +54 -0
  25. data/spec/unit/attributes/create_spec.rb +315 -0
  26. data/spec/unit/attributes/destroy_spec.rb +218 -0
  27. data/spec/unit/attributes/update_spec.rb +210 -0
  28. data/spec/unit/embedded_methods_spec.rb +69 -0
  29. data/spec/unit/history_spec.rb +35 -0
  30. data/spec/unit/my_instance_methods_spec.rb +485 -0
  31. data/spec/unit/options_spec.rb +294 -0
  32. data/spec/unit/singleton_methods_spec.rb +338 -0
  33. data/spec/unit/store/default_store_spec.rb +11 -0
  34. data/spec/unit/store/request_store_spec.rb +13 -0
  35. data/spec/unit/trackable_spec.rb +335 -68
  36. data/spec/unit/tracker_spec.rb +153 -0
  37. metadata +31 -102
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 200d89256cdc175333764f52ff8e87d9e15c5050
4
- data.tar.gz: 0c16bfb17523fb50f65af6b3576026d7bddaa1ea
3
+ metadata.gz: 1fd1d2a3abaccb54ecc0c83da09b16dbaa7d8210
4
+ data.tar.gz: 1126ed19a46d4542a059c2434665436043787424
5
5
  SHA512:
6
- metadata.gz: 9c56ee00ab7c9f6aacb8fa8e0816a4729b2f17c7b33abb6ccd95783d9d5e331182a2cf7707579e4dcd2868f48766777570b35a6497e54be37a34351c86e3963e
7
- data.tar.gz: 5317fa4ee38a6b554dc94d85571a6e80cf6eb2a16fd57b8cfc7ca366820b57947cdd353c2cf8ad7091086953bb7945cb925785cd6a1d78bce21512a208114384
6
+ metadata.gz: d83f26cbfe246d76d2659414b7a7feb48b1a27007cafec7bf1c4d957568b688375ce09302f0ed3a15329d31e87a4e15147212c70f3fa4796d6de783e17edfd0d
7
+ data.tar.gz: 27fce1d91542b620f197f213185b570f057508a929f05abc8b2647cfeeeef543e536850e3127d9b29e567f032bc94a2d587a72c9d8f91facec0254632540b49e
@@ -11,9 +11,12 @@ Lint/HandleExceptions:
11
11
  Exclude:
12
12
  - 'spec/unit/trackable_spec.rb'
13
13
 
14
+ Metrics/ClassLength:
15
+ Max: 103
16
+
14
17
  # Offense count: 12
15
18
  Metrics/AbcSize:
16
- Max: 51
19
+ Max: 60
17
20
 
18
21
  # Offense count: 4
19
22
  Metrics/CyclomaticComplexity:
@@ -27,12 +30,12 @@ Metrics/LineLength:
27
30
  # Offense count: 7
28
31
  # Configuration parameters: CountComments.
29
32
  Metrics/MethodLength:
30
- Max: 32
33
+ Max: 33
31
34
 
32
35
  # Offense count: 2
33
36
  # Configuration parameters: CountComments.
34
37
  Metrics/ModuleLength:
35
- Max: 168
38
+ Max: 223
36
39
 
37
40
  # Offense count: 4
38
41
  Metrics/PerceivedComplexity:
@@ -42,9 +45,14 @@ Metrics/PerceivedComplexity:
42
45
  Style/Documentation:
43
46
  Exclude:
44
47
  - 'lib/mongoid/history.rb'
48
+ - 'lib/mongoid/history/options.rb'
45
49
  - 'lib/mongoid/history/trackable.rb'
46
50
  - 'lib/mongoid/history/tracker.rb'
47
51
  - 'lib/mongoid/history/version.rb'
52
+ - 'lib/mongoid/history/attributes/base.rb'
53
+ - 'lib/mongoid/history/attributes/create.rb'
54
+ - 'lib/mongoid/history/attributes/destroy.rb'
55
+ - 'lib/mongoid/history/attributes/update.rb'
48
56
  - 'spec/integration/embedded_in_polymorphic_spec.rb'
49
57
  - 'spec/integration/integration_spec.rb'
50
58
  - 'spec/integration/multi_relation_spec.rb'
@@ -52,8 +60,13 @@ Style/Documentation:
52
60
  - 'spec/integration/nested_embedded_polymorphic_documents_spec.rb'
53
61
  - 'spec/integration/subclasses_spec.rb'
54
62
  - 'spec/support/mongoid_history.rb'
63
+ - 'spec/unit/options_spec.rb'
64
+ - 'spec/unit/singleton_methods_spec.rb'
55
65
  - 'spec/unit/trackable_spec.rb'
56
66
  - 'spec/unit/tracker_spec.rb'
67
+ - 'spec/unit/attributes/create_spec.rb'
68
+ - 'spec/unit/attributes/destroy_spec.rb'
69
+ - 'spec/unit/attributes/update_spec.rb'
57
70
 
58
71
  # Offense count: 6
59
72
  Style/EachWithObject:
@@ -1,4 +1,3 @@
1
- services: mongodb
2
1
 
3
2
  sudo: false
4
3
 
@@ -6,15 +5,31 @@ language: ruby
6
5
 
7
6
  cache: bundler
8
7
 
9
- rvm:
10
- - 2.2
11
- - 2.1.1
12
- - 2.0.0
13
- - 1.9.3
14
- - rbx-2.2.10
15
- - jruby-19mode
8
+ services: mongodb
16
9
 
17
10
  env:
18
11
  - MONGOID_VERSION=3
19
12
  - MONGOID_VERSION=4
20
13
  - MONGOID_VERSION=5
14
+ - MONGOID_VERSION=6
15
+ - MONGOID_VERSION=HEAD
16
+
17
+ rvm:
18
+ - 2.3.1
19
+
20
+ before_install:
21
+ - gem update bundler
22
+
23
+ before_script:
24
+ - bundle exec danger
25
+
26
+ addons:
27
+ apt:
28
+ sources:
29
+ - mongodb-3.2-precise
30
+ packages:
31
+ - mongodb-org-server
32
+
33
+ matrix:
34
+ allow_failures:
35
+ - env: MONGOID_VERSION=HEAD
@@ -1,8 +1,22 @@
1
+ 0.6.0 (2016/09/13)
2
+ ------------------
3
+
4
+ * [#2](https://github.com/mongoid/mongoid-history/pull/2): Froked into the [mongoid](https://github.com/mongoid) organization - [@dblock](https://github.com/dblock).
5
+ * [#1](https://github.com/mongoid/mongoid-history/pull/1): Added Danger, PR linter - [@dblock](https://github.com/dblock).
6
+ * [#166](https://github.com/aq1018/mongoid-history/pull/166): Hash fields should default to an empty Hash - [@johnnyshields](https://github.com/johnnyshields).
7
+ * [#162](https://github.com/aq1018/mongoid-history/pull/162): Do not consider embedded relations as dynamic fields - [@JagdeepSingh](https://github.com/JagdeepSingh).
8
+ * [#144](https://github.com/aq1018/mongoid-history/pull/158): Can modify history tracker insertion on object creation - [@sivagollapalli](https://github.com/sivagollapalli).
9
+ * [#155](https://github.com/aq1018/mongoid-history/pull/155): Add support to whitelist the attributes for tracked embeds_one and embeds_many relations - [@JagdeepSingh](https://github.com/JagdeepSingh).
10
+ * [#154](https://github.com/aq1018/mongoid-history/pull/154): Prevent soft-deleted embedded documents from tracking - [@JagdeepSingh](https://github.com/JagdeepSingh).
11
+ * [#151](https://github.com/aq1018/mongoid-history/pull/151): Added ability to customize tracker class for each trackable; multiple trackers across the app are now possible - [@JagdeepSingh](https://github.com/JagdeepSingh).
12
+ * [#151](https://github.com/aq1018/mongoid-history/pull/151): Added automatic support for `request_store` gem as drop-in replacement for `Thread.current` - [@JagdeepSingh](https://github.com/JagdeepSingh).
13
+ * [#150](https://github.com/aq1018/mongoid-history/pull/150): Added support for keeping embedded objects audit history in parent itself - [@JagdeepSingh](https://github.com/JagdeepSingh).
14
+
1
15
  0.5.0 (2015/09/18)
2
16
  ------------------
3
17
 
4
18
  * [#143](https://github.com/aq1018/mongoid-history/pull/143): Added support for Mongoid 5 - [@dblock](https://github.com/dblock).
5
- * [#133](https://github.com/aq1018/mongoid-history/pull/133): Added dynamic attributes tracking (Mongoid::Attributes::Dynamic): [@minisai](https://github.com/minisai).
19
+ * [#133](https://github.com/aq1018/mongoid-history/pull/133): Added dynamic attributes tracking (Mongoid::Attributes::Dynamic) - [@minisai](https://github.com/minisai).
6
20
  * [#142](https://github.com/aq1018/mongoid-history/pull/142): Allow non-database fields to be specified in conjunction with a custom changes method - [@kayakyakr](https://github.com/kayakyakr).
7
21
 
8
22
  0.4.7 (2015/04/06)
@@ -26,13 +40,13 @@
26
40
  0.4.3 (2014/07/10)
27
41
  ------------------
28
42
 
29
- * [#110](https://github.com/aq1018/mongoid-history/pull/110): Fixed scope reference on history tracks criteria - [@adbeelitamar](https://github.com/adbeelitamar).
43
+ * [#110](https://github.com/aq1018/mongoid-history/pull/110): Fixed scope reference on history tracks criteria - [@adbeelitamar](https://github.com/adbeelitamar).
30
44
 
31
45
  0.4.2 (2014/07/01)
32
46
  ------------------
33
47
 
34
- * [#106](https://github.com/aq1018/mongoid-history/pull/106): Added support for polymorphic relationship `scope` - [@adbeelitamar](https://github.com/adbeelitamar).
35
- * [#106](https://github.com/aq1018/mongoid-history/pull/106): Enabled specifying an array of relationships in `scope` - [@adbeelitamar](https://github.com/adbeelitamar).
48
+ * [#106](https://github.com/aq1018/mongoid-history/pull/106): Added support for polymorphic relationship `scope` - [@adbeelitamar](https://github.com/adbeelitamar).
49
+ * [#106](https://github.com/aq1018/mongoid-history/pull/106): Enabled specifying an array of relationships in `scope` - [@adbeelitamar](https://github.com/adbeelitamar).
36
50
  * [#83](https://github.com/aq1018/mongoid-history/pull/83): Added support for Mongoid 4.x, which removed `attr_accessible` in favor of protected attributes - [@dblock](https://github.com/dblock).
37
51
  * [#103](https://github.com/aq1018/mongoid-history/pull/103): Fixed compatibility with models using `default_scope` - [@mrjlynch](https://github.com/mrjlynch).
38
52
 
@@ -53,7 +67,7 @@
53
67
 
54
68
  * Added `Mongoid::History.disable` and `Mongoid::History.enabled?` methods for global tracking disablement - [@johnnyshields](https://github.com/johnnyshields).
55
69
  * Added `:changes_method` that optionally overrides which method to call to collect changes - [@joelnordel](https://github.com/joelnordell).
56
- * [API Change] The `:destroy` action now stores trackers in the format `original=value, modified=nil` (previously it was the reverse): [@johnnyshields](https://github.com/johnnyshields).
70
+ * The `:destroy` action now stores trackers in the format `original=value, modified=nil` (previously it was the reverse) - [@johnnyshields](https://github.com/johnnyshields).
57
71
  * Support for polymorphic embedded classes - [@tstepp](https://github.com/tstepp).
58
72
  * Support for Mongoid field aliases, e.g. `field :n, as: :name` - [@johnnyshields](https://github.com/johnnyshields).
59
73
  * Support for Mongoid embedded aliases, e.g. `embeds_many :comments, store_as: :coms` - [@johnnyshields](https://github.com/johnnyshields).
@@ -102,7 +116,7 @@
102
116
 
103
117
  * [#23](https://github.com/aq1018/mongoid-history/pull/34): Updated `Trackable::association_hash` to write through parent - [@tcopple](https://github.com/tcopple).
104
118
  * Fix: `Trackable::association_hash` nil meta value call - [@tcopple](https://github.com/tcopple).
105
- * [#27](https://github.com/aq1018/mongoid-history/pull/27): Added support for re-creation of destroyed embedded documents - [@erlingwl](https://github.com/erlingwl)
119
+ * [#27](https://github.com/aq1018/mongoid-history/pull/27): Added support for re-creation of destroyed embedded documents - [@erlingwl](https://github.com/erlingwl).
106
120
 
107
121
  0.1.7 (2011/12/09)
108
122
  ------------------
@@ -0,0 +1 @@
1
+ danger.import_dangerfile(gem: 'mongoid-danger')
data/Gemfile CHANGED
@@ -2,13 +2,34 @@ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
4
4
 
5
- case version = ENV['MONGOID_VERSION'] || '~> 5.0'
5
+ case version = ENV['MONGOID_VERSION'] || '~> 6.0.0.rc0'
6
+ when 'HEAD'
7
+ gem 'mongoid', github: 'mongodb/mongoid'
8
+ when /6/
9
+ gem 'mongoid', '~> 6.0.0.rc0'
6
10
  when /5/
7
11
  gem 'mongoid', '~> 5.0'
12
+ gem 'mongoid-observers', '~> 0.2.0'
8
13
  when /4/
9
14
  gem 'mongoid', '~> 4.0'
15
+ gem 'mongoid-observers', '~> 0.2.0'
10
16
  when /3/
11
17
  gem 'mongoid', '~> 3.1'
12
18
  else
13
19
  gem 'mongoid', version
14
20
  end
21
+
22
+ group :development, :test do
23
+ gem 'rake'
24
+ gem 'bundler'
25
+ end
26
+
27
+ group :test do
28
+ gem 'mongoid-danger', '~> 0.1.0', require: false
29
+ gem 'rspec', '~> 3.1'
30
+ gem 'rubocop', '0.34.1'
31
+ gem 'yard'
32
+ gem 'gem-release'
33
+ gem 'coveralls'
34
+ gem 'request_store'
35
+ end
@@ -1,4 +1,4 @@
1
- Copyright (c) 2011 Aaron Qian
1
+ Copyright (c) 2011-2016 Aaron Qian & Contributors
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  mongoid-history
2
2
  ===============
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/mongoid-history.png)](http://badge.fury.io/rb/mongoid-history)
5
- [![Build Status](https://secure.travis-ci.org/aq1018/mongoid-history.png?branch=master)](http://travis-ci.org/aq1018/mongoid-history)
6
- [![Dependency Status](https://gemnasium.com/aq1018/mongoid-history.png)](https://gemnasium.com/aq1018/mongoid-history)
7
- [![Code Climate](https://codeclimate.com/github/aq1018/mongoid-history.png)](https://codeclimate.com/github/aq1018/mongoid-history)
8
- [![Coverage Status](https://coveralls.io/repos/aq1018/mongoid-history/badge.png?branch=coveralls)](https://coveralls.io/r/aq1018/mongoid-history?branch=coveralls)
4
+ [![Gem Version](https://badge.fury.io/rb/mongoid-history.svg)](http://badge.fury.io/rb/mongoid-history)
5
+ [![Build Status](https://secure.travis-ci.org/mongoid/mongoid-history.svg?branch=master)](http://travis-ci.org/mongoid/mongoid-history)
6
+ [![Dependency Status](https://gemnasium.com/mongoid/mongoid-history.svg)](https://gemnasium.com/mongoid/mongoid-history)
7
+ [![Code Climate](https://codeclimate.com/github/mongoid/mongoid-history.svg)](https://codeclimate.com/github/mongoid/mongoid-history)
8
+ [![Coverage Status](https://coveralls.io/repos/mongoid/mongoid-history/badge.svg)](https://coveralls.io/r/mongoid/mongoid-history?branch=coveralls)
9
9
 
10
10
  Mongoid-history tracks historical changes for any document, including embedded ones. It achieves this by storing all history tracks in a single collection that you define. Embedded documents are referenced by storing an association path, which is an array of `document_name` and `document_id` fields starting from the top most parent document and down to the embedded document that should track history.
11
11
 
@@ -34,14 +34,14 @@ class HistoryTracker
34
34
  end
35
35
  ```
36
36
 
37
- **Set tracker class name**
37
+ **Set default tracker class name (Optional)**
38
38
 
39
- Manually set the tracker class name to make sure your tracker can be found and loaded properly. You can skip this step if you manually require your tracker before using any trackables.
40
-
41
- The following example sets the tracker class name using a Rails initializer.
39
+ Mongoid::History will use the first loaded class to include Mongoid::History::Tracker as the
40
+ default history tracker. If you are using multiple Tracker classes and would like to set
41
+ a global default you may do so in a Rails initializer:
42
42
 
43
43
  ```ruby
44
- # config/initializers/mongoid-history.rb
44
+ # config/initializers/mongoid_history.rb
45
45
  # initializer for mongoid-history
46
46
  # assuming HistoryTracker is your tracker class
47
47
  Mongoid::History.tracker_class_name = :history_tracker
@@ -156,7 +156,78 @@ Mongoid::History.disable do
156
156
  end
157
157
  ```
158
158
 
159
- **Retrieving the list of tracked fields**
159
+ **Include embedded objects attributes in parent audit**
160
+
161
+ Modify above `Post` and `Comment` classes as below:
162
+
163
+ ```ruby
164
+ class Post
165
+ include Mongoid::Document
166
+ include Mongoid::Timestamps
167
+ include Mongoid::History::Trackable
168
+
169
+ field :title
170
+ field :body
171
+ field :rating
172
+ embeds_many :comments
173
+
174
+ track_history :on => [:title, :body, :comments],
175
+ :modifier_field => :modifier,
176
+ :modifier_field_inverse_of => :nil,
177
+ :version_field => :version,
178
+ :track_create => true, # track create on Post
179
+ :track_update => true,
180
+ :track_destroy => false
181
+ end
182
+
183
+ class Comment
184
+ include Mongoid::Document
185
+ include Mongoid::Timestamps
186
+
187
+ field :title
188
+ field :body
189
+ embedded_in :post, :inverse_of => :comments
190
+ end
191
+
192
+ user = User.create(:name => "Aaron")
193
+ post = Post.create(:title => "Test", :body => "Post", :modifier => user)
194
+ comment = post.comments.build(:title => "test", :body => "comment", :modifier => user)
195
+ post.save
196
+ post.history_tracks.count # should be 1
197
+
198
+ comment.respond_to?(:history_tracks) # should be false
199
+
200
+ track = post.history_tracks.first
201
+ track.original # {}
202
+ track.modified # { "title" => "Test", "body" => "Post", "comments" => [{ "_id" => "575fa9e667d827e5ed00000d", "title" => "test", "body" => "comment" }], ... }
203
+ ```
204
+
205
+ **Whitelist the tracked attributes of embedded relations**
206
+
207
+ If you don't want to track all the attributes of embedded relations in parent audit history, you can whitelist the attributes as below:
208
+
209
+ ```ruby
210
+ class Book
211
+ include Mongoid::Document
212
+ ...
213
+ embeds_many :pages
214
+ track_history :on => { :pages => [:title, :content] }
215
+ end
216
+
217
+ class Page
218
+ include Mongoid::Document
219
+ ...
220
+ field :number
221
+ field :title
222
+ field :subtitle
223
+ field :content
224
+ embedded_in :book
225
+ end
226
+ ```
227
+
228
+ It will now track only `_id` (Mandatory), `title` and `content` attributes for `pages` relation.
229
+
230
+ **Retrieving the list of tracked static and dynamic fields**
160
231
 
161
232
  ```ruby
162
233
  class Book
@@ -172,6 +243,42 @@ Book.tracked_field?(:title) #=> true
172
243
  Book.tracked_field?(:author) #=> false
173
244
  ```
174
245
 
246
+ **Retrieving the list of tracked relations**
247
+
248
+ ```ruby
249
+ class Book
250
+ ...
251
+ track_history :on => [:pages]
252
+ end
253
+
254
+ Book.tracked_relation?(:pages) #=> true
255
+ Book.tracked_embeds_many #=> ["pages"]
256
+ Book.tracked_embeds_many?(:pages) #=> true
257
+ ```
258
+
259
+ **Skip soft-deleted embedded objects with nested tracking**
260
+
261
+ Default paranoia field is `deleted_at`. You can use custom field for each class as below:
262
+
263
+ ```ruby
264
+ class Book
265
+ include Mongoid::Document
266
+ include Mongoid::History::Trackable
267
+ embeds_many :pages
268
+ track_history on: :pages
269
+ end
270
+
271
+ class Page
272
+ include Mongoid::Document
273
+ include Mongoid::History::Trackable
274
+ ...
275
+ embedded_in :book
276
+ history_settings paranoia_field: :removed_at
277
+ end
278
+ ```
279
+
280
+ This will skip the `page` documents with `removed_at` set to a non-blank value from nested tracking
281
+
175
282
  **Displaying history trackers as an audit trail**
176
283
 
177
284
  In your Controller:
@@ -336,7 +443,16 @@ class Baz
336
443
  end
337
444
  ```
338
445
 
339
- For more examples, check out [spec/integration/integration_spec.rb](https://github.com/aq1018/mongoid-history/blob/master/spec/integration/integration_spec.rb).
446
+ For more examples, check out [spec/integration/integration_spec.rb](spec/integration/integration_spec.rb).
447
+
448
+
449
+ **Thread Safety**
450
+
451
+ Mongoid::History stores the tracking enable/disable flag in `Thread.current`.
452
+ If the [RequestStore](https://github.com/steveklabnik/request_store) gem is installed, Mongoid::History
453
+ will automatically store variables in the `RequestStore.store` instead. RequestStore is recommended
454
+ for threaded web servers like Thin or Puma.
455
+
340
456
 
341
457
  Contributing to mongoid-history
342
458
  -------------------------------
@@ -346,10 +462,6 @@ You're encouraged to contribute to this library. See [CONTRIBUTING](CONTRIBUTING
346
462
  Copyright
347
463
  ---------
348
464
 
349
- Copyright (c) 2011-2012 Aaron Qian. MIT License.
350
-
351
- See [LICENSE.txt](https://github.com/aq1018/mongoid-history/blob/master/LICENSE.txt) for further details.
352
-
353
-
465
+ Copyright (c) 2011-2016 Aaron Qian and Contributors.
354
466
 
355
- [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/aq1018/mongoid-history/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
467
+ MIT License. See [LICENSE.txt](LICENSE.txt) for further details.
@@ -1,5 +1,10 @@
1
1
  require 'easy_diff'
2
2
  require 'mongoid/compatibility'
3
+ require 'mongoid/history/attributes/base'
4
+ require 'mongoid/history/attributes/create'
5
+ require 'mongoid/history/attributes/update'
6
+ require 'mongoid/history/attributes/destroy'
7
+ require 'mongoid/history/options'
3
8
  require 'mongoid/history/version'
4
9
  require 'mongoid/history/tracker'
5
10
  require 'mongoid/history/trackable'
@@ -8,28 +13,40 @@ module Mongoid
8
13
  module History
9
14
  GLOBAL_TRACK_HISTORY_FLAG = 'mongoid_history_trackable_enabled'
10
15
 
11
- mattr_accessor :tracker_class_name
12
- mattr_accessor :trackable_class_options
13
- mattr_accessor :modifier_class_name
14
- mattr_accessor :current_user_method
16
+ class << self
17
+ attr_accessor :tracker_class_name
18
+ attr_accessor :trackable_class_options
19
+ attr_accessor :trackable_settings
20
+ attr_accessor :modifier_class_name
21
+ attr_accessor :current_user_method
15
22
 
16
- def self.tracker_class
17
- @tracker_class ||= tracker_class_name.to_s.classify.constantize
18
- end
23
+ def disable(&_block)
24
+ store[GLOBAL_TRACK_HISTORY_FLAG] = false
25
+ yield
26
+ ensure
27
+ store[GLOBAL_TRACK_HISTORY_FLAG] = true
28
+ end
19
29
 
20
- def self.disable(&_block)
21
- Thread.current[GLOBAL_TRACK_HISTORY_FLAG] = false
22
- yield
23
- ensure
24
- Thread.current[GLOBAL_TRACK_HISTORY_FLAG] = true
25
- end
30
+ def enabled?
31
+ store[GLOBAL_TRACK_HISTORY_FLAG] != false
32
+ end
33
+
34
+ def store
35
+ defined?(RequestStore) ? RequestStore.store : Thread.current
36
+ end
37
+
38
+ def default_settings
39
+ @default_settings ||= { paranoia_field: 'deleted_at' }
40
+ end
26
41
 
27
- def self.enabled?
28
- Thread.current[GLOBAL_TRACK_HISTORY_FLAG] != false
42
+ def trackable_class_settings(trackable_class)
43
+ trackable_settings[trackable_class.name.to_sym] || default_settings
44
+ end
29
45
  end
30
46
  end
31
47
  end
32
48
 
33
49
  Mongoid::History.modifier_class_name = 'User'
34
50
  Mongoid::History.trackable_class_options = {}
51
+ Mongoid::History.trackable_settings = {}
35
52
  Mongoid::History.current_user_method ||= :current_user