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

fix: do not store ignored and skipped attributes in object_changes on destroy #1177

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: 1 addition & 8 deletions lib/paper_trail/events/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,9 @@ def changed_and_not_ignored
(changed_in_latest_version - ignore) - skip
end

# Rails 5.1 changed the API of `ActiveRecord::Dirty`. See
# https://github.com/paper-trail-gem/paper_trail/pull/899
#
# @api private
def changed_in_latest_version
if @in_after_callback && RAILS_GTE_5_1
@record.saved_changes.keys
else
@record.changed
end
changes_in_latest_version.keys
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This avoids duplicating the conditional? Nice!

end

# @api private
Expand Down
11 changes: 8 additions & 3 deletions lib/paper_trail/events/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ def data
data[:object] = recordable_object(false)
end
if record_object_changes?
# Rails' implementation returns nothing on destroy :/
changes = @record.attributes.map { |attr, value| [attr, [value, nil]] }.to_h
data[:object_changes] = prepare_object_changes(changes)
data[:object_changes] = prepare_object_changes(notable_changes)
end
merge_item_subtype_into(data)
merge_metadata_into(data)
end

private

# Rails' implementation returns nothing on destroy :/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice use of polymorphism!

When overriding methods in the future, please add a # @override comment, like this:

      # Rails' implementation returns nothing on destroy :/
      #
      # @override

def changes_in_latest_version
@record.attributes.map { |attr, value| [attr, [value, nil]] }.to_h
end
end
end
end
22 changes: 22 additions & 0 deletions spec/paper_trail/events/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,28 @@ module Events
expect(data[:item_type]).to eq("Family::Family")
expect(data[:item_subtype]).to eq("Family::CelebrityFamily")
end

context "skipper" do
let(:skipper) { Skipper.create!(another_timestamp: Time.now) }
let(:data) { PaperTrail::Events::Destroy.new(skipper, false).data }

it "includes `object` without skipped attributes" do
object = YAML.load(data[:object])
expect(object["id"]).to eq(skipper.id)
expect(object).to have_key("updated_at")
expect(object).to have_key("created_at")
expect(object).not_to have_key("another_timestamp")
end

it "includes `object_changes` without skipped and ignored attributes" do
changes = YAML.load(data[:object_changes])
expect(changes["id"]).to eq([skipper.id, nil])
expect(changes["updated_at"][0]).to be_present
expect(changes["updated_at"][1]).to be_nil
expect(changes).not_to have_key("created_at")
expect(changes).not_to have_key("another_timestamp")
end
end
end
end
end
Expand Down