Skip to content

Commit

Permalink
Merge pull request #1177 from michaelkhabarov/ignore-attributes-in-ob…
Browse files Browse the repository at this point in the history
…ject_changes-on-destroy

fix: do not store ignored and skipped attributes in `object_changes` on destroy
  • Loading branch information
jaredbeck authored Jan 2, 2019
2 parents 05406e8 + ec3e72b commit 10396cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
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
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 :/
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

0 comments on commit 10396cc

Please sign in to comment.