Skip to content

Commit

Permalink
No need to calculate previous values of skipped attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
khiav223577 authored and jaredbeck committed Feb 4, 2019
1 parent d81cc4c commit e255e71
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
9 changes: 5 additions & 4 deletions lib/paper_trail/events/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ def attribute_changed_in_latest_version?(attr_name)
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
Expand Down Expand Up @@ -217,8 +219,7 @@ def notably_changed
#
# @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
Expand Down
11 changes: 11 additions & 0 deletions spec/paper_trail/events/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ module Events
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
6 changes: 3 additions & 3 deletions spec/paper_trail/serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
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)
Expand All @@ -34,7 +34,7 @@
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)
Expand Down Expand Up @@ -68,7 +68,7 @@
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))
Expand Down

0 comments on commit e255e71

Please sign in to comment.