paper_trail 10.2.1 → 10.3.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: 35e835ccf1aa917cfbe1202f639d73f7e70f4f35e4845706f6edb8ba713004b3
4
- data.tar.gz: eafb8a0ed8ddfc6daeafbd7fb8197c225a2a962a1e32f42664802b72f386d510
3
+ metadata.gz: 6a8f2b70e39db7aa92c0afb1c521ae2b04aafb9e7ad3194d4f57badfbcd49efc
4
+ data.tar.gz: d021657cedcbd22cc3aab62a361d9608939dd11ff8a8cd97960d45f568b2ce52
5
5
  SHA512:
6
- metadata.gz: 2c22225eb860afba59822dac469c24bd6aa3fd9712a521a93187ce0ce497a2c66a022c8cb6615d1cfc13fbfc8ac9df386cf4f038ac1ec7fd78cea9611d86f26e
7
- data.tar.gz: efb345a2262034ee6ac5366bc1825bb710b79ed3b80e99567bfe873cc1aca7815e8f5136e066358d542ad52aa777274059c3334d5b6fb56c4fff24ecc4521b65
6
+ metadata.gz: eae7bdfbe1be55038dd29c66700bb953d40db2f628c0c51d454ffb362999e5b29a74cbad15fefa81e95e5b8a407fcfd059ed371427d49130f79952648fa422ba
7
+ data.tar.gz: 85f8d1f8b951b9f52eb231e4a95ae152aa1441604c2bb768bd75ccf8ab8bc057c7d5bda5bfb5ce5c52f4b5b0209dbec1dafc219933f5b9aa920598397e4e4ee8
@@ -11,7 +11,7 @@ class CreateVersions < ActiveRecord::Migration<%= migration_version %>
11
11
  def change
12
12
  create_table :versions<%= versions_table_options %> do |t|
13
13
  t.string :item_type<%= item_type_options %>
14
- t.integer :item_id, null: false
14
+ t.integer :item_id, null: false, limit: 8
15
15
  t.string :event, null: false
16
16
  t.string :whodunnit
17
17
  t.text :object, limit: TEXT_BYTES
@@ -18,6 +18,11 @@ module PaperTrail
18
18
  `abstract_class`. This is fine, but all application models must be
19
19
  configured to use concrete (not abstract) version models.
20
20
  STR
21
+ E_MODEL_LIMIT_REQUIRES_ITEM_SUBTYPE = <<~STR.squish.freeze
22
+ To use PaperTrail's per-model limit in your %s model, you must have an
23
+ item_subtype column in your versions table. See documentation sections
24
+ 2.e.1 Per-model limit, and 4.b.1 The optional item_subtype column.
25
+ STR
21
26
  DPR_PASSING_ASSOC_NAME_DIRECTLY_TO_VERSIONS_OPTION = <<~STR.squish
22
27
  Passing versions association name as `has_paper_trail versions: %{versions_name}`
23
28
  is deprecated. Use `has_paper_trail versions: {name: %{versions_name}}` instead.
@@ -112,6 +117,7 @@ module PaperTrail
112
117
  @model_class.send :include, ::PaperTrail::Model::InstanceMethods
113
118
  setup_options(options)
114
119
  setup_associations(options)
120
+ check_presence_of_item_subtype_column(options)
115
121
  @model_class.after_rollback { paper_trail.clear_rolled_back_versions }
116
122
  setup_callbacks_from_options options[:on]
117
123
  end
@@ -139,6 +145,16 @@ module PaperTrail
139
145
  ::ActiveRecord::Base.belongs_to_required_by_default
140
146
  end
141
147
 
148
+ # Some options require the presence of the `item_subtype` column. Currently
149
+ # only `limit`, but in the future there may be others.
150
+ #
151
+ # @api private
152
+ def check_presence_of_item_subtype_column(options)
153
+ return unless options.key?(:limit)
154
+ return if version_class.item_subtype_column_present?
155
+ raise format(E_MODEL_LIMIT_REQUIRES_ITEM_SUBTYPE, @model_class.name)
156
+ end
157
+
142
158
  def check_version_class_name(options)
143
159
  # @api private - `version_class_name`
144
160
  @model_class.class_attribute :version_class_name
@@ -120,6 +120,7 @@ module PaperTrail
120
120
  # this method returns the constant `Animal`. You can see this particular
121
121
  # example in action in `spec/models/animal_spec.rb`.
122
122
  #
123
+ # TODO: Duplication: similar `constantize` in VersionConcern#version_limit
123
124
  def version_reification_class(version, attrs)
124
125
  inheritance_column_name = version.item_type.constantize.inheritance_column
125
126
  inher_col_value = attrs[inheritance_column_name]
@@ -25,6 +25,10 @@ module PaperTrail
25
25
 
26
26
  # :nodoc:
27
27
  module ClassMethods
28
+ def item_subtype_column_present?
29
+ column_names.include?("item_subtype")
30
+ end
31
+
28
32
  def with_item_keys(item_type, item_id)
29
33
  where item_type: item_type, item_id: item_id
30
34
  end
@@ -329,7 +333,7 @@ module PaperTrail
329
333
  # Enforces the `version_limit`, if set. Default: no limit.
330
334
  # @api private
331
335
  def enforce_version_limit!
332
- limit = PaperTrail.config.version_limit
336
+ limit = version_limit
333
337
  return unless limit.is_a? Numeric
334
338
  previous_versions = sibling_versions.not_creates.
335
339
  order(self.class.timestamp_sort_order("asc"))
@@ -337,5 +341,21 @@ module PaperTrail
337
341
  excess_versions = previous_versions - previous_versions.last(limit)
338
342
  excess_versions.map(&:destroy)
339
343
  end
344
+
345
+ # See docs section 2.e. Limiting the Number of Versions Created.
346
+ # The version limit can be global or per-model.
347
+ #
348
+ # @api private
349
+ #
350
+ # TODO: Duplication: similar `constantize` in Reifier#version_reification_class
351
+ def version_limit
352
+ if self.class.item_subtype_column_present?
353
+ klass = (item_subtype || item_type).constantize
354
+ if klass&.paper_trail_options&.key?(:limit)
355
+ return klass.paper_trail_options[:limit]
356
+ end
357
+ end
358
+ PaperTrail.config.version_limit
359
+ end
340
360
  end
341
361
  end
@@ -8,8 +8,8 @@ module PaperTrail
8
8
  # People are encouraged to use `PaperTrail.gem_version` instead.
9
9
  module VERSION
10
10
  MAJOR = 10
11
- MINOR = 2
12
- TINY = 1
11
+ MINOR = 3
12
+ TINY = 0
13
13
 
14
14
  # Set PRE to nil unless it's a pre-release (beta, rc, etc.)
15
15
  PRE = nil
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paper_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 10.2.1
4
+ version: 10.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Stewart
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-03-14 00:00:00.000000000 Z
13
+ date: 2019-04-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord