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

Added in_after_callback argument to save_with_version #1371

Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

## Unreleased

- Added `in_after_callback` argument to `PaperTrail::RecordTrail#save_with_version`,
to allow the caller to indicate if this method is being called during an `after`
callback. Defaults to `false`.

### Breaking Changes

- None
Expand Down
8 changes: 5 additions & 3 deletions lib/paper_trail/record_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,17 @@ def source_version
# Save, and create a version record regardless of options such as `:on`,
# `:if`, or `:unless`.
#
# Arguments are passed to `save`.
# `in_after_callback`: Indicates if this method is being called within an
# `after` callback. Defaults to `false`.
# `options`: Optional arguments passed to `save`.
#
# This is an "update" event. That is, we record the same data we would in
# the case of a normal AR `update`.
def save_with_version(**options)
def save_with_version(in_after_callback: false, **options)
::PaperTrail.request(enabled: false) do
@record.save(**options)
end
record_update(force: true, in_after_callback: false, is_touch: false)
record_update(force: true, in_after_callback: in_after_callback, is_touch: false)
end

# Like the `update_column` method from `ActiveRecord::Persistence`, but also
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def up
end

create_table :not_on_updates, force: true do |t|
t.string :name
t.timestamps null: true, limit: 6
end

Expand Down
7 changes: 7 additions & 0 deletions spec/models/not_on_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@
PaperTrail::Version.count
}.by(+1)
end

it "captures changes when in_after_callback is true" do
record.name = "test"
record.paper_trail.save_with_version(in_after_callback: true)
changeset = record.versions.last.changeset
expect(changeset[:name]).to eq([nil, "test"])
end
end
end