Skip to content

Commit

Permalink
- Add Changelog entry for paper_trail.update_columns
Browse files Browse the repository at this point in the history
- Use a guard in `record_update_columns`
- Use Timecop.freeze so that we can guarantee that an expectation will pass
- Add some comments
  • Loading branch information
TylerRick committed Jan 22, 2018
1 parent 1990fc4 commit 8b8d9a4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).

### Added

- None
- [#1037](https://github.com/airblade/paper_trail/pull/1037) Add `paper_trail.update_columns`

### Fixed

Expand Down
24 changes: 14 additions & 10 deletions lib/paper_trail/record_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,16 @@ def data_for_update
merge_metadata_into(data)
end

# @api private
def record_update_columns(changes)
if enabled?
versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.create(data_for_update_columns(changes))
if version.errors.any?
log_version_errors(version, :update)
else
update_transaction_id(version)
save_associations(version)
end
return unless enabled?
versions_assoc = @record.send(@record.class.versions_association_name)
version = versions_assoc.create(data_for_update_columns(changes))
if version.errors.any?
log_version_errors(version, :update)
else
update_transaction_id(version)
save_associations(version)
end
end

Expand All @@ -323,7 +323,6 @@ def data_for_update_columns(changes)
object: recordable_object,
whodunnit: PaperTrail.whodunnit
}
data[:created_at] = Time.now
if record_object_changes?
data[:object_changes] = recordable_object_changes(changes)
end
Expand Down Expand Up @@ -437,13 +436,18 @@ def touch_with_version(name = nil)

# Like the `update_column` method from `ActiveRecord::Persistence`, but also
# creates a version to record those changes.
# @api public
def update_column(name, value)
update_columns(name => value)
end

# Like the `update_columns` method from `ActiveRecord::Persistence`, but also
# creates a version to record those changes.
# @api public
def update_columns(attributes)
# `@record.update_columns` skips dirty tracking, so we can't just use `@record.changes` or
# @record.saved_changes` from `ActiveModel::Dirty`. We need to build our own hash with the
# changes that will be made directly to the database.
changes = {}
attributes.each do |k, v|
changes[k] = [@record[k], v]
Expand Down
3 changes: 1 addition & 2 deletions spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,8 @@
it "creates a version record" do
widget = Widget.create
expect(widget.versions.count).to eq(1)
Timecop.travel 1.second.since # because MySQL lacks fractional seconds precision
Timecop.freeze Time.now
widget.paper_trail.update_columns(name: "Bugle")
# widget.update_attributes(name: "Bugle")
expect(widget.versions.count).to eq(2)
expect(widget.versions.last.event).to(eq("update"))
expect(widget.versions.last.changeset[:name]).to eq([nil, "Bugle"])
Expand Down

0 comments on commit 8b8d9a4

Please sign in to comment.