mongo_trails 17.0.0 → 17.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47992865a22a08835378021027077b45f22379edf107657c58ceef30a7434af8
4
- data.tar.gz: c6f4f9b31d6ea8cb9aee967a82eacb9fe25b65b8dcec66bb106825a705ed5c13
3
+ metadata.gz: 943d508b66d94b893dc1a553eb9f974b17484294750410f3b0b986073c4de415
4
+ data.tar.gz: 784ec37eed06ee8ffc0674d2f82a9a59d07892bd652a4006ac51b702b7b6e02e
5
5
  SHA512:
6
- metadata.gz: eed6a890f004082f77f1cff771e27e0dbf5644d658e396081eea482198eaefbbecaed03eebe79c3f9ded6bc13df2d5c9d88dc117cd31c6beb4fec261351b71de
7
- data.tar.gz: edad164d6a99840e7ac132cbf6d4497a8ff116dd131b2d02e1a1f1f82102b187978b993cef5974962ecbd0fbb6b8abd9602c134a10ab092e5e097126dee6e02b
6
+ metadata.gz: d56c0b823e24c0b18da5dedbbffb79fb1ecd9dc86fdc47c5455353f6a9e50ad242e346aa40d1d1c628985c93ed00a4f13e0947720487bfe4dd13a32b56c3d814
7
+ data.tar.gz: b6846cf53080d5f5d11c22bc6bfdbf25904a676a019b64f78311c8d44f803d853d7d5d959b7bce9143b9925706d7ed200fb2e9b3112adbfcfa58aed67ad91ea0
data/README.md CHANGED
@@ -33,6 +33,22 @@ require 'mongo_trails/mongo_support/config'
33
33
 
34
34
  Done!
35
35
 
