2016-03-05 17:07:32 -05:00
|
|
|
require "rails_helper"
|
2014-10-06 19:13:39 -04:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
describe Gadget, type: :model do
|
2014-10-31 10:40:43 -04:00
|
|
|
it { is_expected.to be_versioned }
|
2014-10-06 19:13:39 -04:00
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
let(:gadget) { Gadget.create!(name: "Wrench", brand: "Acme") }
|
2014-10-06 19:13:39 -04:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
describe "updates", versioning: true do
|
2014-10-06 19:13:39 -04:00
|
|
|
it "should generate a version for updates to `name` attribute" do
|
2017-04-01 00:33:33 -04:00
|
|
|
expect { gadget.update_attribute(:name, "Hammer") }.to(change { gadget.versions.size }.by(1))
|
2014-10-06 19:13:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should ignore for updates to `brand` attribute" do
|
2017-04-01 00:33:33 -04:00
|
|
|
expect { gadget.update_attribute(:brand, "Stanley") }.to_not(change { gadget.versions.size })
|
2014-10-06 19:13:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should still generate a version when only the `updated_at` attribute is updated" do
|
2016-01-11 22:07:57 -05:00
|
|
|
# Plus 1 second because MySQL lacks sub-second resolution
|
2016-02-15 18:27:57 -05:00
|
|
|
expect {
|
|
|
|
gadget.update_attribute(:updated_at, Time.now + 1)
|
2017-04-01 00:33:33 -04:00
|
|
|
}.to(change { gadget.versions.size }.by(1))
|
2014-10-06 19:13:39 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-07 23:44:38 -04:00
|
|
|
|
|
|
|
describe "Methods" do
|
2016-02-15 22:32:40 -05:00
|
|
|
describe "Instance", versioning: true do
|
2014-10-07 23:44:38 -04:00
|
|
|
describe "private" do
|
2016-06-27 03:02:33 -04:00
|
|
|
describe "#changed_notably?" do
|
2016-02-15 22:32:40 -05:00
|
|
|
subject { Gadget.new(created_at: Time.now) }
|
2014-10-07 23:44:38 -04:00
|
|
|
|
|
|
|
context "create events" do
|
2016-02-23 17:59:14 -05:00
|
|
|
it { expect(subject.paper_trail.changed_notably?).to be true }
|
2014-10-07 23:44:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "update events" do
|
|
|
|
before { subject.save! }
|
|
|
|
|
|
|
|
context "without update timestamps" do
|
|
|
|
it "should only acknowledge non-ignored attrs" do
|
2016-03-05 17:07:32 -05:00
|
|
|
subject.name = "Wrench"
|
2016-02-23 17:59:14 -05:00
|
|
|
expect(subject.paper_trail.changed_notably?).to be true
|
2014-10-07 23:44:38 -04:00
|
|
|
end
|
|
|
|
|
2015-07-15 21:28:07 -04:00
|
|
|
it "should not acknowledge ignored attr (brand)" do
|
2016-03-05 17:07:32 -05:00
|
|
|
subject.brand = "Acme"
|
2016-02-23 17:59:14 -05:00
|
|
|
expect(subject.paper_trail.changed_notably?).to be false
|
2014-10-07 23:44:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with update timestamps" do
|
|
|
|
it "should only acknowledge non-ignored attrs" do
|
2016-03-05 17:23:10 -05:00
|
|
|
subject.name = "Wrench"
|
|
|
|
subject.updated_at = Time.now
|
2016-02-23 17:59:14 -05:00
|
|
|
expect(subject.paper_trail.changed_notably?).to be true
|
2014-10-07 23:44:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not acknowledge ignored attrs and timestamps only" do
|
2016-03-05 17:23:10 -05:00
|
|
|
subject.brand = "Acme"
|
|
|
|
subject.updated_at = Time.now
|
2016-02-23 17:59:14 -05:00
|
|
|
expect(subject.paper_trail.changed_notably?).to be false
|
2014-10-07 23:44:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-06 19:13:39 -04:00
|
|
|
end
|