2017-12-10 23:05:11 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-30 00:58:26 -04:00
|
|
|
require "spec_helper"
|
2017-05-21 02:35:02 -04:00
|
|
|
|
|
|
|
module PaperTrail
|
|
|
|
::RSpec.describe(Version, versioning: true) do
|
|
|
|
describe ".creates" do
|
|
|
|
it "returns only create events" do
|
|
|
|
animal = Animal.create(name: "Foo")
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: "Bar")
|
2017-05-21 02:35:02 -04:00
|
|
|
expect(described_class.creates.pluck(:event)).to eq(["create"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".updates" do
|
|
|
|
it "returns only update events" do
|
|
|
|
animal = Animal.create
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: "Animal")
|
2017-05-21 02:35:02 -04:00
|
|
|
expect(described_class.updates.pluck(:event)).to eq(["update"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".destroys" do
|
|
|
|
it "returns only destroy events" do
|
|
|
|
animal = Animal.create
|
|
|
|
animal.destroy
|
|
|
|
expect(described_class.destroys.pluck(:event)).to eq(["destroy"])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".not_creates" do
|
|
|
|
it "returns all versions except create events" do
|
|
|
|
animal = Animal.create
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: "Animal")
|
2017-05-21 02:35:02 -04:00
|
|
|
animal.destroy
|
2017-05-25 01:45:31 -04:00
|
|
|
expect(
|
|
|
|
described_class.not_creates.pluck(:event)
|
|
|
|
).to match_array(%w[update destroy])
|
2017-05-21 02:35:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".subsequent" do
|
|
|
|
context "given a timestamp" do
|
|
|
|
it "returns all versions that were created after the timestamp" do
|
|
|
|
animal = Animal.create
|
|
|
|
2.times do
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: FFaker::Lorem.word)
|
2017-05-21 02:35:02 -04:00
|
|
|
end
|
|
|
|
value = described_class.subsequent(1.hour.ago, true)
|
|
|
|
expect(value).to eq(animal.versions.to_a)
|
|
|
|
expect(value.to_sql).to match(
|
|
|
|
/ORDER BY #{described_class.arel_table[:created_at].asc.to_sql}/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "given a Version" do
|
|
|
|
it "grab the timestamp from the version and use that as the value" do
|
|
|
|
animal = Animal.create
|
|
|
|
2.times do
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: FFaker::Lorem.word)
|
2017-05-21 02:35:02 -04:00
|
|
|
end
|
|
|
|
expect(described_class.subsequent(animal.versions.first)).to eq(
|
|
|
|
animal.versions.to_a.drop(1)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".preceding" do
|
|
|
|
context "given a timestamp" do
|
|
|
|
it "returns all versions that were created before the timestamp" do
|
|
|
|
animal = Animal.create
|
|
|
|
2.times do
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: FFaker::Lorem.word)
|
2017-05-21 02:35:02 -04:00
|
|
|
end
|
|
|
|
value = described_class.preceding(5.seconds.from_now, true)
|
|
|
|
expect(value).to eq(animal.versions.reverse)
|
|
|
|
expect(value.to_sql).to match(
|
|
|
|
/ORDER BY #{described_class.arel_table[:created_at].desc.to_sql}/
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "given a Version" do
|
|
|
|
it "grab the timestamp from the version and use that as the value" do
|
|
|
|
animal = Animal.create
|
|
|
|
2.times do
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: FFaker::Lorem.word)
|
2017-05-21 02:35:02 -04:00
|
|
|
end
|
|
|
|
expect(described_class.preceding(animal.versions.last)).to eq(
|
|
|
|
animal.versions.to_a.tap(&:pop).reverse
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|