has_paper_trail ":ignore_if" parameter

This commit is contained in:
Nikita Cernovs 2012-01-11 16:26:10 +02:00
parent 02d3bce542
commit 183ba3e7ec
5 changed files with 54 additions and 3 deletions

View File

@ -179,6 +179,15 @@ You can choose which events to track with the `on` option. For example, to igno
end
## Choosing When Not To Save New Versions
You can choose the condition when not to add new versions with the `ignore_if` option. For example, to ignore non-US translations:
class Translation < ActiveRecord::Base
has_paper_trail :ignore_if => Proc.new { |t| t.language_code != 'US' }
end
## Choosing Attributes To Monitor

View File

@ -43,6 +43,9 @@ module PaperTrail
class_attribute :ignore
self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
class_attribute :ignore_condition
self.ignore_condition = options[:ignore_if]
class_attribute :skip
self.skip = ([options[:skip]].flatten.compact || []).map &:to_s
@ -124,6 +127,10 @@ module PaperTrail
self.class.paper_trail_on if paper_trail_was_enabled
end
def prevent_versioning?
ignore_condition && ignore_condition.call(self)
end
private
def version_class
@ -135,13 +142,13 @@ module PaperTrail
end
def record_create
if switched_on?
if switched_on? && !prevent_versioning?
send(self.class.versions_association_name).create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
end
end
def record_update
if switched_on? && changed_notably?
if switched_on? && changed_notably? && !prevent_versioning?
data = {
:event => 'update',
:object => object_to_string(item_before_change),

View File

@ -0,0 +1,3 @@
class Translation < ActiveRecord::Base
has_paper_trail :ignore_if => Proc.new { |t| t.language_code != 'US' }
end

View File

@ -103,7 +103,12 @@ class SetUpTestTables < ActiveRecord::Migration
t.string :name
t.integer :version
end
create_table :translations, :force => true do |t|
t.string :headline
t.string :content
t.string :language_code
end
end
def self.down
@ -123,5 +128,6 @@ class SetUpTestTables < ActiveRecord::Migration
drop_table :widgets
drop_table :documents
drop_table :legacy_widgets
drop_table :translations
end
end

View File

@ -68,6 +68,32 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
end
end
context 'A record with defined "ignore_if" attribute' do
setup { @translation = Translation.new :headline => 'Headline' }
context 'for non-US translations' do
setup { @translation.save }
should_not_change('the number of versions') { Version.count }
context 'after update' do
setup { @translation.update_attributes :content => 'Content' }
should_not_change('the number of versions') { Version.count }
end
end
context 'for US translations' do
setup do
@translation.language_code = "US"
@translation.save
end
should_change('the number of versions', :by => 1) { Version.count }
context 'after update' do
setup { @translation.update_attributes :content => 'Content' }
should_change('the number of versions', :by => 1) { Version.count }
end
end
end
context 'A new record' do
setup { @widget = Widget.new }