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

clear rolled back versions from associated model #440

Merged
merged 4 commits into from
Nov 11, 2014
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
7 changes: 7 additions & 0 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def has_paper_trail(options = {})
after_update :clear_version_instance!
end
after_destroy :record_destroy, :if => :save_version? if options_on.empty? || options_on.include?(:destroy)
after_rollback :clear_rolled_back_versions
end

# Switches PaperTrail off for this class.
Expand Down Expand Up @@ -180,6 +181,12 @@ def originator
(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

# Returns the object (not a Version) as it was at the given timestamp.
def version_at(timestamp, reify_options={})
# Because a version stores how its object looked *before* the change,
Expand Down
1 change: 1 addition & 0 deletions paper_trail.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec-rails', '~> 3.1.0'
s.add_development_dependency 'generator_spec'
s.add_development_dependency 'database_cleaner', '~> 1.2'
s.add_development_dependency 'test_after_commit', '~> 0.4'

# JRuby support for the test ENV
unless defined?(JRUBY_VERSION)
Expand Down
22 changes: 22 additions & 0 deletions spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@
expect(widget.version).to eq(widget.versions.last)
end
end

describe :after_rollback do
let(:rolled_back_name) { 'Big Moo' }

before do
begin
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
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
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/widget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down