paper_trail-association_tracking 2.1.0 → 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b2d339d9003d50ddc86c1e1cdea91863bcb2908ce4b4c42db9b59937431a3f0
4
- data.tar.gz: c9638879c3932b3f5c07b549401c60b2c473da0afed2dab078b35deb5b8eae10
3
+ metadata.gz: 1c44254220614ad32a3f634a83c4c3a0d8e3c5aa5dd76521f09f702d03d0be1c
4
+ data.tar.gz: 9434f7aa8c162a2117d7c33890bdbd2df93729a343073e67ebc024537ee773ca
5
5
  SHA512:
6
- metadata.gz: 79f9131a55657ea99ae5393e5c1bb17e00e79744896f99faf70d2df982c2a38a2c06cb17c8bed7131213de814c10c0e96c29f27782ad1bb064db1778649bc6a7
7
- data.tar.gz: 2199d8c642a22494c6f792f809bc7cd7143f287f9fc6a2a8cd3ea31243ab100de9914bc27ef06ebeafd902730b46b7747d2f8a5d41ca6e662b81d2354c140315
6
+ metadata.gz: f5ee8734919969546148c2cb12add0ef77a6e0e0acec5137713974babdc05bb80be5d7321e41b636c74d3b3d4a3463d8e42ddc6b1d5d02f0affed0de0a024fc6
7
+ data.tar.gz: 3205666624b7f94acecca6dc56757e971ba1382b80bb9762c818f7f7935063adebb36e2ab8d3d3d0cb8216b6276042e7eda7f84658cb5e7d696242798503a3b7
@@ -1,17 +1,23 @@
1
1
  # CHANGELOG
2
2
 
3
- ## Unreleased
3
+ ### Unreleased - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.1...master)
4
4
 
5
5
  - Nothing yet
6
6
 
7
- ## 2.1.0 - 2020-08-14
7
+ ### 2.1.1 - 2020-10-21 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.0...v2.1.1)
8
+
9
+ - Bug fix for reify on `has_many :through` relationships when `:source` is specified
10
+ - Bug fix for reify on `has_many :through` relationships where the association is a has_one on the through model
11
+ - Bug fix to ensure install generator will set `PaperTrail.association_tracking = true`
12
+
13
+ ### 2.1.0 - 2020-08-14 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.0.0...v2.1.0)
8
14
 
