mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
has_paper_trail ":ignore_if" parameter
This commit is contained in:
parent
02d3bce542
commit
183ba3e7ec
5 changed files with 54 additions and 3 deletions
|
@ -179,6 +179,15 @@ You can choose which events to track with the `on` option. For example, to igno
|
||||||
end
|
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
|
## Choosing Attributes To Monitor
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,9 @@ module PaperTrail
|
||||||
class_attribute :ignore
|
class_attribute :ignore
|
||||||
self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
|
self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s
|
||||||
|
|
||||||
|
class_attribute :ignore_condition
|
||||||
|
self.ignore_condition = options[:ignore_if]
|
||||||
|
|
||||||
class_attribute :skip
|
class_attribute :skip
|
||||||
self.skip = ([options[:skip]].flatten.compact || []).map &:to_s
|
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
|
self.class.paper_trail_on if paper_trail_was_enabled
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prevent_versioning?
|
||||||
|
ignore_condition && ignore_condition.call(self)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def version_class
|
def version_class
|
||||||
|
@ -135,13 +142,13 @@ module PaperTrail
|
||||||
end
|
end
|
||||||
|
|
||||||
def record_create
|
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)
|
send(self.class.versions_association_name).create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def record_update
|
def record_update
|
||||||
if switched_on? && changed_notably?
|
if switched_on? && changed_notably? && !prevent_versioning?
|
||||||
data = {
|
data = {
|
||||||
:event => 'update',
|
:event => 'update',
|
||||||
:object => object_to_string(item_before_change),
|
:object => object_to_string(item_before_change),
|
||||||
|
|
3
test/dummy/app/models/translation.rb
Normal file
3
test/dummy/app/models/translation.rb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
class Translation < ActiveRecord::Base
|
||||||
|
has_paper_trail :ignore_if => Proc.new { |t| t.language_code != 'US' }
|
||||||
|
end
|
|
@ -103,7 +103,12 @@ class SetUpTestTables < ActiveRecord::Migration
|
||||||
t.string :name
|
t.string :name
|
||||||
t.integer :version
|
t.integer :version
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table :translations, :force => true do |t|
|
||||||
|
t.string :headline
|
||||||
|
t.string :content
|
||||||
|
t.string :language_code
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.down
|
def self.down
|
||||||
|
@ -123,5 +128,6 @@ class SetUpTestTables < ActiveRecord::Migration
|
||||||
drop_table :widgets
|
drop_table :widgets
|
||||||
drop_table :documents
|
drop_table :documents
|
||||||
drop_table :legacy_widgets
|
drop_table :legacy_widgets
|
||||||
|
drop_table :translations
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -68,6 +68,32 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
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
|
context 'A new record' do
|
||||||
setup { @widget = Widget.new }
|
setup { @widget = Widget.new }
|
||||||
|
|
Loading…
Reference in a new issue