paper_trail-association_tracking 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -7
- data/README.md +75 -49
- data/Rakefile +1 -4
- data/lib/generators/paper_trail_association_tracking/install_generator.rb +1 -1
- data/lib/paper_trail-association_tracking.rb +8 -16
- data/lib/paper_trail_association_tracking/frameworks/active_record.rb +0 -2
- data/lib/paper_trail_association_tracking/frameworks/rails/railtie.rb +17 -0
- data/lib/paper_trail_association_tracking/frameworks/rails.rb +1 -1
- data/lib/paper_trail_association_tracking/model_config.rb +28 -9
- data/lib/paper_trail_association_tracking/reifiers/has_many.rb +7 -1
- data/lib/paper_trail_association_tracking/reifiers/has_many_through.rb +10 -1
- data/lib/paper_trail_association_tracking/version.rb +1 -1
- data/lib/paper_trail_association_tracking/version_concern.rb +1 -20
- metadata +16 -31
- data/lib/paper_trail_association_tracking/frameworks/rails/engine.rb +0 -10
- data/lib/paper_trail_association_tracking/frameworks/rspec.rb +0 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9af07695b14d97e4ca838054a2764188521584545338887289e9c56f91cdb22b
|
4
|
+
data.tar.gz: cb80d2ce702c4d2178cbd82c3191a3b9a81a72e7d7349447fe517b21d65e6e6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2723be9c0afe1f2ffcdec97638ac073a7074ece8d78a67f42a8fe986fa56533e4a9d8306293cd7df1eedde7bfbee5dd3aa29990fb273db41f5dae9cc4a4b4d70
|
7
|
+
data.tar.gz: 27a332d6d0a2aa9b810f2c493f9b48f4c4567b9e8ca1280a64e25206771168ad9982f99c75a6b53cfa1d20a670c2e3f395c16cf30bdab3fafa39f2d2ef2cec51
|
data/CHANGELOG.md
CHANGED
@@ -1,17 +1,36 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
### Unreleased - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.2.0...master)
|
5
4
|
- Nothing yet
|
6
5
|
|
7
|
-
|
6
|
+
### v2.2.0 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.3...v2.2.0)
|
7
|
+
|
8
|
+
- [PR #36](https://github.com/westonganger/paper_trail-association_tracking/pull/36) - Fix load order for paper_trail v12+
|
9
|
+
- Drop support for Ruby 2.5
|
10
|
+
- Add Github Actions CI supporting multiple version of Ruby, Rails and multiple databases types
|
11
|
+
|
12
|
+
### 2.1.3 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.1...v2.1.3)
|
13
|
+
|
14
|
+
- [PR #24](https://github.com/westonganger/paper_trail-association_tracking/pull/24) - Fix reification on STI models that have parent child relationships
|
15
|
+
|
16
|
+
### 2.1.2
|
17
|
+
|
18
|
+
- A late night oopsies, Release yanked immediately, had bug preventing installation.
|
19
|
+
|
20
|
+
### 2.1.1 - 2020-10-21 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.0...v2.1.1)
|
21
|
+
|
22
|
+
- Bug fix for reify on `has_many :through` relationships when `:source` is specified
|
23
|
+
- Bug fix for reify on `has_many :through` relationships where the association is a has_one on the through model
|
24
|
+
- Bug fix to ensure install generator will set `PaperTrail.association_tracking = true`
|
25
|
+
|
26
|
+
### 2.1.0 - 2020-08-14 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.0.0...v2.1.0)
|
8
27
|
|
9
28
|
- [PR #18](https://github.com/westonganger/paper_trail-association_tracking/pull/18) - Improve performance for `Model.reify(has_many: true)` by separating the SQL subquery.
|
10
29
|
- [PR #15](https://github.com/westonganger/paper_trail-association_tracking/pull/15) - Recreate `version_associations.foreign_key` index to utilize the new `version_associations.foreign_type` column
|
11
30
|
- Update test matrix to support multiple versions of PT-core and ActiveRecord
|
12
31
|
- Remove deprecated methods `clear_transaction_id`, `transaction_id` and `transaction_id=`
|
13
32
|
|
14
|
-
|
33
|
+
### 2.0.0 - 2019-01-22 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v1.1.1...v2.0.0)
|
15
34
|
|
16
35
|
- [PR #11](https://github.com/westonganger/paper_trail-association_tracking/issues/11) - Remove null constraint on `version_associations.foreign_type` column which was added in `v1.1.0`. This fixes issues adding the column to existing projects who are upgrading.
|
17
36
|
- Add generator `rails g paper_trail_association_tracking:add_foreign_type_to_version_associations` for `versions_associations.foreign_type` column for upgrading applications from `v1.0.0` or earlier.
|
@@ -20,16 +39,16 @@
|
|
20
39
|
|
21
40
|
- Run `rails g paper_trail_association_tracking:add_foreign_type_to_version_associations` and then migrate your database.
|
22
41
|
|
23
|
-
|
42
|
+
### 1.1.1 - 2018-01-14 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v1.1.0...v1.1.1)
|
24
43
|
|
25
44
|
- Same as v2 release, this is released simply to maintain a working `v1` branch since `v1.1.0` was broken
|
26
45
|
|
27
|
-
|
46
|
+
### 1.1.0 - 2018-12-28 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v1.0.0...v1.1.0)
|
28
47
|
|
29
48
|
- Note: This release is somewhat broken, please upgrade to `v2.0.0` or stay on `v1.0.0`
|
30
49
|
- [PR #10](https://github.com/westonganger/paper_trail-association_tracking/pull/10) - The `has_many: true` option now reifies polymorphic associations. Previously they were skipped.
|
31
50
|
- [PR #9](https://github.com/westonganger/paper_trail-association_tracking/pull/9) - The `belongs_to: true` option now reifies polymorphic associations. Previously they were skipped.
|
32
51
|
|
33
|
-
|
52
|
+
### 1.0.0 - 2018-06-04
|
34
53
|
|
35
54
|
- [PT #1070](https://github.com/paper-trail-gem/paper_trail/issues/1070), [#2](https://github.com/westonganger/paper_trail-association_tracking/issues/2) - Extracted from paper_trail gem in v9.2
|
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# PaperTrail-AssociationTracking
|
2
2
|
|
3
3
|
<a href="https://badge.fury.io/rb/paper_trail-association_tracking" target="_blank"><img height="21" style='border:0px;height:21px;' border='0' src="https://badge.fury.io/rb/paper_trail-association_tracking.svg" alt="Gem Version"></a>
|
4
|
-
<a href='https://
|
4
|
+
<a href='https://github.com/westonganger/paper_trail-association_tracking/actions' target='_blank'><img src="https://github.com/westonganger/paper_trail-association_tracking/workflows/Tests/badge.svg" style="max-width:100%;" height='21' style='border:0px;height:21px;' border='0' alt="CI Status"></a>
|
5
5
|
<a href='https://rubygems.org/gems/paper_trail-association_tracking' target='_blank'><img height='21' style='border:0px;height:21px;' src='https://ruby-gem-downloads-badge.herokuapp.com/paper_trail-association_tracking?label=rubygems&type=total&total_label=downloads&color=brightgreen' border='0' alt='RubyGems Downloads' /></a>
|
6
6
|
|
7
7
|
Plugin for the [PaperTrail](https://github.com/paper-trail-gem/paper_trail.git) gem to track and reify associations. This gem was extracted from PaperTrail for v9.2.0 to simplify things in PaperTrail and association tracking separately.
|
@@ -23,17 +23,23 @@ It will store in the `version_associations` table additional information to corr
|
|
23
23
|
- [Known Issues](#known-issues)
|
24
24
|
- [Contributing](#contributing)
|
25
25
|
- [Credits](#credits)
|
26
|
+
- [Alternative Solution](#alternative-solution)
|
26
27
|
|
27
28
|
|
28
29
|
# Install
|
29
30
|
|
30
31
|
```ruby
|
31
|
-
|
32
|
-
|
33
|
-
gem 'paper_trail' # Minimum required version is v9.2.0
|
32
|
+
gem 'paper_trail'
|
34
33
|
gem 'paper_trail-association_tracking'
|
35
34
|
```
|
36
35
|
|
36
|
+
For PT v9.2 to v11.x you must use PT-AT v2
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
gem 'paper_trail'#, ">=9.2.0", "< 12.0"
|
40
|
+
gem 'paper_trail-association_tracking', "<= 2.1.3"
|
41
|
+
```
|
42
|
+
|
37
43
|
Then run `rails generate paper_trail_association_tracking:install` which will do the following two things for you:
|
38
44
|
|
39
45
|
1. Create a `version_associations` table
|
@@ -55,56 +61,76 @@ For example:
|
|
55
61
|
item.versions.last.reify(has_many: true, has_one: true, belongs_to: false)
|
56
62
|
```
|
57
63
|
|
58
|
-
If
|
64
|
+
If you want the reified associations to be saved upon calling `save` on the parent model then you must set `autosave: true` on all required associations. A little tip, `accepts_nested_attributes` automatically sets `autosave` to true but you should probably still state it explicitly.
|
59
65
|
|
60
|
-
|
61
|
-
item.amount # 100
|
62
|
-
item.location.latitude # 12.345
|
66
|
+
For example:
|
63
67
|
|
64
|
-
|
65
|
-
|
66
|
-
|
68
|
+
```ruby
|
69
|
+
class Product
|
70
|
+
has_many :photos, autosave: true
|
67
71
|
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
|
-
t.location.latitude # 12.345, instead of 54.321
|
73
|
+
product = Product.first.versions.last.reify(has_many: true, has_one: true, belongs_to: false)
|
74
|
+
product.save! ### now this will also save all reified photos
|
72
75
|
```
|
73
76
|
|
74
|
-
|
77
|
+
If you do not set `autosave: true` true on the association then you will have to save/delete them manually.
|
78
|
+
|
79
|
+
For example:
|
75
80
|
|
76
81
|
```ruby
|
77
82
|
class Product < ActiveRecord::Base
|
78
83
|
has_paper_trail
|
79
|
-
|
80
|
-
end
|
81
|
-
|
82
|
-
class Photo < ActiveRecord::Base
|
83
|
-
has_paper_trail
|
84
|
-
belongs_to :product
|
84
|
+
has_many :photos, autosave: false ### or if autosave not set
|
85
85
|
end
|
86
86
|
|
87
87
|
product = Product.create(name: 'product_0')
|
88
|
-
product.
|
89
|
-
product.
|
88
|
+
product.photos.create(name: 'photo')
|
89
|
+
product.update(name: 'product_a')
|
90
|
+
product.photos.create(name: 'photo')
|
91
|
+
|
92
|
+
reified_product = product.versions.last.reify(has_many: true, mark_for_destruction: true)
|
93
|
+
reified_product.save!
|
94
|
+
reified_product.name # product_a
|
95
|
+
reified_product.photos.size # 2
|
96
|
+
reified_product.photos.reload
|
97
|
+
reified_product.photos.size # 1 ### bad, didnt save the associations
|
98
|
+
|
99
|
+
product = Product.create(name: 'product_1')
|
100
|
+
product.update(name: 'product_b')
|
101
|
+
product.photos.create(name: 'photo')
|
102
|
+
|
103
|
+
reified_product = product.versions.last.reify(has_many: true, mark_for_destruction: true)
|
104
|
+
reified_product.save!
|
105
|
+
reified_product.name # product_b
|
106
|
+
reified_product.photos.size # 1
|
107
|
+
reified_product.photos.each{|x| x.marked_for_destruction? ? x.destroy! : x.save! }
|
108
|
+
reified_product.photos.size # 0
|
109
|
+
```
|
90
110
|
|
91
|
-
|
92
|
-
product_0.photo # nil
|
111
|
+
It will also respect AR transactions by utilizing the aforementioned `transaction_id` to reify the models as they were before the transaction (instead of before the update to the model).
|
93
112
|
|
94
|
-
|
95
|
-
product_0.photo.marked_for_destruction? # true
|
96
|
-
product_0.save!
|
97
|
-
product.reload.photo # nil
|
98
|
-
```
|
113
|
+
For example:
|
99
114
|
|
115
|
+
```ruby
|
116
|
+
item.amount # 100
|
117
|
+
item.location.latitude # 12.345
|
118
|
+
|
119
|
+
Item.transaction do
|
120
|
+
item.location.update(latitude: 54.321)
|
121
|
+
item.update(amount: 153)
|
122
|
+
end
|
123
|
+
|
124
|
+
t = item.versions.last.reify(has_one: true)
|
125
|
+
t.amount # 100
|
126
|
+
t.location.latitude # 12.345, instead of 54.321
|
127
|
+
```
|
100
128
|
|
101
129
|
# Limitations
|
102
130
|
|
103
|
-
1. Only reifies the first level of associations. If you want to include nested associations simply add
|
131
|
+
1. Only reifies the first level of associations. If you want to include nested associations simply add `:through` relationships to your model.
|
104
132
|
1. Currently we only supports a single `version_associations` table. Therefore, you can only use a single table to store the versions for all related models.
|
105
|
-
1. Relies on the callbacks on the association model (and the
|
106
|
-
|
107
|
-
Given these models:
|
133
|
+
1. Relies on the callbacks on the association model (and the `:through` association model for Has-Many-Through associations) to record the versions and the relationship between the versions. If the association is changed without invoking the callbacks, then reification won't work. Example:
|
108
134
|
|
109
135
|
```ruby
|
110
136
|
class Book < ActiveRecord::Base
|
@@ -124,22 +150,16 @@ product.reload.photo # nil
|
|
124
150
|
has_many :books, through: :authorships
|
125
151
|
has_paper_trail
|
126
152
|
end
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
```ruby
|
132
|
-
@book.authors << @dostoyevsky
|
133
|
-
@book.authors.create(name: 'Tolstoy')
|
153
|
+
|
154
|
+
### Each of the following will store authorship versions:
|
155
|
+
@book.authors << @john
|
156
|
+
@book.authors.create(name: 'Jack')
|
134
157
|
@book.authorships.last.destroy
|
135
158
|
@book.authorships.clear
|
136
|
-
@book.author_ids = [@
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
```ruby
|
142
|
-
@book.authors.delete @tolstoy
|
159
|
+
@book.author_ids = [@john.id, @joe.id]
|
160
|
+
|
161
|
+
### But none of these will:
|
162
|
+
@book.authors.delete @john
|
143
163
|
@book.author_ids = []
|
144
164
|
@book.authors = []
|
145
165
|
```
|
@@ -149,7 +169,7 @@ product.reload.photo # nil
|
|
149
169
|
|
150
170
|
1. Sometimes the has_one association will find more than one possible candidate and will raise a `PaperTrailAssociationTracking::Reifiers::HasOne::FoundMoreThanOne` error. For example, see `spec/models/person_spec.rb`
|
151
171
|
- If you are not using STI, you may want to just assume the first result of multiple is the correct one and continue. PaperTrail <= v8 did this without error or warning. To do so add the following line to your initializer: `PaperTrail.config.association_reify_error_behaviour = :warn`. Valid options are: `[:error, :warn, :ignore]`
|
152
|
-
- When using STI, even if you enable `:warn` you will likely still end up recieving an `ActiveRecord::AssociationTypeMismatch` error. See [PT Issue #594](https://github.com/airblade/paper_trail/issues/594). I recommend that you
|
172
|
+
- When using STI, even if you enable `:warn` you will likely still end up recieving an `ActiveRecord::AssociationTypeMismatch` error. See [PT Issue #594](https://github.com/airblade/paper_trail/issues/594). I strongly recommend that you do not use STI, however if you do need to decide to use STI, please see https://github.com/paper-trail-gem/paper_trail#4b1-the-optional-item_subtype-column
|
153
173
|
1. Not compatible with transactional tests, see [PT Issue #542](https://github.com/airblade/paper_trail/issues/542). However, apparently there has been some success by using the [transactional_capybara](https://rubygems.org/gems/transactional_capybara) gem.
|
154
174
|
|
155
175
|
|
@@ -168,3 +188,9 @@ Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https:
|
|
168
188
|
Plugin authored by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
|
169
189
|
|
170
190
|
Associations code originally contributed by Ben Atkins, Jared Beck, Andy Stewart & more
|
191
|
+
|
192
|
+
# Alternative Solution
|
193
|
+
|
194
|
+
Model Versioning and Restoration require concious thought, design, and understanding. You should understand your versioning and restoration process completely. Because PT-AT it is mostly a blackbox solution which encourages you to set it up and then assume its "Just Working". This can make for major data problems later.
|
195
|
+
|
196
|
+
Instead I recommend a newer gem that I have created for handling snapshots of records and associations called [active_snapshot](https://github.com/westonganger/active_snapshot). This gem does not utilize `paper_trail` at all. The focus of this gem is to have a simple and fully understandable design is easy to customize and know inside and out for your projects needs.
|
data/Rakefile
CHANGED
@@ -17,9 +17,6 @@ Rake::TestTask.new(:test) do |t|
|
|
17
17
|
t.libs << "test"
|
18
18
|
t.pattern = "test/**/*_test.rb"
|
19
19
|
t.verbose = false
|
20
|
-
|
21
|
-
### Enabling ruby interpreter warnings (-w) is, sadly, impractical.
|
22
|
-
### There are too many noisy warnings that we have no control over, e.g. caused by libs we depend on.
|
23
20
|
t.warning = false
|
24
21
|
end
|
25
22
|
|
@@ -27,7 +24,7 @@ require "rspec/core/rake_task"
|
|
27
24
|
desc "Run tests on PaperTrail with RSpec"
|
28
25
|
task(:spec).clear
|
29
26
|
RSpec::Core::RakeTask.new(:spec) do |t|
|
30
|
-
t.verbose = false # hide list of specs
|
27
|
+
t.verbose = false # hide list of specs
|
31
28
|
end
|
32
29
|
|
33
30
|
desc "Default: run all available test suites"
|
@@ -28,7 +28,7 @@ module PaperTrailAssociationTracking
|
|
28
28
|
def create_initializer
|
29
29
|
create_file(
|
30
30
|
"config/initializers/paper_trail.rb",
|
31
|
-
"PaperTrail.config.track_associations =
|
31
|
+
"PaperTrail.config.track_associations = true\n",
|
32
32
|
"PaperTrail.config.association_reify_error_behaviour = :error"
|
33
33
|
)
|
34
34
|
end
|
@@ -9,6 +9,14 @@ require "paper_trail_association_tracking/request"
|
|
9
9
|
require "paper_trail_association_tracking/paper_trail"
|
10
10
|
require "paper_trail_association_tracking/version_concern"
|
11
11
|
|
12
|
+
if defined?(Rails)
|
13
|
+
require "paper_trail/frameworks/active_record"
|
14
|
+
require "paper_trail_association_tracking/frameworks/rails"
|
15
|
+
elsif defined?(ActiveRecord)
|
16
|
+
require "paper_trail/frameworks/active_record"
|
17
|
+
require "paper_trail_association_tracking/frameworks/active_record"
|
18
|
+
end
|
19
|
+
|
12
20
|
module PaperTrailAssociationTracking
|
13
21
|
def self.version
|
14
22
|
VERSION
|
@@ -47,20 +55,4 @@ module PaperTrail
|
|
47
55
|
prepend ::PaperTrailAssociationTracking::Request::ClassMethods
|
48
56
|
end
|
49
57
|
end
|
50
|
-
|
51
|
-
module VersionConcern
|
52
|
-
include ::PaperTrailAssociationTracking::VersionConcern
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
# Require frameworks
|
57
|
-
if defined?(::Rails)
|
58
|
-
# Rails module is sometimes defined by gems like rails-html-sanitizer so we check for presence of Rails.application.
|
59
|
-
if defined?(::Rails.application)
|
60
|
-
require "paper_trail_association_tracking/frameworks/rails"
|
61
|
-
else
|
62
|
-
::Kernel.warn('PaperTrail has been loaded too early, before rails is loaded. This can happen when another gem defines the ::Rails namespace, then PT is loaded, all before rails is loaded. You may want to reorder your Gemfile, or defer the loading of PT by using `require: false` and a manual require elsewhere.')
|
63
|
-
end
|
64
|
-
else
|
65
|
-
require "paper_trail_association_tracking/frameworks/active_record"
|
66
58
|
end
|
@@ -1,5 +1,3 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# This file only needs to be loaded if the gem is being used outside of Rails,
|
4
|
-
# since otherwise the model(s) will get loaded in via the `Rails::Engine`.
|
5
3
|
require "paper_trail_association_tracking/frameworks/active_record/models/paper_trail/version_association"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaperTrailAssociationTracking
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
|
6
|
+
initializer "paper_trail_association_tracking", after: "paper_trail" do
|
7
|
+
ActiveSupport.on_load(:active_record) do
|
8
|
+
require "paper_trail_association_tracking/frameworks/active_record"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
config.to_prepare do
|
13
|
+
::PaperTrail::Version.include(::PaperTrailAssociationTracking::VersionConcern)
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -38,15 +38,34 @@ module PaperTrailAssociationTracking
|
|
38
38
|
habtm_assocs_not_skipped.each(&method(:setup_habtm_change_callbacks))
|
39
39
|
end
|
40
40
|
|
41
|
-
def setup_habtm_change_callbacks(
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
41
|
+
def setup_habtm_change_callbacks(association)
|
42
|
+
association_name = association.name
|
43
|
+
|
44
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
45
|
+
### https://github.com/westonganger/paper_trail-association_tracking/pull/37#issuecomment-1067146121
|
46
|
+
|
47
|
+
before_add_callback = lambda do |*args|
|
48
|
+
update_habtm_state(association_name, :before_add, args[-2], args.last)
|
49
|
+
end
|
50
|
+
|
51
|
+
before_remove_callback = lambda do |*args|
|
52
|
+
update_habtm_state(association_name, :before_remove, args[-2], args.last)
|
53
|
+
end
|
54
|
+
|
55
|
+
assoc_opts = association.options.merge(before_add: before_add_callback, before_remove: before_remove_callback)
|
56
|
+
|
57
|
+
association.instance_variable_set(:@options, **assoc_opts)
|
58
|
+
|
59
|
+
::ActiveRecord::Associations::Builder::CollectionAssociation.send(:define_callbacks, @model_class, association)
|
60
|
+
else
|
61
|
+
%w[add remove].each do |verb|
|
62
|
+
@model_class.send("before_#{verb}_for_#{association_name}").send(
|
63
|
+
:<<,
|
64
|
+
lambda do |*args|
|
65
|
+
update_habtm_state(association_name, :"before_#{verb}", args[-2], args.last)
|
66
|
+
end
|
67
|
+
)
|
68
|
+
end
|
50
69
|
end
|
51
70
|
end
|
52
71
|
|
@@ -94,12 +94,18 @@ module PaperTrailAssociationTracking
|
|
94
94
|
# from the point in time identified by `tx_id` or `version_at`.
|
95
95
|
# @api private
|
96
96
|
def load_versions_for_hm_association(assoc, model, version_table, tx_id, version_at)
|
97
|
+
# For STI models, associations may be defined to reference superclasses, so looking up
|
98
|
+
# based on only the child-most class is not appropriate.
|
99
|
+
sti_model_names = model.class.ancestors
|
100
|
+
.select { |x| x <= model.class.base_class && x.method_defined?(assoc.name) }
|
101
|
+
.map(&:name)
|
102
|
+
|
97
103
|
version_ids = ::PaperTrail::VersionAssociation.
|
98
104
|
joins(model.class.version_association_name).
|
99
105
|
select("MIN(version_id) as version_id").
|
100
106
|
where("foreign_key_name = ?", assoc.foreign_key).
|
101
107
|
where("foreign_key_id = ?", model.id).
|
102
|
-
where(foreign_type: [
|
108
|
+
where(foreign_type: sti_model_names + [nil]).
|
103
109
|
where("#{version_table}.item_type = ?", assoc.klass.base_class.name).
|
104
110
|
where("created_at >= ? OR transaction_id = ?", version_at, tx_id).
|
105
111
|
group("item_id").
|
@@ -53,7 +53,16 @@ module PaperTrailAssociationTracking
|
|
53
53
|
# association, that is, `model.paragraphs`, has not been loaded. So,
|
54
54
|
# we do that now.
|
55
55
|
through_collection.flat_map { |through_model|
|
56
|
-
through_model.public_send(assoc.
|
56
|
+
records = through_model.public_send(assoc.source_reflection_name)
|
57
|
+
|
58
|
+
if records.respond_to?(:to_a)
|
59
|
+
# Has Many association
|
60
|
+
records = records.to_a
|
61
|
+
else
|
62
|
+
# Has One association - Nothing more to do
|
63
|
+
end
|
64
|
+
|
65
|
+
records
|
57
66
|
}
|
58
67
|
end
|
59
68
|
|
@@ -1,10 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module PaperTrailAssociationTracking
|
4
|
-
# Originally, PaperTrail did not provide this module, and all of this
|
5
|
-
# functionality was in `PaperTrail::Version`. That model still exists (and is
|
6
|
-
# used by most apps) but by moving the functionality to this module, people
|
7
|
-
# can include this concern instead of sub-classing the `Version` model.
|
8
4
|
module VersionConcern
|
9
5
|
extend ::ActiveSupport::Concern
|
10
6
|
|
@@ -16,22 +12,7 @@ module PaperTrailAssociationTracking
|
|
16
12
|
has_many :version_associations, dependent: :destroy
|
17
13
|
end
|
18
14
|
|
19
|
-
scope
|
15
|
+
scope :within_transaction, ->(id) { where(transaction_id: id) }
|
20
16
|
end
|
21
|
-
|
22
|
-
# Restore the item from this version.
|
23
|
-
#
|
24
|
-
# In addition to the options provided by PaperTrail core. This Plugin provides the following Options:
|
25
|
-
#
|
26
|
-
# - :has_one
|
27
|
-
# - `true` - Also reify has_one associations.
|
28
|
-
# - `false - Default.
|
29
|
-
# - :has_many
|
30
|
-
# - `true` - Also reify has_many and has_many :through associations.
|
31
|
-
# - `false` - Default.
|
32
|
-
#
|
33
|
-
#def reify(options = {})
|
34
|
-
# super
|
35
|
-
#end
|
36
17
|
end
|
37
18
|
end
|
metadata
CHANGED
@@ -1,47 +1,33 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paper_trail-association_tracking
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weston Ganger
|
8
8
|
- Jared Beck
|
9
9
|
- Ben Atkins
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2022-03-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
|
-
name:
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
-
requirements:
|
19
|
-
- - ">="
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
version: '0'
|
29
|
-
- !ruby/object:Gem::Dependency
|
30
|
-
name: byebug
|
16
|
+
name: paper_trail
|
31
17
|
requirement: !ruby/object:Gem::Requirement
|
32
18
|
requirements:
|
33
19
|
- - ">="
|
34
20
|
- !ruby/object:Gem::Version
|
35
|
-
version: '0'
|
36
|
-
type: :
|
21
|
+
version: '12.0'
|
22
|
+
type: :runtime
|
37
23
|
prerelease: false
|
38
24
|
version_requirements: !ruby/object:Gem::Requirement
|
39
25
|
requirements:
|
40
26
|
- - ">="
|
41
27
|
- !ruby/object:Gem::Version
|
42
|
-
version: '0'
|
28
|
+
version: '12.0'
|
43
29
|
- !ruby/object:Gem::Dependency
|
44
|
-
name:
|
30
|
+
name: appraisal
|
45
31
|
requirement: !ruby/object:Gem::Requirement
|
46
32
|
requirements:
|
47
33
|
- - ">="
|
@@ -55,7 +41,7 @@ dependencies:
|
|
55
41
|
- !ruby/object:Gem::Version
|
56
42
|
version: '0'
|
57
43
|
- !ruby/object:Gem::Dependency
|
58
|
-
name:
|
44
|
+
name: byebug
|
59
45
|
requirement: !ruby/object:Gem::Requirement
|
60
46
|
requirements:
|
61
47
|
- - ">="
|
@@ -69,7 +55,7 @@ dependencies:
|
|
69
55
|
- !ruby/object:Gem::Version
|
70
56
|
version: '0'
|
71
57
|
- !ruby/object:Gem::Dependency
|
72
|
-
name:
|
58
|
+
name: ffaker
|
73
59
|
requirement: !ruby/object:Gem::Requirement
|
74
60
|
requirements:
|
75
61
|
- - ">="
|
@@ -83,7 +69,7 @@ dependencies:
|
|
83
69
|
- !ruby/object:Gem::Version
|
84
70
|
version: '0'
|
85
71
|
- !ruby/object:Gem::Dependency
|
86
|
-
name:
|
72
|
+
name: generator_spec
|
87
73
|
requirement: !ruby/object:Gem::Requirement
|
88
74
|
requirements:
|
89
75
|
- - ">="
|
@@ -186,8 +172,7 @@ files:
|
|
186
172
|
- lib/paper_trail_association_tracking/frameworks/active_record.rb
|
187
173
|
- lib/paper_trail_association_tracking/frameworks/active_record/models/paper_trail/version_association.rb
|
188
174
|
- lib/paper_trail_association_tracking/frameworks/rails.rb
|
189
|
-
- lib/paper_trail_association_tracking/frameworks/rails/
|
190
|
-
- lib/paper_trail_association_tracking/frameworks/rspec.rb
|
175
|
+
- lib/paper_trail_association_tracking/frameworks/rails/railtie.rb
|
191
176
|
- lib/paper_trail_association_tracking/model_config.rb
|
192
177
|
- lib/paper_trail_association_tracking/paper_trail.rb
|
193
178
|
- lib/paper_trail_association_tracking/record_trail.rb
|
@@ -205,7 +190,7 @@ homepage: https://github.com/westonganger/paper_trail-association_tracking
|
|
205
190
|
licenses:
|
206
191
|
- MIT
|
207
192
|
metadata: {}
|
208
|
-
post_install_message:
|
193
|
+
post_install_message:
|
209
194
|
rdoc_options: []
|
210
195
|
require_paths:
|
211
196
|
- lib
|
@@ -213,15 +198,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
213
198
|
requirements:
|
214
199
|
- - ">="
|
215
200
|
- !ruby/object:Gem::Version
|
216
|
-
version: 2.
|
201
|
+
version: 2.6.0
|
217
202
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
218
203
|
requirements:
|
219
204
|
- - ">="
|
220
205
|
- !ruby/object:Gem::Version
|
221
206
|
version: '0'
|
222
207
|
requirements: []
|
223
|
-
rubygems_version: 3.
|
224
|
-
signing_key:
|
208
|
+
rubygems_version: 3.2.32
|
209
|
+
signing_key:
|
225
210
|
specification_version: 4
|
226
211
|
summary: Plugin for the PaperTrail gem to track and reify associations
|
227
212
|
test_files: []
|
@@ -1,10 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module PaperTrailAssociationTracking
|
4
|
-
module Rails
|
5
|
-
# See http://guides.rubyonrails.org/engines.html
|
6
|
-
class Engine < ::Rails::Engine
|
7
|
-
paths["app/models"] << "lib/paper_trail_association_tracking/frameworks/active_record/models"
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "rspec/core"
|
4
|
-
require "rspec/matchers"
|
5
|
-
|
6
|
-
RSpec::Matchers.define :have_a_version_with do |attributes|
|
7
|
-
# check if the model has a version with the specified attributes
|
8
|
-
match do |actual|
|
9
|
-
versions_association = actual.class.versions_association_name
|
10
|
-
actual.send(versions_association).where_object(attributes).any?
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
RSpec::Matchers.define :have_a_version_with_changes do |attributes|
|
15
|
-
# check if the model has a version changes with the specified attributes
|
16
|
-
match do |actual|
|
17
|
-
versions_association = actual.class.versions_association_name
|
18
|
-
actual.send(versions_association).where_object_changes(attributes).any?
|
19
|
-
end
|
20
|
-
end
|