paper_trail-association_tracking 1.0.0 → 1.1.0

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: e55c15952e70d133273bb7e64458a3064124070c64228667ec1e160f6b0213a2
4
- data.tar.gz: 755381e69ab58abb9d7167040ad237f384e2cd7c940c15b72c4d7da9454432a6
3
+ metadata.gz: 844d3ff1eb96a7bae834ad57625e2fb87cf37ebbea76bd489621a31c2a57d6f7
4
+ data.tar.gz: 0cd147df0a5ee52494abcda09e0fc44c10ebae176cc567a3659debb7533ad72c
5
5
  SHA512:
6
- metadata.gz: 80e0609842fb80eed4e07e57e568d79a4721fcd09d510d6b5033338570963b8989026e96c97e5647599b613a1f0639db6d2211612d41898d349ccd4a3de548e3
7
- data.tar.gz: 991d02c97096a303e445f166a49d0110dda689781ef9bd3f3fcb85083b6d79503e6988e6499e1d873e67345a06265ce5695b92ef30b983bc2627d7aaf72fccbb
6
+ metadata.gz: b49a595381d282b2ace65a18405921fba2970a22dde79841b5b7e42d148392b37caac9323136721cbebb9e0f0b74962d333e38883f923a934bb599b54dcf967a
7
+ data.tar.gz: '0180dff00868dfb4e49be3722df2210c630aa1550cf2963714d445f1b0c7d4eadfc96db258bcf88b4554949720821a33f3f00178b2ee4aeb61c52c26238de047'
data/CHANGELOG.md CHANGED
@@ -1,8 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
- This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and
4
- the recommendations of [keepachangelog.com](http://keepachangelog.com/).
3
+ ## Unreleased
5
4
 
6
- ## 1.0.0
5
+ - None
6
+
7
+ ## 1.1.0 - 2018-12-28
8
+
9
+ - [#10](https://github.com/westonganger/paper_trail-association_tracking/pull/9) - The `has_many: true` option now reifies polymorphic associations. Previously they were skipped.
10
+ - [#9](https://github.com/westonganger/paper_trail-association_tracking/pull/9) - The `belongs_to: true` option now reifies polymorphic associations. Previously they were skipped.
11
+
12
+ ## 1.0.0 - 2018-06-04
7
13
 
8
14
  - [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,6 +1,8 @@
1
1
  # PaperTrail-AssociationTracking
2
2
 
3
- [![Build Status][1]][2]
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.org/westonganger/paper_trail-association_tracking' target='_blank'><img height='21' style='border:0px;height:21px;' src='https://api.travis-ci.org/westonganger/paper_trail-association_tracking.svg?branch=master' border='0' alt='Build Status' /></a>
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>
4
6
 
5
7
  Plugin for the [PaperTrail](https://github.com/paper-trail-gem/paper_trail.git) gem to track and reify associations.
6
8
 
@@ -43,9 +45,8 @@ Has-Many-Through. In order to do this, you will need to do two things:
43
45
  1. Create a `version_associations` table
44
46
  2. Set `PaperTrail.config.track_associations = true` (e.g. in an initializer)
45
47
 
46
- Both will be done for you automatically if you install PaperTrail with the
47
- `--with_associations` option
48
- (e.g. `rails generate paper_trail:install --with-associations`)
48
+
49
+ Both will be done for you automatically if you run the PaperTrail-AssociationTracking generator (e.g. `rails generate paper_trail_association_tracking:install`)
49
50
 
50
51
  If you want to add this functionality after the initial installation, you will
51
52
  need to create the `version_associations` table manually, and you will need to
@@ -242,6 +243,3 @@ Plugin authored by [Weston Ganger](https://github.com/westonganger) & Jared Beck
242
243
  Maintained by [Weston Ganger](https://github.com/westonganger) & [Jared Beck](https://github.com/jaredbeck)
243
244
 
244
245
  Associations code originally contributed by Ben Atkins, Jared Beck, Andy Stewart & more
245
-
246
- [1]: https://api.travis-ci.org/westonganger/paper_trail-association_tracking.svg?branch=master
247
- [2]: https://travis-ci.org/westonganger/paper_trail-association_tracking
@@ -6,10 +6,11 @@ class CreateVersionAssociations < ActiveRecord::Migration<%= migration_version %
6
6
  t.integer :version_id
7
7
  t.string :foreign_key_name, null: false
8
8
  t.integer :foreign_key_id
9
+ t.string :foreign_type, null: false
9
10
  end
10
11
  add_index :version_associations, [:version_id]
11
12
  add_index :version_associations,
12
- %i(foreign_key_name foreign_key_id),
13
+ %i(foreign_key_name foreign_key_id foreign_type),
13
14
  name: "index_version_associations_on_foreign_key"
14
15
  end
15
16
 
@@ -136,7 +136,8 @@ module PaperTrailAssociationTracking
136
136
  ::PaperTrail::VersionAssociation.create(
137
137
  version_id: version.transaction_id,
138
138
  foreign_key_name: a.name,
139
- foreign_key_id: id
139
+ foreign_key_id: id,
140
+ foreign_type: a.klass
140
141
  )
141
142
  end
142
143
  end
@@ -168,12 +169,14 @@ module PaperTrailAssociationTracking
168
169
  }
169
170
 
170
171
  if assoc.options[:polymorphic]
171
- associated_record = @record.send(assoc.name) if @record.send(assoc.foreign_type)
172
- if associated_record && ::PaperTrail.request.enabled_for_model?(associated_record.class)
173
- assoc_version_args[:foreign_key_id] = associated_record.id
172
+ foreign_type = @record.send(assoc.foreign_type)
173
+ if foreign_type && ::PaperTrail.request.enabled_for_model?(foreign_type.constantize)
174
+ assoc_version_args[:foreign_key_id] = @record.send(assoc.foreign_key)
175
+ assoc_version_args[:foreign_type] = foreign_type
174
176
  end
175
177
  elsif ::PaperTrail.request.enabled_for_model?(assoc.klass)
176
178
  assoc_version_args[:foreign_key_id] = @record.send(assoc.foreign_key)
179
+ assoc_version_args[:foreign_type] = assoc.klass
177
180
  end
178
181
 
179
182
  if assoc_version_args.key?(:foreign_key_id)
@@ -50,9 +50,11 @@ module PaperTrailAssociationTracking
50
50
  end
51
51
 
52
52
  # @api private
53
- def each_enabled_association(associations)
53
+ def each_enabled_association(associations, model)
54
54
  associations.each do |assoc|
55
- next unless ::PaperTrail.request.enabled_for_model?(assoc.klass)
55
+ assoc_klass = assoc.polymorphic? ?
56
+ model.send(assoc.foreign_type).constantize : assoc.klass
57
+ next unless ::PaperTrail.request.enabled_for_model?(assoc_klass)
56
58
  yield assoc
57
59
  end
58
60
  end
@@ -79,7 +81,7 @@ module PaperTrailAssociationTracking
79
81
  # @api private
80
82
  def reify_has_one_associations(transaction_id, model, options = {})
81
83
  associations = model.class.reflect_on_all_associations(:has_one)
82
- each_enabled_association(associations) do |assoc|
84
+ each_enabled_association(associations, model) do |assoc|
83
85
  ::PaperTrailAssociationTracking::Reifiers::HasOne.reify(assoc, model, options, transaction_id)
84
86
  end
85
87
  end
@@ -88,7 +90,7 @@ module PaperTrailAssociationTracking
88
90
  # @api private
89
91
  def reify_belongs_to_associations(transaction_id, model, options = {})
90
92
  associations = model.class.reflect_on_all_associations(:belongs_to)
91
- each_enabled_association(associations) do |assoc|
93
+ each_enabled_association(associations, model) do |assoc|
92
94
  ::PaperTrailAssociationTracking::Reifiers::BelongsTo.reify(assoc, model, options, transaction_id)
93
95
  end
94
96
  end
@@ -97,7 +99,7 @@ module PaperTrailAssociationTracking
97
99
  # @api private
98
100
  def reify_has_many_associations(transaction_id, associations, model, options = {})
99
101
  version_table_name = model.class.paper_trail.version_class.table_name
100
- each_enabled_association(associations) do |assoc|
102
+ each_enabled_association(associations, model) do |assoc|
101
103
  ::PaperTrailAssociationTracking::Reifiers::HasMany.reify(assoc, model, options, transaction_id, version_table_name)
102
104
  end
103
105
  end
@@ -106,7 +108,7 @@ module PaperTrailAssociationTracking
106
108
  # direct (non-`through`) has_manys have been reified.
107
109
  # @api private
108
110
  def reify_has_many_through_associations(transaction_id, associations, model, options = {})
109
- each_enabled_association(associations) do |assoc|
111
+ each_enabled_association(associations, model) do |assoc|
110
112
  ::PaperTrailAssociationTracking::Reifiers::HasManyThrough.reify(assoc, model, options, transaction_id)
111
113
  end
112
114
  end
@@ -9,8 +9,10 @@ module PaperTrailAssociationTracking
9
9
  # @api private
10
10
  def reify(assoc, model, options, transaction_id)
11
11
  id = model.send(assoc.foreign_key)
12
- version = load_version(assoc, id, transaction_id, options[:version_at])
13
- record = load_record(assoc, id, options, version)
12
+ klass = assoc.polymorphic? ?
13
+ model.send(assoc.foreign_type).constantize : assoc.klass
14
+ version = load_version(klass, id, transaction_id, options[:version_at])
15
+ record = load_record(klass, id, options, version)
14
16
  model.send("#{assoc.name}=".to_sym, record)
15
17
  end
16
18
 
@@ -19,9 +21,9 @@ module PaperTrailAssociationTracking
19
21
  # Given a `belongs_to` association and a `version`, return a record that
20
22
  # can be assigned in order to reify that association.
21
23
  # @api private
22
- def load_record(assoc, id, options, version)
24
+ def load_record(assoc_klass, id, options, version)
23
25
  if version.nil?
24
- assoc.klass.where(assoc.klass.primary_key => id).first
26
+ assoc_klass.where(assoc_klass.primary_key => id).first
25
27
  else
26
28
  version.reify(
27
29
  options.merge(
@@ -37,9 +39,9 @@ module PaperTrailAssociationTracking
37
39
  # Given a `belongs_to` association and an `id`, return a version record
38
40
  # from the point in time identified by `transaction_id` or `version_at`.
39
41
  # @api private
40
- def load_version(assoc, id, transaction_id, version_at)
41
- assoc.klass.paper_trail.version_class.
42
- where("item_type = ?", assoc.klass.base_class.name).
42
+ def load_version(assoc_klass, id, transaction_id, version_at)
43
+ assoc_klass.paper_trail.version_class.
44
+ where("item_type = ?", assoc_klass.base_class.name).
43
45
  where("item_id = ?", id).
44
46
  where("created_at >= ? OR transaction_id = ?", version_at, transaction_id).
45
47
  order("id").limit(1).first
@@ -100,6 +100,7 @@ module PaperTrailAssociationTracking
100
100
  select("MIN(version_id)").
101
101
  where("foreign_key_name = ?", assoc.foreign_key).
102
102
  where("foreign_key_id = ?", model.id).
103
+ where("foreign_type = ?", model.class.name).
103
104
  where("#{version_table}.item_type = ?", assoc.klass.base_class.name).
104
105
  where("created_at >= ? OR transaction_id = ?", version_at, tx_id).
105
106
  group("item_id").
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PaperTrailAssociationTracking
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
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: 1.0.0
4
+ version: 1.1.0
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: 2018-06-05 00:00:00.000000000 Z
13
+ date: 2018-12-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: appraisal
@@ -252,8 +252,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
252
252
  - !ruby/object:Gem::Version
253
253
  version: 1.3.6
254
254
  requirements: []
255
- rubyforge_project:
256
- rubygems_version: 2.7.6
255
+ rubygems_version: 3.0.1
257
256
  signing_key:
258
257
  specification_version: 4
259
258
  summary: Plugin for the PaperTrail gem to track and reify associations