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-25 01:45:31 -04:00
|
|
|
|
|
|
|
RSpec.describe(::PaperTrail, versioning: true) do
|
2017-10-04 23:26:53 -04:00
|
|
|
context "a new record" do
|
2017-05-25 01:45:31 -04:00
|
|
|
it "not have any previous versions" do
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(Widget.new.versions).to(eq([]))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "be live" do
|
|
|
|
expect(Widget.new.paper_trail.live?).to(eq(true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "a persisted record" do
|
|
|
|
it "have one previous version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry", created_at: (Time.now - 1.day))
|
|
|
|
expect(widget.versions.length).to(eq(1))
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "be nil in its previous version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
expect(widget.versions.first.object).to(be_nil)
|
|
|
|
expect(widget.versions.first.reify).to(be_nil)
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "record the correct event" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
expect(widget.versions.first.event).to(match(/create/i))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "be live" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
expect(widget.paper_trail.live?).to(eq(true))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "use the widget `updated_at` as the version's `created_at`" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
expect(widget.versions.first.created_at.to_i).to(eq(widget.updated_at.to_i))
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#changeset" do
|
|
|
|
it "has expected values" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
changeset = widget.versions.last.changeset
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(changeset["name"]).to eq([nil, "Henry"])
|
2018-06-07 12:43:47 -04:00
|
|
|
expect(changeset["id"]).to eq([nil, widget.id])
|
2017-10-04 23:26:53 -04:00
|
|
|
# When comparing timestamps, round off to the nearest second, because
|
|
|
|
# mysql doesn't do fractional seconds.
|
|
|
|
expect(changeset["created_at"][0]).to be_nil
|
2018-06-07 12:43:47 -04:00
|
|
|
expect(changeset["created_at"][1].to_i).to eq(widget.created_at.to_i)
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(changeset["updated_at"][0]).to be_nil
|
2018-06-07 12:43:47 -04:00
|
|
|
expect(changeset["updated_at"][1].to_i).to eq(widget.updated_at.to_i)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2018-05-21 07:31:05 -04:00
|
|
|
|
|
|
|
context "custom object_changes_adapter" do
|
|
|
|
after do
|
|
|
|
PaperTrail.config.object_changes_adapter = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "calls the adapter's load_changeset method" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
adapter = instance_spy("CustomObjectChangesAdapter")
|
|
|
|
PaperTrail.config.object_changes_adapter = adapter
|
|
|
|
allow(adapter).to(
|
|
|
|
receive(:load_changeset).with(widget.versions.last).and_return(a: "b", c: "d")
|
|
|
|
)
|
|
|
|
changeset = widget.versions.last.changeset
|
2018-05-21 07:31:05 -04:00
|
|
|
expect(changeset[:a]).to eq("b")
|
|
|
|
expect(changeset[:c]).to eq("d")
|
|
|
|
expect(adapter).to have_received(:load_changeset)
|
|
|
|
end
|
2018-08-29 13:21:58 -04:00
|
|
|
|
|
|
|
it "defaults to the original behavior" do
|
|
|
|
adapter = Class.new.new
|
|
|
|
PaperTrail.config.object_changes_adapter = adapter
|
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
changeset = widget.versions.last.changeset
|
|
|
|
expect(changeset[:name]).to eq([nil, "Henry"])
|
|
|
|
end
|
2018-05-21 07:31:05 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "and then updated without any changes" do
|
2018-03-16 18:21:45 -04:00
|
|
|
it "to have two previous versions" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.touch
|
|
|
|
expect(widget.versions.length).to eq(2)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "and then updated with changes" do
|
2018-03-16 18:21:45 -04:00
|
|
|
it "have three previous versions" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
expect(widget.versions.length).to(eq(2))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "be available in its previous version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
expect(widget.name).to(eq("Harry"))
|
|
|
|
expect(widget.versions.last.object).not_to(be_nil)
|
|
|
|
reified_widget = widget.versions.last.reify
|
|
|
|
expect(reified_widget.name).to(eq("Henry"))
|
|
|
|
expect(widget.name).to(eq("Harry"))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "have the same ID in its previous version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
expect(widget.versions.last.reify.id).to(eq(widget.id))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "record the correct event" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
expect(widget.versions.last.event).to(match(/update/i))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "have versions that are not live" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.versions.map(&:reify).compact.each do |v|
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(v.paper_trail).not_to be_live
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "have stored changes" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
last_obj_changes = widget.versions.last.object_changes
|
2017-10-04 23:26:53 -04:00
|
|
|
actual = PaperTrail.serializer.load(last_obj_changes).reject do |k, _v|
|
|
|
|
(k.to_sym == :updated_at)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(actual).to(eq("name" => %w[Henry Harry]))
|
2018-06-07 12:43:47 -04:00
|
|
|
actual = widget.versions.last.changeset.reject { |k, _v| (k.to_sym == :updated_at) }
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(actual).to(eq("name" => %w[Henry Harry]))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "return changes with indifferent access" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
expect(widget.versions.last.changeset[:name]).to(eq(%w[Henry Harry]))
|
|
|
|
expect(widget.versions.last.changeset["name"]).to(eq(%w[Henry Harry]))
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "and has one associated object" do
|
|
|
|
it "not copy the has_one association by default when reifying" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
wotsit = widget.create_wotsit name: "John"
|
|
|
|
reified_widget = widget.versions.last.reify
|
|
|
|
expect(reified_widget.wotsit).to eq(wotsit)
|
|
|
|
expect(widget.reload.wotsit).to eq(wotsit)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "copy the has_one association when reifying with :has_one => true" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
wotsit = widget.create_wotsit name: "John"
|
|
|
|
reified_widget = widget.versions.last.reify(has_one: true)
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(reified_widget.wotsit).to(be_nil)
|
2018-06-07 12:43:47 -04:00
|
|
|
expect(widget.reload.wotsit).to eq(wotsit)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "and has many associated objects" do
|
|
|
|
it "copy the has_many associations when reifying" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.fluxors.create(name: "f-zero")
|
|
|
|
widget.fluxors.create(name: "f-one")
|
|
|
|
reified_widget = widget.versions.last.reify
|
|
|
|
expect(reified_widget.fluxors.length).to(eq(widget.fluxors.length))
|
|
|
|
expect(reified_widget.fluxors).to match_array(widget.fluxors)
|
|
|
|
expect(reified_widget.versions.length).to(eq(widget.versions.length))
|
|
|
|
expect(reified_widget.versions).to match_array(widget.versions)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "and has many associated polymorphic objects" do
|
|
|
|
it "copy the has_many associations when reifying" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.whatchamajiggers.create(name: "f-zero")
|
|
|
|
widget.whatchamajiggers.create(name: "f-zero")
|
|
|
|
reified_widget = widget.versions.last.reify
|
|
|
|
expect(reified_widget.whatchamajiggers.length).to eq(widget.whatchamajiggers.length)
|
|
|
|
expect(reified_widget.whatchamajiggers).to match_array(widget.whatchamajiggers)
|
|
|
|
expect(reified_widget.versions.length).to(eq(widget.versions.length))
|
|
|
|
expect(reified_widget.versions).to match_array(widget.versions)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "polymorphic objects by themselves" do
|
|
|
|
it "not fail with a nil pointer on the polymorphic association" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget = Whatchamajigger.new(name: "f-zero")
|
|
|
|
widget.save!
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
context "and then destroyed" do
|
|
|
|
it "record the correct event" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.destroy
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(PaperTrail::Version.last.event).to(match(/destroy/i))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "have three previous versions" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.destroy
|
|
|
|
expect(PaperTrail::Version.with_item_keys("Widget", widget.id).length).to(eq(3))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
describe "#attributes" do
|
|
|
|
it "returns the expected attributes for the reified widget" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.destroy
|
|
|
|
reified_widget = PaperTrail::Version.last.reify
|
|
|
|
expect(reified_widget.id).to eq(widget.id)
|
|
|
|
expected = widget.attributes
|
|
|
|
actual = reified_widget.attributes
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(expected["id"]).to eq(actual["id"])
|
|
|
|
expect(expected["name"]).to eq(actual["name"])
|
|
|
|
expect(expected["a_text"]).to eq(actual["a_text"])
|
|
|
|
expect(expected["an_integer"]).to eq(actual["an_integer"])
|
|
|
|
expect(expected["a_float"]).to eq(actual["a_float"])
|
|
|
|
expect(expected["a_decimal"]).to eq(actual["a_decimal"])
|
|
|
|
expect(expected["a_datetime"]).to eq(actual["a_datetime"])
|
|
|
|
expect(expected["a_time"]).to eq(actual["a_time"])
|
|
|
|
expect(expected["a_date"]).to eq(actual["a_date"])
|
|
|
|
expect(expected["a_boolean"]).to eq(actual["a_boolean"])
|
|
|
|
expect(expected["type"]).to eq(actual["type"])
|
2018-06-07 12:43:47 -04:00
|
|
|
|
|
|
|
# We are using `to_i` to truncate to the nearest second, but isn't
|
|
|
|
# there still a chance of this failing intermittently if
|
|
|
|
# ___ and ___ occured more than 0.5s apart?
|
2017-10-04 23:26:53 -04:00
|
|
|
expect(expected["created_at"].to_i).to eq(actual["created_at"].to_i)
|
|
|
|
expect(expected["updated_at"].to_i).to eq(actual["updated_at"].to_i)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "be re-creatable from its previous version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.destroy
|
|
|
|
reified_widget = PaperTrail::Version.last.reify
|
|
|
|
expect(reified_widget.save).to(be_truthy)
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "restore its associations on its previous version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.fluxors.create(name: "flux")
|
|
|
|
widget.destroy
|
|
|
|
reified_widget = PaperTrail::Version.last.reify
|
|
|
|
reified_widget.save
|
|
|
|
expect(reified_widget.fluxors.length).to(eq(1))
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2017-10-04 23:26:53 -04:00
|
|
|
it "have nil item for last version" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.create(name: "Henry")
|
|
|
|
widget.update_attributes(name: "Harry")
|
|
|
|
widget.destroy
|
|
|
|
expect(widget.versions.last.item).to be_nil
|
2017-10-04 23:26:53 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2018-08-13 21:11:24 -04:00
|
|
|
it "has changes" do
|
|
|
|
book = Book.create! title: "A"
|
|
|
|
changes = YAML.load book.versions.last.attributes["object_changes"]
|
|
|
|
expect(changes).to eq("id" => [nil, book.id], "title" => [nil, "A"])
|
|
|
|
|
|
|
|
book.update! title: "B"
|
|
|
|
changes = YAML.load book.versions.last.attributes["object_changes"]
|
|
|
|
expect(changes).to eq("title" => %w[A B])
|
|
|
|
|
|
|
|
book.destroy
|
|
|
|
changes = YAML.load book.versions.last.attributes["object_changes"]
|
|
|
|
expect(changes).to eq("id" => [book.id, nil], "title" => ["B", nil])
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 12:43:47 -04:00
|
|
|
# rubocop:disable RSpec/InstanceVariable
|
2017-10-04 23:26:53 -04:00
|
|
|
context "a record's papertrail" do
|
2017-05-25 01:45:31 -04:00
|
|
|
before do
|
2017-10-27 11:36:37 -04:00
|
|
|
@date_time = Time.now
|
2017-05-25 01:45:31 -04:00
|
|
|
@time = Time.now
|
|
|
|
@date = Date.new(2009, 5, 29)
|
|
|
|
@widget = Widget.create(
|
|
|
|
name: "Warble",
|
|
|
|
a_text: "The quick brown fox",
|
|
|
|
an_integer: 42,
|
|
|
|
a_float: 153.01,
|
|
|
|
a_decimal: 2.71828,
|
|
|
|
a_datetime: @date_time,
|
|
|
|
a_time: @time,
|
|
|
|
a_date: @date,
|
|
|
|
a_boolean: true
|
|
|
|
)
|
|
|
|
@widget.update_attributes(
|
|
|
|
name: nil,
|
|
|
|
a_text: nil,
|
|
|
|
an_integer: nil,
|
|
|
|
a_float: nil,
|
|
|
|
a_decimal: nil,
|
|
|
|
a_datetime: nil,
|
|
|
|
a_time: nil,
|
|
|
|
a_date: nil,
|
|
|
|
a_boolean: false
|
|
|
|
)
|
|
|
|
@previous = @widget.versions.last.reify
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle strings" do
|
|
|
|
expect(@previous.name).to(eq("Warble"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle text" do
|
|
|
|
expect(@previous.a_text).to(eq("The quick brown fox"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle integers" do
|
|
|
|
expect(@previous.an_integer).to(eq(42))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle floats" do
|
|
|
|
assert_in_delta(153.01, @previous.a_float, 0.001)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle decimals" do
|
|
|
|
assert_in_delta(2.7183, @previous.a_decimal, 0.0001)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle datetimes" do
|
|
|
|
expect(@previous.a_datetime.to_time.utc.to_i).to(eq(@date_time.to_time.utc.to_i))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle times" do
|
|
|
|
expect(@previous.a_time.utc.to_i).to(eq(@time.utc.to_i))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle dates" do
|
|
|
|
expect(@previous.a_date).to(eq(@date))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "handle booleans" do
|
|
|
|
expect(@previous.a_boolean).to(be_truthy)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "after a column is removed from the record's schema" do
|
|
|
|
before { @last = @widget.versions.last }
|
|
|
|
|
|
|
|
it "reify previous version" do
|
|
|
|
assert_kind_of(Widget, @last.reify)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "restore all forward-compatible attributes" do
|
|
|
|
expect(@last.reify.name).to(eq("Warble"))
|
|
|
|
expect(@last.reify.a_text).to(eq("The quick brown fox"))
|
|
|
|
expect(@last.reify.an_integer).to(eq(42))
|
|
|
|
assert_in_delta(153.01, @last.reify.a_float, 0.001)
|
|
|
|
assert_in_delta(2.7183, @last.reify.a_decimal, 0.0001)
|
|
|
|
expect(@last.reify.a_datetime.to_time.utc.to_i).to(eq(@date_time.to_time.utc.to_i))
|
|
|
|
expect(@last.reify.a_time.utc.to_i).to(eq(@time.utc.to_i))
|
|
|
|
expect(@last.reify.a_date).to(eq(@date))
|
|
|
|
expect(@last.reify.a_boolean).to(be_truthy)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "A record" do
|
|
|
|
before { @widget = Widget.create(name: "Zaphod") }
|
|
|
|
|
|
|
|
context "with PaperTrail globally disabled" do
|
|
|
|
before do
|
|
|
|
PaperTrail.enabled = false
|
|
|
|
@count = @widget.versions.length
|
|
|
|
end
|
|
|
|
|
|
|
|
after { PaperTrail.enabled = true }
|
|
|
|
|
|
|
|
context "when updated" do
|
|
|
|
before { @widget.update_attributes(name: "Beeblebrox") }
|
|
|
|
|
|
|
|
it "not add to its trail" do
|
|
|
|
expect(@widget.versions.length).to(eq(@count))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with its paper trail turned off" do
|
|
|
|
before do
|
2018-02-01 12:04:50 -05:00
|
|
|
PaperTrail.request.disable_model(Widget)
|
2017-05-25 01:45:31 -04:00
|
|
|
@count = @widget.versions.length
|
|
|
|
end
|
|
|
|
|
2018-02-01 12:04:50 -05:00
|
|
|
after do
|
|
|
|
PaperTrail.request.enable_model(Widget)
|
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
|
|
|
context "when updated" do
|
|
|
|
before { @widget.update_attributes(name: "Beeblebrox") }
|
|
|
|
|
|
|
|
it "not add to its trail" do
|
|
|
|
expect(@widget.versions.length).to(eq(@count))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "and then its paper trail turned on" do
|
2018-02-01 12:04:50 -05:00
|
|
|
before do
|
|
|
|
PaperTrail.request.enable_model(Widget)
|
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
|
|
|
context "when updated" do
|
|
|
|
before { @widget.update_attributes(name: "Ford") }
|
|
|
|
|
|
|
|
it "add to its trail" do
|
|
|
|
expect(@widget.versions.length).to(eq((@count + 1)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "A papertrail with somebody making changes" do
|
|
|
|
before { @widget = Widget.new(name: "Fidget") }
|
|
|
|
|
|
|
|
context "when a record is created" do
|
|
|
|
before do
|
2018-02-01 12:04:50 -05:00
|
|
|
PaperTrail.request.whodunnit = "Alice"
|
2017-05-25 01:45:31 -04:00
|
|
|
@widget.save
|
|
|
|
@version = @widget.versions.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it "track who made the change" do
|
|
|
|
expect(@version.whodunnit).to(eq("Alice"))
|
|
|
|
expect(@version.paper_trail_originator).to(be_nil)
|
|
|
|
expect(@version.terminator).to(eq("Alice"))
|
|
|
|
expect(@widget.paper_trail.originator).to(eq("Alice"))
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when a record is updated" do
|
|
|
|
before do
|
2018-02-01 12:04:50 -05:00
|
|
|
PaperTrail.request.whodunnit = "Bob"
|
2017-05-25 01:45:31 -04:00
|
|
|
@widget.update_attributes(name: "Rivet")
|
|
|
|
@version = @widget.versions.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it "track who made the change" do
|
|
|
|
expect(@version.whodunnit).to(eq("Bob"))
|
|
|
|
expect(@version.paper_trail_originator).to(eq("Alice"))
|
|
|
|
expect(@version.terminator).to(eq("Bob"))
|
|
|
|
expect(@widget.paper_trail.originator).to(eq("Bob"))
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when a record is destroyed" do
|
|
|
|
before do
|
2018-02-01 12:04:50 -05:00
|
|
|
PaperTrail.request.whodunnit = "Charlie"
|
2017-05-25 01:45:31 -04:00
|
|
|
@widget.destroy
|
|
|
|
@version = PaperTrail::Version.last
|
|
|
|
end
|
|
|
|
|
|
|
|
it "track who made the change" do
|
|
|
|
expect(@version.whodunnit).to(eq("Charlie"))
|
|
|
|
expect(@version.paper_trail_originator).to(eq("Bob"))
|
|
|
|
expect(@version.terminator).to(eq("Charlie"))
|
|
|
|
expect(@widget.paper_trail.originator).to(eq("Charlie"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "update_attributes! records timestamps" do
|
|
|
|
wotsit = Wotsit.create!(name: "wotsit")
|
|
|
|
wotsit.update_attributes!(name: "changed")
|
|
|
|
reified = wotsit.versions.last.reify
|
|
|
|
expect(reified.created_at).not_to(be_nil)
|
|
|
|
expect(reified.updated_at).not_to(be_nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "update_attributes! does not raise error" do
|
|
|
|
wotsit = Wotsit.create!(name: "name1")
|
|
|
|
expect { wotsit.update_attributes!(name: "name2") }.not_to(raise_error)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "A subclass" do
|
|
|
|
before do
|
|
|
|
@foo = FooWidget.create
|
|
|
|
@foo.update_attributes!(name: "Foo")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reify with the correct type" do
|
|
|
|
if ActiveRecord::VERSION::MAJOR < 4
|
|
|
|
assert_kind_of(FooWidget, @foo.versions.last.reify)
|
|
|
|
end
|
|
|
|
expect(PaperTrail::Version.last.previous).to(eq(@foo.versions.first))
|
|
|
|
expect(PaperTrail::Version.last.next).to(be_nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the correct originator" do
|
2018-02-01 12:04:50 -05:00
|
|
|
PaperTrail.request.whodunnit = "Ben"
|
2017-05-25 01:45:31 -04:00
|
|
|
@foo.update_attribute(:name, "Geoffrey")
|
2018-02-01 12:04:50 -05:00
|
|
|
expect(@foo.paper_trail.originator).to(eq(PaperTrail.request.whodunnit))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when destroyed" do
|
|
|
|
before { @foo.destroy }
|
|
|
|
|
|
|
|
it "reify with the correct type" do
|
|
|
|
assert_kind_of(FooWidget, @foo.versions.last.reify)
|
|
|
|
expect(PaperTrail::Version.last.previous).to(eq(@foo.versions[1]))
|
|
|
|
expect(PaperTrail::Version.last.next).to(be_nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "An item with versions" do
|
|
|
|
before do
|
|
|
|
@widget = Widget.create(name: "Widget")
|
|
|
|
@widget.update_attributes(name: "Fidget")
|
|
|
|
@widget.update_attributes(name: "Digit")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "which were created over time" do
|
|
|
|
before do
|
|
|
|
@created = 2.days.ago
|
|
|
|
@first_update = 1.day.ago
|
|
|
|
@second_update = 1.hour.ago
|
|
|
|
@widget.versions[0].update_attributes(created_at: @created)
|
|
|
|
@widget.versions[1].update_attributes(created_at: @first_update)
|
|
|
|
@widget.versions[2].update_attributes(created_at: @second_update)
|
|
|
|
@widget.update_attribute(:updated_at, @second_update)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return nil for version_at before it was created" do
|
|
|
|
expect(@widget.paper_trail.version_at((@created - 1))).to(be_nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return how it looked when created for version_at its creation" do
|
|
|
|
expect(@widget.paper_trail.version_at(@created).name).to(eq("Widget"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return how it looked before its first update" do
|
|
|
|
expect(@widget.paper_trail.version_at((@first_update - 1)).name).to(eq("Widget"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return how it looked after its first update" do
|
|
|
|
expect(@widget.paper_trail.version_at(@first_update).name).to(eq("Fidget"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return how it looked before its second update" do
|
|
|
|
expect(@widget.paper_trail.version_at((@second_update - 1)).name).to(eq("Fidget"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return how it looked after its second update" do
|
|
|
|
expect(@widget.paper_trail.version_at(@second_update).name).to(eq("Digit"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return the current object for version_at after latest update" do
|
|
|
|
expect(@widget.paper_trail.version_at(1.day.from_now).name).to(eq("Digit"))
|
|
|
|
end
|
|
|
|
|
|
|
|
context "passing in a string representation of a timestamp" do
|
|
|
|
it "still return a widget when appropriate" do
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.version_at((@created + 1.second).to_s).name
|
|
|
|
).to(eq("Widget"))
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.version_at((@first_update + 1.second).to_s).name
|
|
|
|
).to(eq("Fidget"))
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.version_at((@second_update + 1.second).to_s).name
|
|
|
|
).to(eq("Digit"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context ".versions_between" do
|
|
|
|
before do
|
|
|
|
@created = 30.days.ago
|
|
|
|
@first_update = 15.days.ago
|
|
|
|
@second_update = 1.day.ago
|
|
|
|
@widget.versions[0].update_attributes(created_at: @created)
|
|
|
|
@widget.versions[1].update_attributes(created_at: @first_update)
|
|
|
|
@widget.versions[2].update_attributes(created_at: @second_update)
|
|
|
|
@widget.update_attribute(:updated_at, @second_update)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return versions in the time period" do
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.versions_between(20.days.ago, 10.days.ago).map(&:name)
|
|
|
|
).to(eq(["Fidget"]))
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.versions_between(45.days.ago, 10.days.ago).map(&:name)
|
|
|
|
).to(eq(%w[Widget Fidget]))
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.versions_between(16.days.ago, 1.minute.ago).map(&:name)
|
|
|
|
).to(eq(%w[Fidget Digit Digit]))
|
|
|
|
expect(
|
|
|
|
@widget.paper_trail.versions_between(60.days.ago, 45.days.ago).map(&:name)
|
|
|
|
).to(eq([]))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "on the first version" do
|
|
|
|
before { @version = @widget.versions.first }
|
|
|
|
|
|
|
|
it "have a nil previous version" do
|
|
|
|
expect(@version.previous).to(be_nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return the next version" do
|
|
|
|
expect(@version.next).to(eq(@widget.versions[1]))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return the correct index" do
|
|
|
|
expect(@version.index).to(eq(0))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "on the last version" do
|
|
|
|
before { @version = @widget.versions.last }
|
|
|
|
|
|
|
|
it "return the previous version" do
|
|
|
|
expect(@version.previous).to(eq(@widget.versions[(@widget.versions.length - 2)]))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "have a nil next version" do
|
|
|
|
expect(@version.next).to(be_nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return the correct index" do
|
|
|
|
expect(@version.index).to(eq((@widget.versions.length - 1)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "An item" do
|
|
|
|
before do
|
|
|
|
@initial_title = "Foobar"
|
|
|
|
@article = Article.new(title: @initial_title)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "which is created" do
|
|
|
|
before { @article.save }
|
|
|
|
|
|
|
|
it "store fixed meta data" do
|
|
|
|
expect(@article.versions.last.answer).to(eq(42))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data which is independent of the item" do
|
|
|
|
expect(@article.versions.last.question).to(eq("31 + 11 = 42"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data which depends on the item" do
|
|
|
|
expect(@article.versions.last.article_id).to(eq(@article.id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data based on a method of the item" do
|
|
|
|
expect(@article.versions.last.action).to(eq(@article.action_data_provider_method))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data based on an attribute of the item at creation" do
|
|
|
|
expect(@article.versions.last.title).to(eq(@initial_title))
|
|
|
|
end
|
|
|
|
|
|
|
|
context "and updated" do
|
|
|
|
before do
|
|
|
|
@article.update_attributes!(content: "Better text.", title: "Rhubarb")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store fixed meta data" do
|
|
|
|
expect(@article.versions.last.answer).to(eq(42))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data which is independent of the item" do
|
|
|
|
expect(@article.versions.last.question).to(eq("31 + 11 = 42"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data which depends on the item" do
|
|
|
|
expect(@article.versions.last.article_id).to(eq(@article.id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic meta data based on an attribute of the item prior to the update" do
|
|
|
|
expect(@article.versions.last.title).to(eq(@initial_title))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "and destroyed" do
|
|
|
|
before { @article.destroy }
|
|
|
|
|
|
|
|
it "store fixed metadata" do
|
|
|
|
expect(@article.versions.last.answer).to(eq(42))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic metadata which is independent of the item" do
|
|
|
|
expect(@article.versions.last.question).to(eq("31 + 11 = 42"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic metadata which depends on the item" do
|
|
|
|
expect(@article.versions.last.article_id).to(eq(@article.id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store dynamic metadata based on attribute of item prior to destruction" do
|
|
|
|
expect(@article.versions.last.title).to(eq(@initial_title))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "A reified item" do
|
|
|
|
before do
|
|
|
|
widget = Widget.create(name: "Bob")
|
|
|
|
%w[Tom Dick Jane].each do |name|
|
|
|
|
widget.update_attributes(name: name)
|
|
|
|
end
|
|
|
|
@version = widget.versions.last
|
|
|
|
@widget = @version.reify
|
|
|
|
end
|
|
|
|
|
|
|
|
it "know which version it came from" do
|
|
|
|
expect(@widget.version).to(eq(@version))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "return its previous self" do
|
|
|
|
expect(@widget.paper_trail.previous_version).to(eq(@widget.versions[-2].reify))
|
|
|
|
end
|
|
|
|
end
|
2018-08-18 17:49:59 -04:00
|
|
|
# rubocop:enable RSpec/InstanceVariable
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2018-08-18 17:49:59 -04:00
|
|
|
describe "#next_version" do
|
|
|
|
context "a reified item" do
|
|
|
|
it "returns the object (not a Version) as it became next" do
|
|
|
|
widget = Widget.create(name: "Bob")
|
2017-05-25 01:45:31 -04:00
|
|
|
%w[Tom Dick Jane].each do |name|
|
2018-08-18 17:49:59 -04:00
|
|
|
widget.update_attributes(name: name)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2018-08-18 17:49:59 -04:00
|
|
|
second_widget = widget.versions[1].reify
|
|
|
|
last_widget = widget.versions.last.reify
|
|
|
|
expect(second_widget.paper_trail.next_version.name).to(eq(widget.versions[2].reify.name))
|
|
|
|
expect(widget.name).to(eq(last_widget.paper_trail.next_version.name))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
2018-08-18 17:49:59 -04:00
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
|
2018-08-18 17:49:59 -04:00
|
|
|
context "a non-reified item" do
|
|
|
|
it "always returns nil because cannot ever have a next version" do
|
|
|
|
widget = Widget.new
|
|
|
|
expect(widget.paper_trail.next_version).to(be_nil)
|
|
|
|
widget.save
|
|
|
|
%w[Tom Dick Jane].each do |name|
|
|
|
|
widget.update_attributes(name: name)
|
|
|
|
end
|
|
|
|
expect(widget.paper_trail.next_version).to(be_nil)
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-18 17:49:59 -04:00
|
|
|
describe "#previous_version" do
|
|
|
|
context "a reified item" do
|
|
|
|
it "returns the object (not a Version) as it was most recently" do
|
|
|
|
widget = Widget.create(name: "Bob")
|
|
|
|
%w[Tom Dick Jane].each do |name|
|
|
|
|
widget.update_attributes(name: name)
|
|
|
|
end
|
|
|
|
second_widget = widget.versions[1].reify
|
|
|
|
last_widget = widget.versions.last.reify
|
|
|
|
expect(second_widget.paper_trail.previous_version).to(be_nil)
|
|
|
|
expect(last_widget.paper_trail.previous_version.name).to(eq(widget.versions[-2].reify.name))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-18 17:49:59 -04:00
|
|
|
context "a non-reified item" do
|
|
|
|
it "returns the object (not a Version) as it was most recently" do
|
|
|
|
widget = Widget.new
|
|
|
|
expect(widget.paper_trail.previous_version).to(be_nil)
|
|
|
|
widget.save
|
|
|
|
%w[Tom Dick Jane].each do |name|
|
|
|
|
widget.update_attributes(name: name)
|
|
|
|
end
|
|
|
|
expect(widget.paper_trail.previous_version.name).to(eq(widget.versions.last.reify.name))
|
|
|
|
end
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context ":has_many :through" do
|
|
|
|
it "store version on source <<" do
|
2018-08-18 17:49:59 -04:00
|
|
|
book = Book.create(title: "War and Peace")
|
|
|
|
dostoyevsky = Person.create(name: "Dostoyevsky")
|
|
|
|
Person.create(name: "Solzhenitsyn")
|
2017-05-25 01:45:31 -04:00
|
|
|
count = PaperTrail::Version.count
|
2018-08-18 17:49:59 -04:00
|
|
|
(book.authors << dostoyevsky)
|
2017-05-25 01:45:31 -04:00
|
|
|
expect((PaperTrail::Version.count - count)).to(eq(1))
|
2018-08-18 17:49:59 -04:00
|
|
|
expect(book.authorships.first.versions.first).to(eq(PaperTrail::Version.last))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "store version on source create" do
|
2018-08-18 17:49:59 -04:00
|
|
|
book = Book.create(title: "War and Peace")
|
|
|
|
Person.create(name: "Dostoyevsky")
|
|
|
|
Person.create(name: "Solzhenitsyn")
|
2017-05-25 01:45:31 -04:00
|
|
|
count = PaperTrail::Version.count
|
2018-08-18 17:49:59 -04:00
|
|
|
book.authors.create(name: "Tolstoy")
|
2017-05-25 01:45:31 -04:00
|
|
|
expect((PaperTrail::Version.count - count)).to(eq(2))
|
|
|
|
expect(
|
|
|
|
[PaperTrail::Version.order(:id).to_a[-2].item, PaperTrail::Version.last.item]
|
|
|
|
).to match_array([Person.last, Authorship.last])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "store version on join destroy" do
|
2018-08-18 17:49:59 -04:00
|
|
|
book = Book.create(title: "War and Peace")
|
|
|
|
dostoyevsky = Person.create(name: "Dostoyevsky")
|
|
|
|
Person.create(name: "Solzhenitsyn")
|
|
|
|
(book.authors << dostoyevsky)
|
2017-05-25 01:45:31 -04:00
|
|
|
count = PaperTrail::Version.count
|
2018-08-18 17:49:59 -04:00
|
|
|
book.authorships.reload.last.destroy
|
2017-05-25 01:45:31 -04:00
|
|
|
expect((PaperTrail::Version.count - count)).to(eq(1))
|
2018-08-18 17:49:59 -04:00
|
|
|
expect(PaperTrail::Version.last.reify.book).to(eq(book))
|
|
|
|
expect(PaperTrail::Version.last.reify.author).to(eq(dostoyevsky))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "store version on join clear" do
|
2018-08-18 17:49:59 -04:00
|
|
|
book = Book.create(title: "War and Peace")
|
|
|
|
dostoyevsky = Person.create(name: "Dostoyevsky")
|
|
|
|
Person.create(name: "Solzhenitsyn")
|
|
|
|
book.authors << dostoyevsky
|
2017-05-25 01:45:31 -04:00
|
|
|
count = PaperTrail::Version.count
|
2018-08-18 17:49:59 -04:00
|
|
|
book.authorships.reload.destroy_all
|
2017-05-25 01:45:31 -04:00
|
|
|
expect((PaperTrail::Version.count - count)).to(eq(1))
|
2018-08-18 17:49:59 -04:00
|
|
|
expect(PaperTrail::Version.last.reify.book).to(eq(book))
|
|
|
|
expect(PaperTrail::Version.last.reify.author).to(eq(dostoyevsky))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 12:43:47 -04:00
|
|
|
context "the default accessor, length=, is overwritten" do
|
|
|
|
it "returns overwritten value on reified instance" do
|
|
|
|
song = Song.create(length: 4)
|
|
|
|
song.update_attributes(length: 5)
|
|
|
|
expect(song.length).to(eq(5))
|
|
|
|
expect(song.versions.last.reify.length).to(eq(4))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 12:43:47 -04:00
|
|
|
context "song name is a virtual attribute (no such db column)" do
|
|
|
|
it "returns overwritten virtual attribute on the reified instance" do
|
|
|
|
song = Song.create(length: 4)
|
|
|
|
song.update_attributes(length: 5)
|
|
|
|
song.name = "Good Vibrations"
|
|
|
|
song.save
|
|
|
|
song.name = "Yellow Submarine"
|
|
|
|
expect(song.name).to(eq("Yellow Submarine"))
|
|
|
|
expect(song.versions.last.reify.name).to(eq("Good Vibrations"))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "An unsaved record" do
|
|
|
|
it "not have a version created on destroy" do
|
2018-06-07 12:43:47 -04:00
|
|
|
widget = Widget.new
|
|
|
|
widget.destroy
|
|
|
|
expect(widget.versions.empty?).to(eq(true))
|
2017-05-25 01:45:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|