2017-05-30 00:58:26 -04:00
|
|
|
require "spec_helper"
|
2014-10-06 19:13:39 -04:00
|
|
|
|
2017-05-21 01:41:20 -04:00
|
|
|
RSpec.describe Gadget, type: :model do
|
2016-03-05 17:07:32 -05:00
|
|
|
let(:gadget) { Gadget.create!(name: "Wrench", brand: "Acme") }
|
2014-10-06 19:13:39 -04:00
|
|
|
|
2017-09-18 23:56:07 -04:00
|
|
|
it { is_expected.to be_versioned }
|
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
describe "updates", versioning: true do
|
2017-04-01 01:50:13 -04:00
|
|
|
it "generates 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
|
|
|
|
|
2017-04-01 01:50:13 -04:00
|
|
|
it "ignores for updates to `brand` attribute" do
|
2017-04-01 01:59:47 -04:00
|
|
|
expect { gadget.update_attribute(:brand, "Stanley") }.not_to(change { gadget.versions.size })
|
2014-10-06 19:13:39 -04:00
|
|
|
end
|
|
|
|
|
2017-04-01 01:50:13 -04:00
|
|
|
it "still generates 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
|
|
|
|
2017-04-01 02:28:55 -04:00
|
|
|
describe "#changed_notably?", versioning: true do
|
2017-04-01 20:13:08 -04:00
|
|
|
context "new record" do
|
2017-04-01 02:28:55 -04:00
|
|
|
it "returns true" do
|
2017-04-01 20:13:08 -04:00
|
|
|
g = Gadget.new(created_at: Time.now)
|
|
|
|
expect(g.paper_trail.changed_notably?).to eq(true)
|
2017-04-01 02:28:55 -04:00
|
|
|
end
|
|
|
|
end
|
2014-10-07 23:44:38 -04:00
|
|
|
|
2017-04-01 20:13:08 -04:00
|
|
|
context "persisted record without update timestamps" do
|
|
|
|
it "only acknowledges non-ignored attrs" do
|
|
|
|
subject = Gadget.create!(created_at: Time.now)
|
|
|
|
subject.name = "Wrench"
|
|
|
|
expect(subject.paper_trail.changed_notably?).to be true
|
|
|
|
end
|
2014-10-07 23:44:38 -04:00
|
|
|
|
2017-04-01 20:13:08 -04:00
|
|
|
it "does not acknowledge ignored attr (brand)" do
|
|
|
|
subject = Gadget.create!(created_at: Time.now)
|
|
|
|
subject.brand = "Acme"
|
|
|
|
expect(subject.paper_trail.changed_notably?).to be false
|
2017-04-01 02:28:55 -04:00
|
|
|
end
|
2017-04-01 20:13:08 -04:00
|
|
|
end
|
2014-10-07 23:44:38 -04:00
|
|
|
|
2017-04-01 20:13:08 -04:00
|
|
|
context "persisted record with update timestamps" do
|
|
|
|
it "only acknowledges non-ignored attrs" do
|
|
|
|
subject = Gadget.create!(created_at: Time.now)
|
|
|
|
subject.name = "Wrench"
|
|
|
|
subject.updated_at = Time.now
|
|
|
|
expect(subject.paper_trail.changed_notably?).to be true
|
|
|
|
end
|
2014-10-07 23:44:38 -04:00
|
|
|
|
2017-04-01 20:13:08 -04:00
|
|
|
it "does not acknowledge ignored attrs and timestamps only" do
|
|
|
|
subject = Gadget.create!(created_at: Time.now)
|
|
|
|
subject.brand = "Acme"
|
|
|
|
subject.updated_at = Time.now
|
|
|
|
expect(subject.paper_trail.changed_notably?).to be false
|
2014-10-07 23:44:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-10-06 19:13:39 -04:00
|
|
|
end
|