Add some additional testing and comments surrounding the PaperTrail::Model::InstanceMethods#changed_notably? private method changes

This commit is contained in:
Ben Atkins 2014-10-07 23:44:38 -04:00
parent 052f16495b
commit 8f256d7081
2 changed files with 51 additions and 0 deletions

View File

@ -366,6 +366,10 @@ module PaperTrail
end
end
# This method is invoked in order to determine whether it is appropriate to generate a new version instance.
# Because we are now using `after_(create/update/etc)` callbacks, we need to go out of our way to
# ensure that during updates timestamp attributes are not acknowledged as a notable changes
# to raise false positives when attributes are ignored.
def changed_notably?
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?

View File

@ -18,4 +18,51 @@ describe Gadget do
expect { gadget.update_attribute(:updated_at, Time.now) }.to change{gadget.versions.size}.by(1)
end
end
describe "Methods" do
describe "Instance", :versioning => true do
describe "private" do
describe :changed_notably? do
subject { Gadget.new(:created_at => Time.now) }
# apparently the private methods list in Ruby18 is different than in Ruby19+
if RUBY_VERSION.to_f >= 1.9
its(:private_methods) { should include(:changed_notably?) }
end
context "create events" do
it { subject.send(:changed_notably?).should == true }
end
context "update events" do
before { subject.save! }
context "without update timestamps" do
it "should only acknowledge non-ignored attrs" do
subject.name = 'Wrench'
subject.send(:changed_notably?).should == true
end
it "should not acknowledge ignored attrs and timestamps only" do
subject.brand = 'Acme'
subject.send(:changed_notably?).should == false
end
end
context "with update timestamps" do
it "should only acknowledge non-ignored attrs" do
subject.name, subject.updated_at = 'Wrench', Time.now
subject.send(:changed_notably?).should == true
end
it "should not acknowledge ignored attrs and timestamps only" do
subject.brand, subject.updated_at = 'Acme', Time.now
subject.send(:changed_notably?).should == false
end
end
end
end
end
end
end
end