36
+ ## Restores
37
+
38
+ Paper Trail records `create`, `update` and `destroy`, but a soft-deleted record brought back by [Paranoia](https://github.com/rubysherpas/paranoia) is restored with `update_columns`, which Paper Trail never sees. Include `MongoTrails::Restorable` in a Paranoia model to record a version with event `restore` (`MongoTrails::RESTORE_EVENT`) each time it is restored:
39
+
40
+ ```ruby
41
+ class Post < ActiveRecord::Base
42
+ acts_as_paranoid
43
+ has_paper_trail
44
+ include MongoTrails::Restorable # after acts_as_paranoid, which defines the restore callbacks
45
+ end
46
+ ```
47
+
48
+ The version's changes are the columns Paranoia writes on restore (`paranoia_restore_attributes`), from their deleted values to the restored ones. Associated records Paranoia leaves deleted (outside the recovery window) get no version. Paranoia is not a dependency of this gem.
49
+
50
+ To record a restore done some other way, call `record.paper_trail.record_restore(changes)` with each restored column mapped to `[before, after]`, for example `{ 'deleted_at' => [deleted_at, nil] }`. Like `record_update_columns`, it does nothing while Paper Trail is disabled and respects the record size limit.
51
+
36
52
  ## Config Options
37
53
 
38
54
  `PaperTrail.config.mongo_config = { hosts: ['localhost:27017'], database: 'my_test_db' }` - Options for MongoDB connection
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mongo_trails (17.0.0)
4
+ mongo_trails (17.0.1)
5
5
  mongoid (>= 7, < 10)
6
6
  ostruct
7
7
  paper_trail (>= 16, < 18)
@@ -172,7 +172,7 @@ CHECKSUMS
172
172
  mcp (0.8.0) sha256=ae8bd146bb8e168852866fd26f805f52744f6326afb3211e073f78a95e0c34fb
173
173
  minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
174
174
  mongo (2.25.0) sha256=b4663884d23523d24f50c579846956444410f939093e8c2a95fa4b76bb929053
175
- mongo_trails (17.0.0)
175
+ mongo_trails (17.0.1)
176
176
  mongoid (9.1.0) sha256=ef7b50f4c84e636850c3431bc3c92f98481f4e1d147ff06a1c21242d0b242a9e
177
177
  ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912
178
178
  paper_trail (17.0.0) sha256=1c2842061d3874ca7015908e821e2aa14f9b982af2acb2a7974713bf79021c85
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mongo_trails (17.0.0)
4
+ mongo_trails (17.0.1)
5
5
  mongoid (>= 7, < 10)
6
6
  ostruct
7
7
  paper_trail (>= 16, < 18)
@@ -172,7 +172,7 @@ CHECKSUMS
172
172
  mcp (0.8.0) sha256=ae8bd146bb8e168852866fd26f805f52744f6326afb3211e073f78a95e0c34fb
173
173
  minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
174
174
  mongo (2.25.0) sha256=b4663884d23523d24f50c579846956444410f939093e8c2a95fa4b76bb929053
175
- mongo_trails (17.0.0)
175
+ mongo_trails (17.0.1)
176
176
  mongoid (9.1.0) sha256=ef7b50f4c84e636850c3431bc3c92f98481f4e1d147ff06a1c21242d0b242a9e
177
177
  ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912
178
178
  paper_trail (17.0.0) sha256=1c2842061d3874ca7015908e821e2aa14f9b982af2acb2a7974713bf79021c85
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mongo_trails (17.0.0)
4
+ mongo_trails (17.0.1)
5
5
  mongoid (>= 7, < 10)
6
6
  ostruct
7
7
  paper_trail (>= 16, < 18)
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- mongo_trails (17.0.0)
4
+ mongo_trails (17.0.1)
5
5
  mongoid (>= 7, < 10)
6
6
  ostruct
7
7
  paper_trail (>= 16, < 18)
@@ -175,7 +175,7 @@ CHECKSUMS
175
175
  mcp (0.8.0) sha256=ae8bd146bb8e168852866fd26f805f52744f6326afb3211e073f78a95e0c34fb
176
176
  minitest (5.27.0) sha256=2d3b17f8a36fe7801c1adcffdbc38233b938eb0b4966e97a6739055a45fa77d5
177
177
  mongo (2.25.0) sha256=b4663884d23523d24f50c579846956444410f939093e8c2a95fa4b76bb929053
178
- mongo_trails (17.0.0)
178
+ mongo_trails (17.0.1)
179
179
  mongoid (9.1.0) sha256=ef7b50f4c84e636850c3431bc3c92f98481f4e1d147ff06a1c21242d0b242a9e
180
180
  ostruct (0.6.3) sha256=95a2ed4a4bd1d190784e666b47b2d3f078e4a9efda2fccf18f84ddc6538ed912
181
181
  paper_trail (16.0.0) sha256=e9b9f0fb1b8b590c8231cfa931b282ba92f90e066e393930a5e1c61ae4c5019d
@@ -43,8 +43,23 @@ module PaperTrail
43
43
  def record_update_columns(changes)
44
44
  return unless enabled?
45
45
 
46
- event = Events::Update.new(@record, false, false, changes)
47
- data = event.data.merge(data_for_update_columns)
46
+ save_update_columns_version(changes)
47
+ end
48
+
49
+ # Records a 'restore' version for a soft-deleted record brought back outside Paper Trail's callbacks,
50
+ # e.g. by Paranoia, which writes with update_columns. `changes` maps each restored column to
51
+ # [before, after], such as { 'deleted_at' => [deleted_at, nil] }.
52
+ def record_restore(changes)
53
+ return unless enabled?
54
+
55
+ save_update_columns_version(changes, event: MongoTrails::RESTORE_EVENT)
56
+ end
57
+
58
+ private
59
+
60
+ def save_update_columns_version(changes, event: nil)
61
+ data = Events::Update.new(@record, false, false, changes).data.merge(data_for_update_columns)
62
+ data[:event] = event if event
48
63
  versions_assoc = @record.send(@record.class.versions_association_name)
49
64
  version = versions_assoc.new(data)
50
65
  return if exceeds_record_size_limit?(version)
@@ -53,8 +68,6 @@ module PaperTrail
53
68
  version.save_version
54
69
  end
55
70
 
56
- private
57
-
58
71
  def exceeds_record_size_limit?(version)
59
72
  size_limit = PaperTrail.config.mongo_trails_config&.dig(:record_size_limit)
60
73
  size_limit && size_limit.to_i < version.to_json.to_s.bytesize
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MongoTrails
4
+ # Paper Trail event of the version recorded when a soft-deleted record is restored (RecordTrail#record_restore)
5
+ RESTORE_EVENT = 'restore'
6
+
7
+ # Records a 'restore' version when a Paranoia model is restored. Paranoia restores with update_columns,
8
+ # which Paper Trail never sees, so the version history would otherwise jump from 'destroy' to the next update.
9
+ #
10
+ # class Post < ActiveRecord::Base
11
+ # acts_as_paranoid
12
+ # has_paper_trail
13
+ # include MongoTrails::Restorable
14
+ # end
15
+ #
16
+ # Include it after acts_as_paranoid, which defines the restore callbacks. The version's changes are the columns
17
+ # Paranoia writes on restore (paranoia_restore_attributes), from their deleted values to the restored ones.
18
+ # Paranoia runs the callbacks for associated records outside the recovery window too, so only a record that
19
+ # was actually restored gets a version.
20
+ module Restorable
21
+ extend ActiveSupport::Concern
22
+
23
+ included do
24
+ before_restore :remember_mongo_trails_deleted_attributes
25
+ after_restore :record_mongo_trails_restore
26
+ end
27
+
28
+ private
29
+
30
+ def remember_mongo_trails_deleted_attributes
31
+ @mongo_trails_deleted_attributes = paranoia_destroyed? ? attributes.slice(*mongo_trails_restore_columns) : nil
32
+ end
33
+
34
+ def record_mongo_trails_restore
35
+ deleted_attributes = @mongo_trails_deleted_attributes
36
+ @mongo_trails_deleted_attributes = nil
37
+ return if deleted_attributes.nil? || paranoia_destroyed?
38
+
39
+ paper_trail.record_restore(deleted_attributes.to_h { |column, value| [column, [value, self[column]]] })
40
+ end
41
+
42
+ def mongo_trails_restore_columns
43
+ send(:paranoia_restore_attributes).keys.map(&:to_s)
44
+ end
45
+ end
46
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MongoTrails
4
- VERSION = '17.0.0'
4
+ VERSION = '17.0.1'
5
5
  end
data/lib/mongo_trails.rb CHANGED
@@ -12,6 +12,7 @@ require 'mongo_trails/events/base'
12
12
  require 'mongo_trails/model_config'
13
13
  require 'mongo_trails/version_concern'
14
14
  require 'mongo_trails/record_trail'
15
+ require 'mongo_trails/restorable'
15
16
 
16
17
  ActiveSupport.on_load(:active_record) do
17
18
  require 'mongo_trails/mongo_support/version'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongo_trails
3
3
  version: !ruby/object:Gem::Version
4
- version: 17.0.0
4
+ version: 17.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Tsirel
@@ -123,6 +123,7 @@ files:
123
123
  - lib/mongo_trails/mongo_support/version_commit_wrap.rb
124
124
  - lib/mongo_trails/mongo_support/write_version_worker.rb
125
125
  - lib/mongo_trails/record_trail.rb
126
+ - lib/mongo_trails/restorable.rb
126
127
  - lib/mongo_trails/version.rb
127
128
  - lib/mongo_trails/version_concern.rb
128
129
  - mongo_trails.gemspec