Fix PaperTrail::Model::InstanceMethods#changed_notably? to return proper values when attrs are ignored

This commit is contained in:
Ben Atkins 2014-10-06 19:13:39 -04:00
parent 0dcdb32a44
commit 049a86f1ab
5 changed files with 37 additions and 2 deletions

View File

@ -367,7 +367,11 @@ module PaperTrail
end
def changed_notably?
notably_changed.any?
if self.paper_trail_options[:ignore].any? && (changed & self.paper_trail_options[:ignore]).any?
(notably_changed - timestamp_attributes_for_update_in_model.map(&:to_s)).any?
else
notably_changed.any?
end
end
def notably_changed

View File

@ -0,0 +1,21 @@
require 'spec_helper'
describe Gadget do
it { should be_versioned }
let(:gadget) { Gadget.create!(name: 'Wrench', brand: 'Acme') }
describe "updates", :versioning => true do
it "should generate a version for updates to `name` attribute" do
expect { gadget.update_attribute(:name, 'Hammer').to change{gadget.versions.size}.by(1) }
end
it "should ignore for updates to `brand` attribute" do
expect { gadget.update_attribute(:brand, 'Stanley') }.to_not change{gadget.versions.size}
end
it "should still generate a version when only the `updated_at` attribute is updated" do
expect { gadget.update_attribute(:updated_at, Time.now) }.to change{gadget.versions.size}.by(1)
end
end
end

View File

@ -0,0 +1,3 @@
class Gadget < ActiveRecord::Base
has_paper_trail :ignore => :brand
end

View File

@ -116,6 +116,12 @@ class SetUpTestTables < ActiveRecord::Migration
t.string :language_code
t.string :type
end
create_table :gadgets, :force => true do |t|
t.string :name
t.string :brand
t.timestamps
end
end
def self.down
@ -136,5 +142,6 @@ class SetUpTestTables < ActiveRecord::Migration
drop_table :documents
drop_table :legacy_widgets
drop_table :translations
drop_table :gadgets
end
end

View File

@ -227,7 +227,7 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
'id' => [nil, @widget.id]
}
assert_equal "Time", @widget.versions.last.changeset['updated_at'][1].class.to_s
assert_equal Time, @widget.versions.last.changeset['updated_at'][1].class
assert_equal changes, @widget.versions.last.changeset
end