Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No need to calculate previous values of skipped attributes #1184

Merged
merged 1 commit into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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