Merge branch 'master' of https://github.com/stympy/paper_trail into stympy-master

* 'master' of https://github.com/stympy/paper_trail:
  Added :skip option.
This commit is contained in:
Andy Stewart 2011-11-09 10:58:34 +01:00
commit d4e8525702
4 changed files with 36 additions and 2 deletions

View File

@ -40,6 +40,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 :skip
self.skip = ([options[:skip]].flatten.compact || []).map &:to_s
class_attribute :only class_attribute :only
self.only = ([options[:only]].flatten.compact || []).map &:to_s self.only = ([options[:only]].flatten.compact || []).map &:to_s
@ -191,7 +194,7 @@ module PaperTrail
end end
def object_to_string(object) def object_to_string(object)
object.attributes.to_yaml object.attributes.except(*self.class.skip).to_yaml
end end
def changed_notably? def changed_notably?
@ -203,7 +206,7 @@ module PaperTrail
end end
def changed_and_not_ignored def changed_and_not_ignored
changed - self.class.ignore changed - self.class.ignore - self.class.skip
end end
def switched_on? def switched_on?

View File

@ -1,6 +1,7 @@
class Article < ActiveRecord::Base class Article < ActiveRecord::Base
has_paper_trail :ignore => :title, has_paper_trail :ignore => :title,
:only => [:content], :only => [:content],
:skip => [:file_upload],
:meta => {:answer => 42, :meta => {:answer => 42,
:action => :action_data_provider_method, :action => :action_data_provider_method,
:question => Proc.new { "31 + 11 = #{31 + 11}" }, :question => Proc.new { "31 + 11 = #{31 + 11}" },

View File

@ -65,6 +65,7 @@ class SetUpTestTables < ActiveRecord::Migration
t.string :title t.string :title
t.string :content t.string :content
t.string :abstract t.string :abstract
t.string :file_upload
end end
create_table :books, :force => true do |t| create_table :books, :force => true do |t|

View File

@ -28,7 +28,36 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
setup { @article.update_attributes :abstract => 'Other abstract'} setup { @article.update_attributes :abstract => 'Other abstract'}
should_not_change('the number of versions') { Version.count } should_not_change('the number of versions') { Version.count }
end end
context 'which updates a skipped column' do
setup { @article.update_attributes :file_upload => 'Your data goes here' }
should_not_change('the number of versions') { Version.count }
end
context 'which updates a skipped column and a selected column' do
setup { @article.update_attributes :file_upload => 'Your data goes here', :content => 'Some text here.' }
should_change('the number of versions', :by => 1) { Version.count }
should 'have stored only non-skipped attributes' do
assert_equal ({'content' => [nil, 'Some text here.']}), @article.versions.last.changeset
end
context 'and when updated again' do
setup do
@article.update_attributes :file_upload => 'More data goes here', :content => 'More text here.'
@old_article = @article.versions.last
end
should 'have removed the skipped attributes when saving the previous version' do
assert_equal nil, YAML::load(@old_article.object)['file_upload']
end
should 'have kept the non-skipped attributes in the previous version' do
assert_equal 'Some text here.', YAML::load(@old_article.object)['content']
end
end
end
end end