1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00

Fixes touch_with_version re: the :on option

Fixes a bug where e.g. `on: [:create, :destroy]` (not :update)
failed to create a version.
This commit is contained in:
Jared Beck 2015-07-08 23:21:44 -04:00
parent 3d6d796fea
commit 0fd007cff9
5 changed files with 42 additions and 6 deletions

View file

@ -266,8 +266,9 @@ module PaperTrail
PaperTrail.whodunnit = current_whodunnit
end
# Mimicks behavior of `touch` method from `ActiveRecord::Persistence`,
# but generates a version
# Mimics the `touch` method from `ActiveRecord::Persistence`, but also
# creates a version. A version is created regardless of options such as
# `:on`, `:if`, or `:unless`.
#
# TODO: look into leveraging the `after_touch` callback from
# `ActiveRecord` to allow the regular `touch` method go generate a version
@ -281,13 +282,20 @@ module PaperTrail
current_time = current_time_from_proper_timezone
attributes.each { |column| write_attribute(column, current_time) }
# ensure a version is written even if the `:on` collection is empty
record_update(true) if paper_trail_options[:on] == []
record_update(true) unless will_record_after_update?
save!(:validate => false)
end
private
# Returns true if `save` will cause `record_update`
# to be called via the `after_update` callback.
def will_record_after_update?
on = paper_trail_options[:on]
on.nil? || on.include?(:update)
end
def source_version
send self.class.version_association_name
end

View file

@ -0,0 +1,19 @@
require 'rails_helper'
describe NotOnUpdate, :type => :model do
describe "#touch_with_version", :versioning => true do
let!(:record) { described_class.create! }
it "should create a version, regardless" do
expect { record.touch_with_version }.to change {
PaperTrail::Version.count
}.by(+1)
end
it "increments the `:updated_at` timestamp" do
before = record.updated_at
record.touch_with_version
expect(record.updated_at).to be > before
end
end
end

View file

@ -252,13 +252,13 @@ describe Widget, :type => :model do
describe '#touch_with_version' do
it { is_expected.to respond_to(:touch_with_version) }
it "should generate a version" do
it "creates a version" do
count = widget.versions.size
widget.touch_with_version
expect(widget.versions.size).to eq(count + 1)
end
it "should increment the `:updated_at` timestamp" do
it "increments the `:updated_at` timestamp" do
time_was = widget.updated_at
widget.touch_with_version
expect(widget.updated_at).to be > time_was

View file

@ -0,0 +1,4 @@
# This model does not record versions when updated.
class NotOnUpdate < ActiveRecord::Base
has_paper_trail :on => [:create, :destroy]
end

View file

@ -73,6 +73,10 @@ class SetUpTestTables < ActiveRecord::Migration
add_index :json_versions, [:item_type, :item_id]
end
create_table :not_on_updates, :force => true do |t|
t.timestamps :null => true
end
create_table :bananas, :force => true do |t|
t.timestamps :null => true
end
@ -205,6 +209,7 @@ class SetUpTestTables < ActiveRecord::Migration
def self.down
drop_table :animals
drop_table :not_on_updates
drop_table :posts
drop_table :songs
drop_table :editors