1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
paper-trail-gem--paper_trail/spec/models/gadget_spec.rb

64 lines
2.1 KiB
Ruby
Raw Normal View History

require "spec_helper"
RSpec.describe Gadget, type: :model do
let(:gadget) { Gadget.create!(name: "Wrench", brand: "Acme") }
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
expect { gadget.update_attribute(:name, "Hammer") }.to(change { gadget.versions.size }.by(1))
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 })
end
2017-04-01 01:50:13 -04:00
it "still generates a version when only the `updated_at` attribute is updated" do
# 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)
}.to(change { gadget.versions.size }.by(1))
end
end
describe "#changed_notably?", versioning: true do
2017-04-01 20:13:08 -04:00
context "new record" do
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)
end
end
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
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
end
2017-04-01 20:13:08 -04:00
end
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
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
end
end
end
end