No need to calculate previous values of skipped attributes

This commit is contained in:
Rumble Huang 2019-01-29 11:51:43 +08:00 committed by Jared Beck
parent d81cc4c931
commit e255e71756
3 changed files with 19 additions and 7 deletions

View File

@ -59,8 +59,10 @@ module PaperTrail
end
# @api private
def attributes_before_change(is_touch)
Hash[@record.attributes.map do |k, v|
def nonskipped_attributes_before_change(is_touch)
record_attributes = @record.attributes.except(*@record.paper_trail_options[:skip])
Hash[record_attributes.map do |k, v|
if @record.class.column_names.include?(k)
[k, attribute_in_previous_version(k, is_touch)]
else
@ -217,8 +219,7 @@ module PaperTrail
#
# @api private
def object_attrs_for_paper_trail(is_touch)
attrs = attributes_before_change(is_touch).
except(*@record.paper_trail_options[:skip])
attrs = nonskipped_attributes_before_change(is_touch)
AttributeSerializers::ObjectAttribute.new(@record.class).serialize(attrs)
attrs
end

View File

@ -48,6 +48,17 @@ module PaperTrail
end
end
end
describe "#nonskipped_attributes_before_change", versioning: true do
subject { event.send(:nonskipped_attributes_before_change, false) }
let(:event) { PaperTrail::Events::Base.new(skipper, false) }
let(:skipper) { Skipper.create!(another_timestamp: Time.now) }
it do
is_expected.not_to have_key("another_timestamp")
end
end
end
end
end

View File

@ -9,7 +9,7 @@ RSpec.describe(PaperTrail, versioning: true) do
customer = Customer.create(name: "Some text.")
original_attributes = PaperTrail::Events::Base.
new(customer, false).
send(:attributes_before_change, false)
send(:nonskipped_attributes_before_change, false)
customer.update(name: "Some more text.")
expect(customer.versions.length).to(eq(2))
expect(customer.versions[0].reify).to(be_nil)
@ -34,7 +34,7 @@ RSpec.describe(PaperTrail, versioning: true) do
customer = Customer.create(name: "Some text.")
original_attributes = PaperTrail::Events::Base.
new(customer, false).
send(:attributes_before_change, false)
send(:nonskipped_attributes_before_change, false)
customer.update(name: "Some more text.")
expect(customer.versions.length).to(eq(2))
expect(customer.versions[0].reify).to(be_nil)
@ -68,7 +68,7 @@ RSpec.describe(PaperTrail, versioning: true) do
customer = Customer.create
original_attributes = PaperTrail::Events::Base.
new(customer, false).
send(:attributes_before_change, false).
send(:nonskipped_attributes_before_change, false).
reject { |_k, v| v.nil? }
customer.update(name: "Some more text.")
expect(customer.versions.length).to(eq(2))