mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
add test for after_rollback
This commit is contained in:
parent
fec6597f77
commit
eaa9809d20
3 changed files with 26 additions and 0 deletions
|
@ -181,6 +181,8 @@ module PaperTrail
|
|||
(source_version || send(self.class.versions_association_name).last).try(:whodunnit)
|
||||
end
|
||||
|
||||
# Invoked after rollbacks to ensure versions records are not created
|
||||
# for changes that never actually took place
|
||||
def clear_rolled_back_versions
|
||||
send(self.class.versions_association_name).reload
|
||||
end
|
||||
|
|
|
@ -85,6 +85,26 @@ describe Widget, :type => :model do
|
|||
expect(widget.version).to eq(widget.versions.last)
|
||||
end
|
||||
end
|
||||
|
||||
describe :after_rollback do
|
||||
let(:rolled_back_name) { 'Big Moo' }
|
||||
|
||||
before do
|
||||
widget.transaction do
|
||||
widget.update_attributes!(name: rolled_back_name)
|
||||
widget.update_attributes!(name: described_class::EXCLUDED_NAME)
|
||||
end
|
||||
rescue ActiveRecord::RecordInvalid
|
||||
widget.name = nil
|
||||
widget.save
|
||||
end
|
||||
|
||||
it 'does not create an event for changes that did not happen' do
|
||||
widget.versions.map(&:changeset).each do |changeset|
|
||||
expect(changeset.fetch('name', [])).to_not include(rolled_back_name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "Association", :versioning => true do
|
||||
|
|
|
@ -2,6 +2,10 @@ class Widget < ActiveRecord::Base
|
|||
has_paper_trail
|
||||
has_one :wotsit
|
||||
|
||||
EXCLUDED_NAME = 'Biglet'
|
||||
|
||||
validates :name, exclusion: { in: [EXCLUDED_NAME] }
|
||||
|
||||
if ::ActiveRecord::VERSION::MAJOR >= 4 # `has_many` syntax for specifying order uses a lambda in Rails 4
|
||||
has_many :fluxors, lambda { order(:name) }
|
||||
else
|
||||
|
|
Loading…
Reference in a new issue