paper_trail-association_tracking 2.1.1 → 2.2.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: 1c44254220614ad32a3f634a83c4c3a0d8e3c5aa5dd76521f09f702d03d0be1c
4
- data.tar.gz: 9434f7aa8c162a2117d7c33890bdbd2df93729a343073e67ebc024537ee773ca
3
+ metadata.gz: 3b49b412d6cd431cd4310dadde716d334231225fed80e84c5d4d8696f42fcb0f
4
+ data.tar.gz: a77500b35d0f66a1840272b8efd9891ce876da341756fe47077cd1257e4f26b2
5
5
  SHA512:
6
- metadata.gz: f5ee8734919969546148c2cb12add0ef77a6e0e0acec5137713974babdc05bb80be5d7321e41b636c74d3b3d4a3463d8e42ddc6b1d5d02f0affed0de0a024fc6
7
- data.tar.gz: 3205666624b7f94acecca6dc56757e971ba1382b80bb9762c818f7f7935063adebb36e2ab8d3d3d0cb8216b6276042e7eda7f84658cb5e7d696242798503a3b7
6
+ metadata.gz: dff6c0eacc9cf46bb6ed00edbed900426ac3b842b7e5046bb9511422b44d78bb88782d7ffc4c4322c40e28426ee0cc2c3c4fdcd7630f0c0ca4b294dabaa9f815
7
+ data.tar.gz: 41c42f324a608af7c22d7b442827eedf15f4d6c79e0c023eb93adc99f177076a95b84c7dc18acdf8f8a3f0d3a9726378d542d18f02b8bc92a3a18092d9e792d2
data/CHANGELOG.md CHANGED
@@ -1,8 +1,24 @@
1
1
  # CHANGELOG
2
2
 
3
- ### Unreleased - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.1...master)
3
+ ### Unreleased - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.2.1...master)
4
+ - Nothing yet
4
5
 
5
- - Nothing yet
6
+ ### v2.2.1 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.2.0...v2.2.1)
7
+ - [PR #38](https://github.com/westonganger/paper_trail-association_tracking/pull/38) - Fix the issue where reifying has_one association with `dependent: :destroy` could destroy a live record
8
+
9
+ ### v2.2.0 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.3...v2.2.0)
10
+
11
+ - [PR #36](https://github.com/westonganger/paper_trail-association_tracking/pull/36) - Fix load order for paper_trail v12+
12
+ - Drop support for Ruby 2.5
13
+ - Add Github Actions CI supporting multiple version of Ruby, Rails and multiple databases types
14
+
15
+ ### 2.1.3 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.1...v2.1.3)
16
+
17
+ - [PR #24](https://github.com/westonganger/paper_trail-association_tracking/pull/24) - Fix reification on STI models that have parent child relationships
18
+
19
+ ### 2.1.2
20
+
21
+ - A late night oopsies, Release yanked immediately, had bug preventing installation.
6
22
 
7
23
  ### 2.1.1 - 2020-10-21 - [View Diff](https://github.com/westonganger/paper_trail-association_tracking/compare/v2.1.0...v2.1.1)
8
24
 
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://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>
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,14 +23,13 @@ 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
- # Gemfile
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
 
@@ -182,3 +181,9 @@ Maintained by [Weston Ganger](https://westonganger.com) - [@westonganger](https:
182
181
  Plugin authored by [Weston Ganger](https://westonganger.com) - [@westonganger](https://github.com/westonganger)
183
182
 
184
183
  Associations code originally contributed by Ben Atkins, Jared Beck, Andy Stewart & more
184
+
185
+ # Alternative Solution
186
+
187
+ 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.
188
+
189
+ 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 bit.ly/1nVq3Jn
27
+ t.verbose = false # hide list of specs
31
28
  end
32
29
 
33
30
  desc "Default: run all available test suites"
@@ -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
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "paper_trail_association_tracking/frameworks/rails/engine"
3
+ require "paper_trail_association_tracking/frameworks/rails/railtie"
@@ -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(assoc)
42
- assoc_name = assoc.name
43
- %w[add remove].each do |verb|
44
- @model_class.send(:"before_#{verb}_for_#{assoc_name}").send(
45
- :<<,
46
- lambda do |*args|
47
- update_habtm_state(assoc_name, :"before_#{verb}", args[-2], args.last)
48
- end
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: [model.class.name, nil]).
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").
@@ -56,9 +56,7 @@ module PaperTrailAssociationTracking
56
56
  if options[:mark_for_destruction]
57
57
  model.send(assoc.name).mark_for_destruction if model.send(assoc.name, true)
58
58
  else
59
- model.paper_trail.appear_as_new_record do
60
- model.send "#{assoc.name}=", nil
61
- end
59
+ model.association(assoc.name).target = nil
62
60
  end
63
61
  end
64
62
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PaperTrailAssociationTracking
4
- VERSION = "2.1.1".freeze
4
+ VERSION = "2.2.1".freeze
5
5
  end
@@ -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(:within_transaction, ->(id) { where transaction_id: id })
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.1.1
4
+ version: 2.2.1
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: 2020-10-22 00:00:00.000000000 Z
13
+ date: 2022-03-29 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: appraisal
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: :development
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: ffaker
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: generator_spec
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: mysql2
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: pg
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/engine.rb
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.3.0
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.1.2
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