9
15
  - [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
16
  - [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
17
  - Update test matrix to support multiple versions of PT-core and ActiveRecord
12
18
  - Remove deprecated methods `clear_transaction_id`, `transaction_id` and `transaction_id=`
13
19
 
14
- ## 2.0.0 - 2019-01-22
20
+ ### 2.0.0 - 2019-01-22 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v1.1.1...v2.0.0)
15
21
 
16
22
  - [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
23
  - 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 +26,16 @@
20
26
 
21
27
  - Run `rails g paper_trail_association_tracking:add_foreign_type_to_version_associations` and then migrate your database.
22
28
 
23
- ## 1.1.1 - 2018-01-14
29
+ ### 1.1.1 - 2018-01-14 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v1.1.0...v1.1.1)
24
30
 
25
31
  - Same as v2 release, this is released simply to maintain a working `v1` branch since `v1.1.0` was broken
26
32
 
27
- ## 1.1.0 - 2018-12-28
33
+ ### 1.1.0 - 2018-12-28 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v1.0.0...v1.1.0)
28
34
 
29
35
  - Note: This release is somewhat broken, please upgrade to `v2.0.0` or stay on `v1.0.0`
30
36
  - [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
37
  - [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
38
 
33
- ## 1.0.0 - 2018-06-04
39
+ ### 1.0.0 - 2018-06-04
34
40
 
35
41
  - [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
@@ -55,56 +55,76 @@ For example:
55
55
  item.versions.last.reify(has_many: true, has_one: true, belongs_to: false)
56
56
  ```
57
57
 
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).
58
+ 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
59
 
60
- ```ruby
61
- item.amount # 100
62
- item.location.latitude # 12.345
60
+ For example:
63
61
 
64
- Item.transaction do
65
- item.location.update(latitude: 54.321)
66
- item.update(amount: 153)
62
+ ```ruby
63
+ class Product
64
+ has_many :photos, autosave: true
67
65
  end
68
66
 
69
- t = item.versions.last.reify(has_one: true)
70
- t.amount # 100
71
- t.location.latitude # 12.345, instead of 54.321
67
+ product = Product.first.versions.last.reify(has_many: true, has_one: true, belongs_to: false)
68
+ product.save! ### now this will also save all reified photos
72
69
  ```
73
70
 
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).
71
+ If you do not set `autosave: true` true on the association then you will have to save/delete them manually.
72
+
73
+ For example:
75
74
 
76
75
  ```ruby
77
76
  class Product < ActiveRecord::Base
78
77
  has_paper_trail
79
- has_one :photo, autosave: true
80
- end
81
-
82
- class Photo < ActiveRecord::Base
83
- has_paper_trail
84
- belongs_to :product
78
+ has_many :photos, autosave: false ### or if autosave not set
85
79
  end
86
80
 
87
81
  product = Product.create(name: 'product_0')
88
- product.update(name: 'product_1')
89
- product.create_photo(name: 'photo')
82
+ product.photos.create(name: 'photo')
83
+ product.update(name: 'product_a')
84
+ product.photos.create(name: 'photo')
85
+
86
+ reified_product = product.versions.last.reify(has_many: true, mark_for_destruction: true)
87
+ reified_product.save!
88
+ reified_product.name # product_a
89
+ reified_product.photos.size # 2
90
+ reified_product.photos.reload
91
+ reified_product.photos.size # 1 ### bad, didnt save the associations
92
+
93
+ product = Product.create(name: 'product_1')
94
+ product.update(name: 'product_b')
95
+ product.photos.create(name: 'photo')
96
+
97
+ reified_product = product.versions.last.reify(has_many: true, mark_for_destruction: true)
98
+ reified_product.save!
99
+ reified_product.name # product_b
100
+ reified_product.photos.size # 1
101
+ reified_product.photos.each{|x| x.marked_for_destruction? ? x.destroy! : x.save! }
102
+ reified_product.photos.size # 0
103
+ ```
90
104
 
91
- product_0 = product.versions.last.reify(has_one: true)
92
- product_0.photo # nil
105
+ 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
106
 
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
98
- ```
107
+ For example:
99
108
 
109
+ ```ruby
110
+ item.amount # 100
111
+ item.location.latitude # 12.345
112
+
113
+ Item.transaction do
114
+ item.location.update(latitude: 54.321)
115
+ item.update(amount: 153)
116
+ end
117
+
118
+ t = item.versions.last.reify(has_one: true)
119
+ t.amount # 100
120
+ t.location.latitude # 12.345, instead of 54.321
121
+ ```
100
122
 
101
123
  # Limitations
102
124
 
103
- 1. Only reifies the first level of associations. If you want to include nested associations simply add :through relationships to your model.
125
+ 1. Only reifies the first level of associations. If you want to include nested associations simply add `:through` relationships to your model.
104
126
  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:
106
-
107
- Given these models:
127
+ 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
128
 
109
129
  ```ruby
110
130
  class Book < ActiveRecord::Base
@@ -124,22 +144,16 @@ product.reload.photo # nil
124
144
  has_many :books, through: :authorships
125
145
  has_paper_trail
126
146
  end
127
- ```
128
-
129
- Then each of the following will store authorship versions:
130
-
131
- ```ruby
132
- @book.authors << @dostoyevsky
133
- @book.authors.create(name: 'Tolstoy')
147
+
148
+ ### Each of the following will store authorship versions:
149
+ @book.authors << @john
150
+ @book.authors.create(name: 'Jack')
134
151
  @book.authorships.last.destroy
135
152
  @book.authorships.clear
136
- @book.author_ids = [@solzhenistyn.id, @dostoyevsky.id]
137
- ```
138
-
139
- But none of these will:
140
-
141
- ```ruby
142
- @book.authors.delete @tolstoy
153
+ @book.author_ids = [@john.id, @joe.id]
154
+
155
+ ### But none of these will:
156
+ @book.authors.delete @john
143
157
  @book.author_ids = []
144
158
  @book.authors = []
145
159
  ```
@@ -149,7 +163,7 @@ product.reload.photo # nil
149
163
 
150
164
  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
165
  - 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.
166
+ - 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
167
  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
168
 
155
169
 
@@ -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 = #{!!options.with_associations?}\n",
31
+ "PaperTrail.config.track_associations = true\n",
32
32
  "PaperTrail.config.association_reify_error_behaviour = :error"
33
33
  )
34
34
  end
@@ -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.name.to_sym).to_a
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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PaperTrailAssociationTracking
4
- VERSION = "2.1.0"
4
+ VERSION = "2.1.1".freeze
5
5
  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.1.0
4
+ version: 2.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Weston Ganger
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-08-14 00:00:00.000000000 Z
13
+ date: 2020-10-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appraisal