2017-12-10 23:05:11 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-30 00:58:26 -04:00
|
|
|
require "spec_helper"
|
2015-03-01 14:02:31 -05:00
|
|
|
|
2017-05-21 01:41:20 -04:00
|
|
|
RSpec.describe Animal, type: :model, versioning: true do
|
2017-02-05 19:08:27 -05:00
|
|
|
it "baseline test setup" do
|
|
|
|
expect(Animal.new).to be_versioned
|
|
|
|
expect(Animal.inheritance_column).to eq("species")
|
|
|
|
end
|
2015-03-01 14:02:31 -05:00
|
|
|
|
2018-07-30 10:50:32 -04:00
|
|
|
describe "#descends_from_active_record?" do
|
|
|
|
it "returns true, meaning that Animal is not an STI subclass" do
|
|
|
|
expect(described_class.descends_from_active_record?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-05 19:08:27 -05:00
|
|
|
it "works with custom STI inheritance column" do
|
|
|
|
animal = Animal.create(name: "Animal")
|
2018-12-04 16:10:35 -05:00
|
|
|
animal.update(name: "Animal from the Muppets")
|
|
|
|
animal.update(name: "Animal Muppet")
|
2017-02-05 19:08:27 -05:00
|
|
|
animal.destroy
|
|
|
|
dog = Dog.create(name: "Snoopy")
|
2018-12-04 16:10:35 -05:00
|
|
|
dog.update(name: "Scooby")
|
|
|
|
dog.update(name: "Scooby Doo")
|
2017-02-05 19:08:27 -05:00
|
|
|
dog.destroy
|
|
|
|
cat = Cat.create(name: "Garfield")
|
2018-12-04 16:10:35 -05:00
|
|
|
cat.update(name: "Garfield (I hate Mondays)")
|
|
|
|
cat.update(name: "Garfield The Cat")
|
2017-02-05 19:08:27 -05:00
|
|
|
cat.destroy
|
|
|
|
expect(PaperTrail::Version.count).to(eq(12))
|
|
|
|
expect(animal.versions.count).to(eq(4))
|
|
|
|
expect(animal.versions.first.reify).to(be_nil)
|
|
|
|
animal.versions[(1..-1)].each do |v|
|
|
|
|
expect(v.reify.class.name).to(eq("Animal"))
|
2015-03-01 14:02:31 -05:00
|
|
|
end
|
2017-02-05 19:08:27 -05:00
|
|
|
dog_versions = PaperTrail::Version.where(item_id: dog.id).order(:created_at)
|
|
|
|
expect(dog_versions.count).to(eq(4))
|
|
|
|
expect(dog_versions.first.reify).to(be_nil)
|
2017-05-21 02:40:23 -04:00
|
|
|
expect(dog_versions.map { |v| v.reify.class.name }).to eq(%w[NilClass Dog Dog Dog])
|
2017-02-05 19:08:27 -05:00
|
|
|
cat_versions = PaperTrail::Version.where(item_id: cat.id).order(:created_at)
|
|
|
|
expect(cat_versions.count).to(eq(4))
|
|
|
|
expect(cat_versions.first.reify).to(be_nil)
|
2017-05-21 02:40:23 -04:00
|
|
|
expect(cat_versions.map { |v| v.reify.class.name }).to eq(%w[NilClass Cat Cat Cat])
|
2017-02-05 19:08:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows the inheritance_column (species) to be updated" do
|
|
|
|
cat = Cat.create!(name: "Leo")
|
2018-12-04 16:10:35 -05:00
|
|
|
cat.update(name: "Spike", species: "Dog")
|
2017-02-05 19:08:27 -05:00
|
|
|
dog = Animal.find(cat.id)
|
|
|
|
expect(dog).to be_instance_of(Dog)
|
|
|
|
end
|
2015-09-15 09:06:14 -04:00
|
|
|
|
2017-02-05 19:08:27 -05:00
|
|
|
context "with callback-methods" do
|
|
|
|
context "when only has_paper_trail set in super class" do
|
|
|
|
let(:callback_cat) { Cat.create(name: "Markus") }
|
2015-09-15 09:06:14 -04:00
|
|
|
|
2017-02-05 19:08:27 -05:00
|
|
|
it "trails all events" do
|
2018-12-04 16:10:35 -05:00
|
|
|
callback_cat.update(name: "Billie")
|
2017-02-05 19:08:27 -05:00
|
|
|
callback_cat.destroy
|
2017-05-21 02:40:23 -04:00
|
|
|
expect(callback_cat.versions.collect(&:event)).to eq %w[create update destroy]
|
2017-02-05 19:08:27 -05:00
|
|
|
end
|
2015-09-15 09:06:14 -04:00
|
|
|
|
2017-02-05 19:08:27 -05:00
|
|
|
it "does not break reify" do
|
|
|
|
callback_cat.destroy
|
|
|
|
expect { callback_cat.versions.last.reify }.not_to raise_error
|
2015-09-15 09:06:14 -04:00
|
|
|
end
|
|
|
|
end
|
2015-03-01 14:02:31 -05:00
|
|
|
end
|
|
|
|
end
|