paper_trail-association_tracking 2.0.0 → 2.1.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 +15 -4
- data/README.md +57 -131
- data/Rakefile +2 -29
- data/lib/generators/paper_trail_association_tracking/add_foreign_type_to_version_associations_generator.rb +1 -4
- data/lib/generators/paper_trail_association_tracking/install_generator.rb +1 -4
- data/lib/generators/paper_trail_association_tracking/templates/add_foreign_type_to_version_associations.rb.erb +10 -0
- data/lib/paper_trail-association_tracking.rb +1 -2
- data/lib/paper_trail_association_tracking/paper_trail.rb +0 -27
- data/lib/paper_trail_association_tracking/reifiers/has_many.rb +7 -8
- data/lib/paper_trail_association_tracking/reifiers/has_many_through.rb +4 -4
- data/lib/paper_trail_association_tracking/version.rb +1 -1
- metadata +46 -81
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8b2d339d9003d50ddc86c1e1cdea91863bcb2908ce4b4c42db9b59937431a3f0
|
4
|
+
data.tar.gz: c9638879c3932b3f5c07b549401c60b2c473da0afed2dab078b35deb5b8eae10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79f9131a55657ea99ae5393e5c1bb17e00e79744896f99faf70d2df982c2a38a2c06cb17c8bed7131213de814c10c0e96c29f27782ad1bb064db1778649bc6a7
|
7
|
+
data.tar.gz: 2199d8c642a22494c6f792f809bc7cd7143f287f9fc6a2a8cd3ea31243ab100de9914bc27ef06ebeafd902730b46b7747d2f8a5d41ca6e662b81d2354c140315
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
-
##
|
3
|
+
## Unreleased
|
4
4
|
|
5
|
-
-
|
5
|
+
- Nothing yet
|
6
|
+
|
7
|
+
## 2.1.0 - 2020-08-14
|
8
|
+
|
9
|
+
- [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
|
+
- [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
|
+
- Update test matrix to support multiple versions of PT-core and ActiveRecord
|
12
|
+
- Remove deprecated methods `clear_transaction_id`, `transaction_id` and `transaction_id=`
|
13
|
+
|
14
|
+
## 2.0.0 - 2019-01-22
|
15
|
+
|
16
|
+
- [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.
|
6
17
|
- 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.
|
7
18
|
|
8
19
|
### How to Upgrade from v1.0.0 or earlier
|
@@ -16,8 +27,8 @@
|
|
16
27
|
## 1.1.0 - 2018-12-28
|
17
28
|
|
18
29
|
- Note: This release is somewhat broken, please upgrade to `v2.0.0` or stay on `v1.0.0`
|
19
|
-
- [#10](https://github.com/westonganger/paper_trail-association_tracking/pull/
|
20
|
-
- [#9](https://github.com/westonganger/paper_trail-association_tracking/pull/9) - The `belongs_to: true` option now reifies polymorphic associations. Previously they were skipped.
|
30
|
+
- [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
|
+
- [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.
|
21
32
|
|
22
33
|
## 1.0.0 - 2018-06-04
|
23
34
|
|
data/README.md
CHANGED
@@ -1,170 +1,108 @@
|
|
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://travis-ci.
|
4
|
+
<a href='https://travis-ci.com/westonganger/paper_trail-association_tracking' target='_blank'><img height='21' style='border:0px;height:21px;' src='https://api.travis-ci.com/westonganger/paper_trail-association_tracking.svg?branch=master' border='0' alt='Build 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
|
-
Plugin for the [PaperTrail](https://github.com/paper-trail-gem/paper_trail.git) gem to track and reify associations.
|
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.
|
8
8
|
|
9
9
|
**PR's will happily be accepted**
|
10
10
|
|
11
|
-
|
12
|
-
|
11
|
+
PaperTrail-AssociationTracking can restore three types of associations: Has-One, Has-Many, and Has-Many-Through.
|
12
|
+
|
13
|
+
It will store in the `version_associations` table additional information to correlate versions of the association and versions of the model when the associated record is changed. When reifying the model, it will utilize this table, together with the `transaction_id` to find the correct version of the association and reify it. The `transaction_id` is a unique id for version records created in the same transaction. It is used to associate the version of the model and the version of the association that are created in the same transaction.
|
13
14
|
|
14
|
-
A little history lesson, discussed as early as 2009, and first implemented in late 2014, association
|
15
|
-
tracking was part of PT core until 2018 as an experimental feature and was use at your own risk. This gem now
|
16
|
-
maintains a list of known issues and we hope the community can help remove some of them via PR's.
|
17
15
|
|
18
|
-
## Table of Contents
|
19
16
|
|
20
|
-
|
17
|
+
|
18
|
+
## Table of Contents
|
21
19
|
|
22
20
|
- [Install](#install)
|
23
|
-
- [
|
21
|
+
- [Usage](#usage)
|
22
|
+
- [Limitations](#limitations)
|
24
23
|
- [Known Issues](#known-issues)
|
25
24
|
- [Contributing](#contributing)
|
26
25
|
- [Credits](#credits)
|
27
26
|
|
28
|
-
<!-- tocstop -->
|
29
27
|
|
30
28
|
# Install
|
31
29
|
|
32
30
|
```ruby
|
33
31
|
# Gemfile
|
34
32
|
|
35
|
-
gem 'paper_trail' #
|
33
|
+
gem 'paper_trail' # Minimum required version is v9.2.0
|
36
34
|
gem 'paper_trail-association_tracking'
|
37
35
|
```
|
38
36
|
|
39
|
-
|
40
|
-
|
41
|
-
This plugin currently can restore three types of associations: Has-One, Has-Many, and
|
42
|
-
Has-Many-Through. In order to do this, you will need to do two things:
|
37
|
+
Then run `rails generate paper_trail_association_tracking:install` which will do the following two things for you:
|
43
38
|
|
44
39
|
1. Create a `version_associations` table
|
45
|
-
2. Set `PaperTrail.config.track_associations = true`
|
40
|
+
2. Set `PaperTrail.config.track_associations = true` in an initializer
|
46
41
|
|
42
|
+
# Usage
|
47
43
|
|
48
|
-
|
44
|
+
First, ensure that you have added `has_paper_trail` to your main model and all associated models that are to be tracked.
|
49
45
|
|
50
|
-
|
51
|
-
need to create the `version_associations` table manually, and you will need to
|
52
|
-
ensure that `PaperTrail.config.track_associations = true` is set.
|
46
|
+
To restore associations as they were at the time you must pass any of the following options to the `reify` method.
|
53
47
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
table, together with the `transaction_id` to find the correct version of the
|
58
|
-
association and reify it. The `transaction_id` is a unique id for version records
|
59
|
-
created in the same transaction. It is used to associate the version of the model
|
60
|
-
and the version of the association that are created in the same transaction.
|
48
|
+
- To restore Has-Many and Has-Many-Through associations, use option `has_many: true`
|
49
|
+
- To restore Has-One associations , use option `has_one: true` to `reify`
|
50
|
+
- To restore Belongs-To associations, use option `belongs_to: true`
|
61
51
|
|
62
|
-
|
63
|
-
true` to `reify`. To restore Has-Many and Has-Many-Through associations, use
|
64
|
-
option `has_many: true`. To restore Belongs-To association, use
|
65
|
-
option `belongs_to: true`. For example:
|
52
|
+
For example:
|
66
53
|
|
67
54
|
```ruby
|
68
|
-
|
69
|
-
belongs_to :treasure
|
70
|
-
has_paper_trail
|
71
|
-
end
|
72
|
-
|
73
|
-
class Treasure < ActiveRecord::Base
|
74
|
-
has_one :location
|
75
|
-
has_paper_trail
|
76
|
-
end
|
77
|
-
|
78
|
-
treasure.amount # 100
|
79
|
-
treasure.location.latitude # 12.345
|
80
|
-
|
81
|
-
treasure.update_attributes amount: 153
|
82
|
-
treasure.location.update_attributes latitude: 54.321
|
83
|
-
|
84
|
-
t = treasure.versions.last.reify(has_one: true)
|
85
|
-
t.amount # 100
|
86
|
-
t.location.latitude # 12.345
|
55
|
+
item.versions.last.reify(has_many: true, has_one: true, belongs_to: false)
|
87
56
|
```
|
88
57
|
|
89
|
-
If the parent and child are updated in one go,
|
90
|
-
aforementioned `transaction_id` to reify the models as they were before the
|
91
|
-
transaction (instead of before the update to the model).
|
58
|
+
If the parent and child are updated in one go, it will utilize the aforementioned `transaction_id` to reify the models as they were before the transaction (instead of before the update to the model).
|
92
59
|
|
93
60
|
```ruby
|
94
|
-
|
95
|
-
|
61
|
+
item.amount # 100
|
62
|
+
item.location.latitude # 12.345
|
96
63
|
|
97
|
-
|
98
|
-
|
99
|
-
|
64
|
+
Item.transaction do
|
65
|
+
item.location.update(latitude: 54.321)
|
66
|
+
item.update(amount: 153)
|
100
67
|
end
|
101
68
|
|
102
|
-
t =
|
69
|
+
t = item.versions.last.reify(has_one: true)
|
103
70
|
t.amount # 100
|
104
71
|
t.location.latitude # 12.345, instead of 54.321
|
105
72
|
```
|
106
73
|
|
107
|
-
By default,
|
108
|
-
model if the associated record exists in the live model but did not exist as at
|
109
|
-
the time the version was created. This is usually what you want if you just want
|
110
|
-
to look at the reified version. But if you want to persist it, it would be
|
111
|
-
better to pass in option `mark_for_destruction: true` so that the associated
|
112
|
-
record is included and marked for destruction. Note that `mark_for_destruction`
|
113
|
-
only has [an effect on associations marked with `autosave: true`](http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html#method-i-mark_for_destruction).
|
74
|
+
By default, it excludes an associated record from the reified parent model if the associated record exists in the live model but did not exist as at the time the version was created. This is usually what you want if you just want to look at the reified version. But if you want to persist it, it would be better to pass in option `mark_for_destruction: true` so that the associated record is included and marked for destruction. Note that `mark_for_destruction` only has [an effect on associations marked with `autosave: true`](http://api.rubyonrails.org/classes/ActiveRecord/AutosaveAssociation.html#method-i-mark_for_destruction).
|
114
75
|
|
115
76
|
```ruby
|
116
|
-
class
|
77
|
+
class Product < ActiveRecord::Base
|
117
78
|
has_paper_trail
|
118
|
-
has_one :
|
79
|
+
has_one :photo, autosave: true
|
119
80
|
end
|
120
81
|
|
121
|
-
class
|
82
|
+
class Photo < ActiveRecord::Base
|
122
83
|
has_paper_trail
|
123
|
-
belongs_to :
|
84
|
+
belongs_to :product
|
124
85
|
end
|
125
86
|
|
126
|
-
|
127
|
-
|
128
|
-
|
87
|
+
product = Product.create(name: 'product_0')
|
88
|
+
product.update(name: 'product_1')
|
89
|
+
product.create_photo(name: 'photo')
|
129
90
|
|
130
|
-
|
131
|
-
|
91
|
+
product_0 = product.versions.last.reify(has_one: true)
|
92
|
+
product_0.photo # nil
|
132
93
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
94
|
+
product_0 = product.versions.last.reify(has_one: true, mark_for_destruction: true)
|
95
|
+
product_0.photo.marked_for_destruction? # true
|
96
|
+
product_0.save!
|
97
|
+
product.reload.photo # nil
|
137
98
|
```
|
138
99
|
|
139
|
-
# Known Issues
|
140
|
-
|
141
|
-
Associations have the following known issues, in order of descending importance. Use in Production at your own risk.
|
142
100
|
|
143
|
-
|
101
|
+
# Limitations
|
144
102
|
|
145
|
-
If you
|
146
|
-
|
147
|
-
1.
|
148
|
-
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`
|
149
|
-
- 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]`
|
150
|
-
- When using STI, even if you enable `:warn` you will likely still end up recieving an `ActiveRecord::AssociationTypeMismatch` error.
|
151
|
-
1. Not compatible with [transactional tests](https://github.com/rails/rails/blob/591a0bb87fff7583e01156696fbbf929d48d3e54/activerecord/lib/active_record/fixtures.rb#L142), aka. transactional fixtures. - [PT Issue #542](https://github.com/airblade/paper_trail/issues/542)
|
152
|
-
1. Requires database timestamp columns with fractional second precision.
|
153
|
-
- Sqlite and postgres timestamps have fractional second precision by default.
|
154
|
-
[MySQL timestamps do not](https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html). Furthermore, MySQL 5.5 and earlier do not
|
155
|
-
support fractional second precision at all.
|
156
|
-
- Also, support for fractional seconds in MySQL was not added to
|
157
|
-
rails until ActiveRecord 4.2 (https://github.com/rails/rails/pull/14359).
|
158
|
-
1. PaperTrail-AssociationTracking can't restore an association properly if the association record
|
159
|
-
can be updated to replace its parent model (by replacing the foreign key)
|
160
|
-
1. Currently PaperTrail-AssociationTracking only supports a single `version_associations` table.
|
161
|
-
Therefore, you can only use a single table to store the versions for
|
162
|
-
all related models. Sorry for those who use multiple version tables.
|
163
|
-
1. PaperTrail-AssociationTracking relies on the callbacks on the association model (and the :through
|
164
|
-
association model for Has-Many-Through associations) to record the versions
|
165
|
-
and the relationship between the versions. If the association is changed
|
166
|
-
without invoking the callbacks, Reification won't work. Below are some
|
167
|
-
examples:
|
103
|
+
1. Only reifies the first level of associations. If you want to include nested associations simply add :through relationships to your model.
|
104
|
+
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 :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, Reification won't work. Below are some examples:
|
168
106
|
|
169
107
|
Given these models:
|
170
108
|
|
@@ -192,7 +130,7 @@ If you notice anything here that should be updated/removed/edited feel free to c
|
|
192
130
|
|
193
131
|
```ruby
|
194
132
|
@book.authors << @dostoyevsky
|
195
|
-
@book.authors.create
|
133
|
+
@book.authors.create(name: 'Tolstoy')
|
196
134
|
@book.authorships.last.destroy
|
197
135
|
@book.authorships.clear
|
198
136
|
@book.author_ids = [@solzhenistyn.id, @dostoyevsky.id]
|
@@ -206,39 +144,27 @@ If you notice anything here that should be updated/removed/edited feel free to c
|
|
206
144
|
@book.authors = []
|
207
145
|
```
|
208
146
|
|
209
|
-
Having said that, you can apparently get all these working (I haven't tested it
|
210
|
-
myself) with this patch:
|
211
|
-
|
212
|
-
```ruby
|
213
|
-
# config/initializers/active_record_patch.rb
|
214
|
-
|
215
|
-
class HasManyThroughAssociationPatch
|
216
|
-
def delete_records(records, method)
|
217
|
-
method ||= :destroy
|
218
|
-
super
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
ActiveRecord::Associations::HasManyThroughAssociation.prepend(HasManyThroughAssociationPatch)
|
223
|
-
```
|
224
147
|
|
225
|
-
|
148
|
+
# Known Issues
|
226
149
|
|
150
|
+
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
|
+
- 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 never use STI in any Rails application due to the problems they cause.
|
153
|
+
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.
|
227
154
|
|
228
|
-
### Regarding ActiveRecord Single Table Inheritance (STI)
|
229
155
|
|
230
|
-
|
156
|
+
# Contributing
|
231
157
|
|
232
|
-
|
158
|
+
We use the `appraisal` gem for testing multiple versions of `paper_trail` and `activerecord`. Please use the following steps to test using `appraisal`.
|
233
159
|
|
234
|
-
|
160
|
+
1. `bundle exec appraisal install`
|
161
|
+
2. `bundle exec appraisal rake test`
|
235
162
|
|
236
|
-
See the paper_trail [contribution guidelines](https://github.com/paper-trail-gem/paper_trail/blob/master/.github/CONTRIBUTING.md)
|
237
163
|
|
238
164
|
# Credits
|
239
165
|
|
240
|
-
|
166
|
+
Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
|
241
167
|
|
242
|
-
|
168
|
+
Plugin authored by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
|
243
169
|
|
244
170
|
Associations code originally contributed by Ben Atkins, Jared Beck, Andy Stewart & more
|
data/Rakefile
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
1
|
require "bundler"
|
4
2
|
Bundler::GemHelper.install_tasks
|
5
3
|
|
@@ -20,9 +18,8 @@ Rake::TestTask.new(:test) do |t|
|
|
20
18
|
t.pattern = "test/**/*_test.rb"
|
21
19
|
t.verbose = false
|
22
20
|
|
23
|
-
|
24
|
-
|
25
|
-
# depend on.
|
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.
|
26
23
|
t.warning = false
|
27
24
|
end
|
28
25
|
|
@@ -33,29 +30,5 @@ RSpec::Core::RakeTask.new(:spec) do |t|
|
|
33
30
|
t.verbose = false # hide list of specs bit.ly/1nVq3Jn
|
34
31
|
end
|
35
32
|
|
36
|
-
task :autocorrect do
|
37
|
-
rules = [
|
38
|
-
'FrozenStringLiteralComment',
|
39
|
-
'Layout/EmptyLineAfterMagicComment',
|
40
|
-
'Layout/EmptyLinesAroundBlockBody',
|
41
|
-
'Layout/EmptyLinesAroundClassBody',
|
42
|
-
'Layout/EmptyLinesAroundMethodBody',
|
43
|
-
'Layout/EmptyLinesAroundModuleBody',
|
44
|
-
'Layout/TrailingWhitespace',
|
45
|
-
'Style/EmptyMethod',
|
46
|
-
'Style/TrailingCommaInArguments',
|
47
|
-
]
|
48
|
-
|
49
|
-
rules.each do |rule|
|
50
|
-
`bundle exec rubocop --auto-correct --only #{rule}`
|
51
|
-
end
|
52
|
-
|
53
|
-
Rake::Task['rubocop'].invoke
|
54
|
-
end
|
55
|
-
|
56
|
-
require "rubocop/rake_task"
|
57
|
-
RuboCop::RakeTask.new
|
58
|
-
|
59
|
-
### TODO: Allow rubocop to fail, but still continue
|
60
33
|
desc "Default: run all available test suites"
|
61
34
|
task default: %i[prepare test spec]
|
@@ -46,10 +46,7 @@ module PaperTrailAssociationTracking
|
|
46
46
|
private
|
47
47
|
|
48
48
|
def migration_version
|
49
|
-
|
50
|
-
if major >= 5
|
51
|
-
"[#{major}.#{ActiveRecord::VERSION::MINOR}]"
|
52
|
-
end
|
49
|
+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
53
50
|
end
|
54
51
|
end
|
55
52
|
end
|
@@ -55,10 +55,7 @@ module PaperTrailAssociationTracking
|
|
55
55
|
private
|
56
56
|
|
57
57
|
def migration_version
|
58
|
-
|
59
|
-
if major >= 5
|
60
|
-
"[#{major}.#{ActiveRecord::VERSION::MINOR}]"
|
61
|
-
end
|
58
|
+
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
62
59
|
end
|
63
60
|
end
|
64
61
|
end
|
@@ -3,9 +3,19 @@
|
|
3
3
|
class AddForeignTypeToVersionAssociations < ActiveRecord::Migration<%= migration_version %>
|
4
4
|
def self.up
|
5
5
|
add_column :version_associations, :foreign_type, :string, index: true
|
6
|
+
remove_index :version_associations,
|
7
|
+
name: "index_version_associations_on_foreign_key"
|
8
|
+
add_index :version_associations,
|
9
|
+
%i(foreign_key_name foreign_key_id foreign_type),
|
10
|
+
name: "index_version_associations_on_foreign_key"
|
6
11
|
end
|
7
12
|
|
8
13
|
def self.down
|
14
|
+
remove_index :version_associations,
|
15
|
+
name: "index_version_associations_on_foreign_key"
|
9
16
|
remove_column :version_associations, :foreign_type
|
17
|
+
add_index :version_associations,
|
18
|
+
%i(foreign_key_name foreign_key_id),
|
19
|
+
name: "index_version_associations_on_foreign_key"
|
10
20
|
end
|
11
21
|
end
|
@@ -55,8 +55,7 @@ end
|
|
55
55
|
|
56
56
|
# Require frameworks
|
57
57
|
if defined?(::Rails)
|
58
|
-
# Rails module is sometimes defined by gems like rails-html-sanitizer
|
59
|
-
# so we check for presence of Rails.application.
|
58
|
+
# Rails module is sometimes defined by gems like rails-html-sanitizer so we check for presence of Rails.application.
|
60
59
|
if defined?(::Rails.application)
|
61
60
|
require "paper_trail_association_tracking/frameworks/rails"
|
62
61
|
else
|
@@ -6,33 +6,6 @@ module PaperTrailAssociationTracking
|
|
6
6
|
def transaction?
|
7
7
|
::ActiveRecord::Base.connection.open_transactions.positive?
|
8
8
|
end
|
9
|
-
|
10
|
-
# @deprecated
|
11
|
-
def clear_transaction_id
|
12
|
-
::ActiveSupport::Deprecation.warn(
|
13
|
-
"PaperTrail.clear_transaction_id is deprecated, use PaperTrail.request.clear_transaction_id",
|
14
|
-
caller(1)
|
15
|
-
)
|
16
|
-
request.clear_transaction_id
|
17
|
-
end
|
18
|
-
|
19
|
-
# @deprecated
|
20
|
-
def transaction_id
|
21
|
-
::ActiveSupport::Deprecation.warn(
|
22
|
-
"PaperTrail.transaction_id is deprecated without replacement.",
|
23
|
-
caller(1)
|
24
|
-
)
|
25
|
-
request.transaction_id
|
26
|
-
end
|
27
|
-
|
28
|
-
# @deprecated
|
29
|
-
def transaction_id=(id)
|
30
|
-
::ActiveSupport::Deprecation.warn(
|
31
|
-
"PaperTrail.transaction_id= is deprecated without replacement.",
|
32
|
-
caller(1)
|
33
|
-
)
|
34
|
-
request.transaction_id = id
|
35
|
-
end
|
36
9
|
end
|
37
10
|
end
|
38
11
|
end
|
@@ -78,14 +78,13 @@ module PaperTrailAssociationTracking
|
|
78
78
|
#
|
79
79
|
# @api private
|
80
80
|
# @param klass - An ActiveRecord class.
|
81
|
-
# @param
|
82
|
-
# the IDs of version records.
|
81
|
+
# @param version_ids - Array. The IDs of version records.
|
83
82
|
# @return A `Hash` mapping IDs to `Version`s
|
84
83
|
#
|
85
|
-
def versions_by_id(klass,
|
84
|
+
def versions_by_id(klass, version_ids)
|
86
85
|
klass.
|
87
86
|
paper_trail.version_class.
|
88
|
-
where(
|
87
|
+
where(id: version_ids).
|
89
88
|
inject({}) { |a, e| a.merge!(e.item_id => e) }
|
90
89
|
end
|
91
90
|
|
@@ -95,17 +94,17 @@ module PaperTrailAssociationTracking
|
|
95
94
|
# from the point in time identified by `tx_id` or `version_at`.
|
96
95
|
# @api private
|
97
96
|
def load_versions_for_hm_association(assoc, model, version_table, tx_id, version_at)
|
98
|
-
|
97
|
+
version_ids = ::PaperTrail::VersionAssociation.
|
99
98
|
joins(model.class.version_association_name).
|
100
|
-
select("MIN(version_id)").
|
99
|
+
select("MIN(version_id) as version_id").
|
101
100
|
where("foreign_key_name = ?", assoc.foreign_key).
|
102
101
|
where("foreign_key_id = ?", model.id).
|
103
102
|
where(foreign_type: [model.class.name, nil]).
|
104
103
|
where("#{version_table}.item_type = ?", assoc.klass.base_class.name).
|
105
104
|
where("created_at >= ? OR transaction_id = ?", version_at, tx_id).
|
106
105
|
group("item_id").
|
107
|
-
|
108
|
-
versions_by_id(model.class,
|
106
|
+
map{|e| e.version_id}
|
107
|
+
versions_by_id(model.class, version_ids)
|
109
108
|
end
|
110
109
|
end
|
111
110
|
end
|
@@ -73,8 +73,8 @@ module PaperTrailAssociationTracking
|
|
73
73
|
# `version_at`.
|
74
74
|
# @api private
|
75
75
|
def load_versions_for_hmt_association(assoc, ids, tx_id, version_at)
|
76
|
-
|
77
|
-
select("MIN(id)").
|
76
|
+
version_ids = assoc.klass.paper_trail.version_class.
|
77
|
+
select("MIN(id) as id").
|
78
78
|
where("item_type = ?", assoc.klass.base_class.name).
|
79
79
|
where("item_id IN (?)", ids).
|
80
80
|
where(
|
@@ -83,8 +83,8 @@ module PaperTrailAssociationTracking
|
|
83
83
|
tx_id
|
84
84
|
).
|
85
85
|
group("item_id").
|
86
|
-
|
87
|
-
::PaperTrailAssociationTracking::Reifiers::HasMany.versions_by_id(assoc.klass,
|
86
|
+
map{|e| e.id}
|
87
|
+
::PaperTrailAssociationTracking::Reifiers::HasMany.versions_by_id(assoc.klass, version_ids)
|
88
88
|
end
|
89
89
|
end
|
90
90
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weston Ganger
|
@@ -10,196 +10,162 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-08-14 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: appraisal
|
17
17
|
requirement: !ruby/object:Gem::Requirement
|
18
18
|
requirements:
|
19
|
-
- - "
|
19
|
+
- - ">="
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
21
|
+
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
|
-
- - "
|
26
|
+
- - ">="
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: '
|
28
|
+
version: '0'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: byebug
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
32
32
|
requirements:
|
33
|
-
- - "
|
33
|
+
- - ">="
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: '
|
35
|
+
version: '0'
|
36
36
|
type: :development
|
37
37
|
prerelease: false
|
38
38
|
version_requirements: !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
|
-
- - "
|
40
|
+
- - ">="
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: '
|
42
|
+
version: '0'
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
44
|
name: ffaker
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
46
46
|
requirements:
|
47
|
-
- - "
|
47
|
+
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '
|
49
|
+
version: '0'
|
50
50
|
type: :development
|
51
51
|
prerelease: false
|
52
52
|
version_requirements: !ruby/object:Gem::Requirement
|
53
53
|
requirements:
|
54
|
-
- - "
|
54
|
+
- - ">="
|
55
55
|
- !ruby/object:Gem::Version
|
56
|
-
version: '
|
56
|
+
version: '0'
|
57
57
|
- !ruby/object:Gem::Dependency
|
58
58
|
name: generator_spec
|
59
59
|
requirement: !ruby/object:Gem::Requirement
|
60
60
|
requirements:
|
61
|
-
- - "
|
61
|
+
- - ">="
|
62
62
|
- !ruby/object:Gem::Version
|
63
|
-
version: 0
|
63
|
+
version: '0'
|
64
64
|
type: :development
|
65
65
|
prerelease: false
|
66
66
|
version_requirements: !ruby/object:Gem::Requirement
|
67
67
|
requirements:
|
68
|
-
- - "
|
68
|
+
- - ">="
|
69
69
|
- !ruby/object:Gem::Version
|
70
|
-
version: 0
|
70
|
+
version: '0'
|
71
71
|
- !ruby/object:Gem::Dependency
|
72
72
|
name: mysql2
|
73
73
|
requirement: !ruby/object:Gem::Requirement
|
74
74
|
requirements:
|
75
|
-
- - "
|
75
|
+
- - ">="
|
76
76
|
- !ruby/object:Gem::Version
|
77
|
-
version: 0
|
77
|
+
version: '0'
|
78
78
|
type: :development
|
79
79
|
prerelease: false
|
80
80
|
version_requirements: !ruby/object:Gem::Requirement
|
81
81
|
requirements:
|
82
|
-
- - "
|
82
|
+
- - ">="
|
83
83
|
- !ruby/object:Gem::Version
|
84
|
-
version: 0
|
84
|
+
version: '0'
|
85
85
|
- !ruby/object:Gem::Dependency
|
86
86
|
name: pg
|
87
87
|
requirement: !ruby/object:Gem::Requirement
|
88
88
|
requirements:
|
89
|
-
- - "
|
89
|
+
- - ">="
|
90
90
|
- !ruby/object:Gem::Version
|
91
|
-
version: 0
|
91
|
+
version: '0'
|
92
92
|
type: :development
|
93
93
|
prerelease: false
|
94
94
|
version_requirements: !ruby/object:Gem::Requirement
|
95
95
|
requirements:
|
96
|
-
- - "
|
96
|
+
- - ">="
|
97
97
|
- !ruby/object:Gem::Version
|
98
|
-
version: 0
|
98
|
+
version: '0'
|
99
99
|
- !ruby/object:Gem::Dependency
|
100
100
|
name: rack-test
|
101
101
|
requirement: !ruby/object:Gem::Requirement
|
102
102
|
requirements:
|
103
103
|
- - ">="
|
104
104
|
- !ruby/object:Gem::Version
|
105
|
-
version: 0
|
106
|
-
- - "<"
|
107
|
-
- !ruby/object:Gem::Version
|
108
|
-
version: '0.9'
|
105
|
+
version: '0'
|
109
106
|
type: :development
|
110
107
|
prerelease: false
|
111
108
|
version_requirements: !ruby/object:Gem::Requirement
|
112
109
|
requirements:
|
113
110
|
- - ">="
|
114
111
|
- !ruby/object:Gem::Version
|
115
|
-
version: 0
|
116
|
-
- - "<"
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: '0.9'
|
112
|
+
version: '0'
|
119
113
|
- !ruby/object:Gem::Dependency
|
120
114
|
name: rake
|
121
115
|
requirement: !ruby/object:Gem::Requirement
|
122
116
|
requirements:
|
123
|
-
- - "
|
117
|
+
- - ">="
|
124
118
|
- !ruby/object:Gem::Version
|
125
|
-
version: '
|
119
|
+
version: '0'
|
126
120
|
type: :development
|
127
121
|
prerelease: false
|
128
122
|
version_requirements: !ruby/object:Gem::Requirement
|
129
123
|
requirements:
|
130
|
-
- - "
|
124
|
+
- - ">="
|
131
125
|
- !ruby/object:Gem::Version
|
132
|
-
version: '
|
126
|
+
version: '0'
|
133
127
|
- !ruby/object:Gem::Dependency
|
134
128
|
name: rspec-rails
|
135
129
|
requirement: !ruby/object:Gem::Requirement
|
136
130
|
requirements:
|
137
|
-
- - "
|
138
|
-
- !ruby/object:Gem::Version
|
139
|
-
version: 3.7.2
|
140
|
-
type: :development
|
141
|
-
prerelease: false
|
142
|
-
version_requirements: !ruby/object:Gem::Requirement
|
143
|
-
requirements:
|
144
|
-
- - "~>"
|
145
|
-
- !ruby/object:Gem::Version
|
146
|
-
version: 3.7.2
|
147
|
-
- !ruby/object:Gem::Dependency
|
148
|
-
name: rubocop
|
149
|
-
requirement: !ruby/object:Gem::Requirement
|
150
|
-
requirements:
|
151
|
-
- - "~>"
|
152
|
-
- !ruby/object:Gem::Version
|
153
|
-
version: 0.51.0
|
154
|
-
type: :development
|
155
|
-
prerelease: false
|
156
|
-
version_requirements: !ruby/object:Gem::Requirement
|
157
|
-
requirements:
|
158
|
-
- - "~>"
|
159
|
-
- !ruby/object:Gem::Version
|
160
|
-
version: 0.51.0
|
161
|
-
- !ruby/object:Gem::Dependency
|
162
|
-
name: rubocop-rspec
|
163
|
-
requirement: !ruby/object:Gem::Requirement
|
164
|
-
requirements:
|
165
|
-
- - "~>"
|
131
|
+
- - ">="
|
166
132
|
- !ruby/object:Gem::Version
|
167
|
-
version:
|
133
|
+
version: '0'
|
168
134
|
type: :development
|
169
135
|
prerelease: false
|
170
136
|
version_requirements: !ruby/object:Gem::Requirement
|
171
137
|
requirements:
|
172
|
-
- - "
|
138
|
+
- - ">="
|
173
139
|
- !ruby/object:Gem::Version
|
174
|
-
version:
|
140
|
+
version: '0'
|
175
141
|
- !ruby/object:Gem::Dependency
|
176
142
|
name: sqlite3
|
177
143
|
requirement: !ruby/object:Gem::Requirement
|
178
144
|
requirements:
|
179
|
-
- - "
|
145
|
+
- - ">="
|
180
146
|
- !ruby/object:Gem::Version
|
181
|
-
version: '
|
147
|
+
version: '0'
|
182
148
|
type: :development
|
183
149
|
prerelease: false
|
184
150
|
version_requirements: !ruby/object:Gem::Requirement
|
185
151
|
requirements:
|
186
|
-
- - "
|
152
|
+
- - ">="
|
187
153
|
- !ruby/object:Gem::Version
|
188
|
-
version: '
|
154
|
+
version: '0'
|
189
155
|
- !ruby/object:Gem::Dependency
|
190
156
|
name: timecop
|
191
157
|
requirement: !ruby/object:Gem::Requirement
|
192
158
|
requirements:
|
193
|
-
- - "
|
159
|
+
- - ">="
|
194
160
|
- !ruby/object:Gem::Version
|
195
|
-
version: 0
|
161
|
+
version: '0'
|
196
162
|
type: :development
|
197
163
|
prerelease: false
|
198
164
|
version_requirements: !ruby/object:Gem::Requirement
|
199
165
|
requirements:
|
200
|
-
- - "
|
166
|
+
- - ">="
|
201
167
|
- !ruby/object:Gem::Version
|
202
|
-
version: 0
|
168
|
+
version: '0'
|
203
169
|
description: Plugin for the PaperTrail gem to track and reify associations
|
204
170
|
email: weston@westonganger.com
|
205
171
|
executables: []
|
@@ -252,10 +218,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
252
218
|
requirements:
|
253
219
|
- - ">="
|
254
220
|
- !ruby/object:Gem::Version
|
255
|
-
version:
|
221
|
+
version: '0'
|
256
222
|
requirements: []
|
257
|
-
|
258
|
-
rubygems_version: 2.7.6
|
223
|
+
rubygems_version: 3.1.2
|
259
224
|
signing_key:
|
260
225
|
specification_version: 4
|
261
226
|
summary: Plugin for the PaperTrail gem to track and reify associations
|