1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Skip creating version for timestamp when changed attributed is ignored via Hash

- It was only ignoring attributes defined as symbols.
- It now ignores when attributes are either defined as symbols or Hashes.
- Consolidates calculation to be shared when determining if changed and not ignored.

Resolves #1240
This commit is contained in:
Todd Lynam 2020-08-16 10:15:09 -10:00
parent 8f73c1540e
commit f7a94d0b63
5 changed files with 28 additions and 7 deletions

View file

@ -27,6 +27,9 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
- [#1238](https://github.com/paper-trail-gem/paper_trail/pull/1238) -
Query optimization in `reify`
- [#1256](https://github.com/paper-trail-gem/paper_trail/pull/1256) -
Skip version for timestamp when changed attributed is ignored via Hash
### Dependencies
- Drop support for rails <= 5.1 (reached EOL when 6.0 was released,

View file

@ -107,7 +107,7 @@ module PaperTrail
end
# @api private
def changed_and_not_ignored
def calculated_ignored_array
ignore = @record.paper_trail_options[:ignore].dup
# Remove Hash arguments and then evaluate whether the attributes (the
# keys of the hash) should also get pushed into the collection.
@ -117,8 +117,12 @@ module PaperTrail
ignore << attr if condition.respond_to?(:call) && condition.call(@record)
}
end
end
# @api private
def changed_and_not_ignored
skip = @record.paper_trail_options[:skip]
(changed_in_latest_version - ignore) - skip
(changed_in_latest_version - calculated_ignored_array) - skip
end
# @api private
@ -148,7 +152,7 @@ module PaperTrail
#
# @api private
def ignored_attr_has_changed?
ignored = @record.paper_trail_options[:ignore] + @record.paper_trail_options[:skip]
ignored = calculated_ignored_array + @record.paper_trail_options[:skip]
ignored.any? && (changed_in_latest_version & ignored).any?
end

View file

@ -1,5 +1,5 @@
# frozen_string_literal: true
class Gadget < ActiveRecord::Base
has_paper_trail ignore: :brand
has_paper_trail ignore: [:brand, { color: proc { |obj| obj.color == "Yellow" } }]
end

View file

@ -254,6 +254,7 @@ class SetUpTestTables < ::ActiveRecord::Migration::Current
create_table :gadgets, force: true do |t|
t.string :name
t.string :brand
t.string :color
t.timestamps null: true, limit: 6
end

View file

@ -8,12 +8,25 @@ RSpec.describe Gadget, type: :model do
it { is_expected.to be_versioned }
describe "updates", versioning: true do
it "generates a version for updates to `name` attribute" do
it "generates a version for updates" do
expect { gadget.update_attribute(:name, "Hammer") }.to(change { gadget.versions.size }.by(1))
end
it "ignores for updates to `brand` attribute" do
expect { gadget.update_attribute(:brand, "Stanley") }.not_to(change { gadget.versions.size })
context "ignored via symbol" do
it "doesn't generate a version" do
expect { gadget.update_attribute(:brand, "Picard") }.not_to(change { gadget.versions.size })
end
end
context "ignored via Hash" do
it "generates a version when the ignored attribute isn't true" do
expect { gadget.update_attribute(:color, "Blue") }.to(change { gadget.versions.size }.by(1))
expect(gadget.versions.last.changeset.keys).to eq %w[color updated_at]
end
it "doesn't generate a version when the ignored attribute is true" do
expect { gadget.update_attribute(:color, "Yellow") }.not_to(change { gadget.versions.size })
end
end
it "still generates a version when only the `updated_at` attribute is updated" do