diff --git a/spec/dummy_app/app/models/article.rb b/spec/dummy_app/app/models/article.rb index 37e125de..63d9ce1e 100644 --- a/spec/dummy_app/app/models/article.rb +++ b/spec/dummy_app/app/models/article.rb @@ -1,3 +1,4 @@ +# Demonstrates the `only` and `ignore` attributes, among other things. class Article < ActiveRecord::Base has_paper_trail( ignore: [ diff --git a/spec/dummy_app/app/models/document.rb b/spec/dummy_app/app/models/document.rb index d92b5bee..f27ad195 100644 --- a/spec/dummy_app/app/models/document.rb +++ b/spec/dummy_app/app/models/document.rb @@ -1,3 +1,5 @@ +# Demonstrates a "custom versions association name". Instead of the assication +# being named `versions`, it will be named `paper_trail_versions`. class Document < ActiveRecord::Base has_paper_trail( versions: :paper_trail_versions, diff --git a/spec/dummy_app/app/models/legacy_widget.rb b/spec/dummy_app/app/models/legacy_widget.rb index 23ab454c..ee9afdb3 100644 --- a/spec/dummy_app/app/models/legacy_widget.rb +++ b/spec/dummy_app/app/models/legacy_widget.rb @@ -1,3 +1,6 @@ +# The `legacy_widgets` table has a `version` column that would conflict with our +# `version` method. It is configured to define a method named `custom_version` +# instead. class LegacyWidget < ActiveRecord::Base has_paper_trail ignore: :version, version: "custom_version" end diff --git a/spec/dummy_app/app/models/translation.rb b/spec/dummy_app/app/models/translation.rb index 6753d44d..7c55999b 100644 --- a/spec/dummy_app/app/models/translation.rb +++ b/spec/dummy_app/app/models/translation.rb @@ -1,4 +1,9 @@ +# Demonstrates the `if` and `unless` configuration options. class Translation < ActiveRecord::Base + # Has a `type` column, but it's not used for STI. + # TODO: rename column + self.inheritance_column = nil + has_paper_trail( if: proc { |t| t.language_code == "US" }, unless: proc { |t| t.type == "DRAFT" } diff --git a/spec/models/article_spec.rb b/spec/models/article_spec.rb new file mode 100644 index 00000000..488bf71f --- /dev/null +++ b/spec/models/article_spec.rb @@ -0,0 +1,186 @@ +require "spec_helper" + +RSpec.describe Article, type: :model, versioning: true do + describe ".create" do + it "also creates a version record" do + expect { described_class.create }.to( + change { PaperTrail::Version.count }.by(+1) + ) + end + end + + context "which updates an ignored column" do + it "not change the number of versions" do + article = described_class.create + article.update_attributes(title: "My first title") + expect(PaperTrail::Version.count).to(eq(1)) + end + end + + context "which updates an ignored column with truly Proc" do + it "not change the number of versions" do + article = described_class.create + article.update_attributes(abstract: "ignore abstract") + expect(PaperTrail::Version.count).to(eq(1)) + end + end + + context "which updates an ignored column with falsy Proc" do + it "change the number of versions" do + article = described_class.create + article.update_attributes(abstract: "do not ignore abstract!") + expect(PaperTrail::Version.count).to(eq(2)) + end + end + + context "which updates an ignored column, ignored with truly Proc and a selected column" do + it "change the number of versions" do + article = described_class.create + article.update_attributes( + title: "My first title", + content: "Some text here.", + abstract: "ignore abstract" + ) + expect(PaperTrail::Version.count).to(eq(2)) + expect(article.versions.size).to(eq(2)) + end + + it "have stored only non-ignored attributes" do + article = described_class.create + article.update_attributes( + title: "My first title", + content: "Some text here.", + abstract: "ignore abstract" + ) + expected = { "content" => [nil, "Some text here."] } + expect(article.versions.last.changeset).to(eq(expected)) + end + end + + context "which updates an ignored column, ignored with falsy Proc and a selected column" do + it "change the number of versions" do + article = described_class.create + article.update_attributes( + title: "My first title", + content: "Some text here.", + abstract: "do not ignore abstract" + ) + expect(PaperTrail::Version.count).to(eq(2)) + expect(article.versions.size).to(eq(2)) + end + + it "stores only non-ignored attributes" do + article = described_class.create + article.update_attributes( + title: "My first title", + content: "Some text here.", + abstract: "do not ignore abstract" + ) + expected = { + "content" => [nil, "Some text here."], + "abstract" => [nil, "do not ignore abstract"] + } + expect(article.versions.last.changeset).to(eq(expected)) + end + end + + context "which updates a selected column" do + it "change the number of versions" do + article = described_class.create + article.update_attributes(content: "Some text here.") + expect(PaperTrail::Version.count).to(eq(2)) + expect(article.versions.size).to(eq(2)) + end + end + + context "which updates a non-ignored and non-selected column" do + it "not change the number of versions" do + article = described_class.create + article.update_attributes(abstract: "Other abstract") + expect(PaperTrail::Version.count).to(eq(1)) + end + end + + context "which updates a skipped column" do + it "not change the number of versions" do + article = described_class.create + article.update_attributes(file_upload: "Your data goes here") + expect(PaperTrail::Version.count).to(eq(1)) + end + end + + context "which updates a skipped column and a selected column" do + it "change the number of versions" do + article = described_class.create + article.update_attributes( + file_upload: "Your data goes here", + content: "Some text here." + ) + expect(PaperTrail::Version.count).to(eq(2)) + end + + it "show the new version in the model's `versions` association" do + article = described_class.create + article.update_attributes( + file_upload: "Your data goes here", + content: "Some text here." + ) + expect(article.versions.size).to(eq(2)) + end + + it "have stored only non-skipped attributes" do + article = described_class.create + article.update_attributes( + file_upload: "Your data goes here", + content: "Some text here." + ) + expect( + article.versions.last.changeset + ).to(eq("content" => [nil, "Some text here."])) + end + + context "and when updated again" do + it "have removed the skipped attributes when saving the previous version" do + article = described_class.create + article.update_attributes( + file_upload: "Your data goes here", + content: "Some text here." + ) + article.update_attributes( + file_upload: "More data goes here", + content: "More text here." + ) + old_article = article.versions.last + expect( + PaperTrail.serializer.load(old_article.object)["file_upload"] + ).to(be_nil) + end + + it "have kept the non-skipped attributes in the previous version" do + article = described_class.create + article.update_attributes( + file_upload: "Your data goes here", + content: "Some text here." + ) + article.update_attributes( + file_upload: "More data goes here", + content: "More text here." + ) + old_article = article.versions.last + expect( + PaperTrail.serializer.load(old_article.object)["content"] + ).to(eq("Some text here.")) + end + end + end + + context "#destroy" do + it "creates a version record" do + article = described_class.create + article.destroy + expect(PaperTrail::Version.count).to(eq(2)) + expect(article.versions.size).to(eq(2)) + expect(article.versions.map(&:event)).to(match_array(%w[create destroy])) + end + end +end diff --git a/spec/models/document_spec.rb b/spec/models/document_spec.rb index ecf41720..c5588ea8 100644 --- a/spec/models/document_spec.rb +++ b/spec/models/document_spec.rb @@ -1,21 +1,57 @@ require "spec_helper" -RSpec.describe Document, type: :model do - describe "`have_a_version_with` matcher", versioning: true do +RSpec.describe Document, type: :model, versioning: true do + describe "have_a_version_with matcher" do it "works with custom versions association" do document = Document.create!(name: "Foo") document.update_attributes!(name: "Bar") - expect(document).to have_a_version_with(name: "Foo") end end - describe "`have_a_version_with_changes` matcher", versioning: true do + describe "have_a_version_with_changes matcher" do it "works with custom versions association" do document = Document.create!(name: "Foo") document.update_attributes!(name: "Bar") - expect(document).to have_a_version_with_changes(name: "Bar") end end + + describe "#paper_trail.next_version" do + it "returns the expected document" do + doc = Document.create + doc.update_attributes(name: "Doc 1") + reified = doc.paper_trail_versions.last.reify + expect(doc.name).to(eq(reified.paper_trail.next_version.name)) + end + end + + describe "#paper_trail.previous_version" do + it "returns the expected document" do + doc = Document.create + doc.update_attributes(name: "Doc 1") + doc.update_attributes(name: "Doc 2") + expect(doc.paper_trail_versions.length).to(eq(3)) + expect(doc.paper_trail.previous_version.name).to(eq("Doc 1")) + end + end + + describe "#paper_trail_versions" do + it "returns the expected version records" do + doc = Document.create + doc.update_attributes(name: "Doc 1") + expect(doc.paper_trail_versions.length).to(eq(2)) + expect(doc.paper_trail_versions.map(&:event)).to( + match_array(%w[create update]) + ) + end + end + + describe "#versions" do + it "does not respond to versions method" do + doc = Document.create + doc.update_attributes(name: "Doc 1") + expect(doc).not_to respond_to(:versions) + end + end end diff --git a/spec/models/legacy_widget_spec.rb b/spec/models/legacy_widget_spec.rb new file mode 100644 index 00000000..ea10dedb --- /dev/null +++ b/spec/models/legacy_widget_spec.rb @@ -0,0 +1,40 @@ +require "spec_helper" + +RSpec.describe LegacyWidget, type: :model, versioning: true do + describe "#custom_version" do + it "knows which version it came from" do + widget = described_class.create(name: "foo", version: 2) + %w[bar baz].each { |name| widget.update_attributes(name: name) } + version = widget.versions.last + reified = version.reify + expect(reified.custom_version).to(eq(version)) + end + end + + describe "#previous_version" do + it "return its previous self" do + widget = described_class.create(name: "foo", version: 2) + %w[bar baz].each { |name| widget.update_attributes(name: name) } + version = widget.versions.last + reified = version.reify + expect(reified.paper_trail.previous_version).to(eq(reified.versions[-2].reify)) + end + end + + describe "#update_attributes" do + it "does not create a PT version record because the updated column is ignored" do + described_class.create.update_attributes(version: 1) + expect(PaperTrail::Version.count).to(eq(1)) + end + end + + describe "#version" do + it "is a normal attribute and has nothing to do with PT" do + widget = described_class.create(name: "foo", version: 2) + expect(widget.versions.size).to(eq(1)) + expect(widget.version).to(eq(2)) + widget.update_attributes(version: 3) + expect(widget.version).to(eq(3)) + end + end +end diff --git a/spec/models/on/create_spec.rb b/spec/models/on/create_spec.rb new file mode 100644 index 00000000..dd3566c9 --- /dev/null +++ b/spec/models/on/create_spec.rb @@ -0,0 +1,27 @@ +require "spec_helper" +require_dependency "on/create" + +module On + RSpec.describe Create, type: :model, versioning: true do + describe "#versions" do + it "only have a version for the create event" do + record = described_class.create(name: "Alice") + record.update_attributes(name: "blah") + record.destroy + expect(record.versions.length).to(eq(1)) + expect(record.versions.last.event).to(eq("create")) + end + end + + context "#paper_trail_event" do + it "rembembers the custom event name" do + record = described_class.new + record.paper_trail_event = "banana" + record.update_attributes(name: "blah") + record.destroy + expect(record.versions.length).to(eq(1)) + expect(record.versions.last.event).to(eq("banana")) + end + end + end +end diff --git a/spec/models/on/destroy_spec.rb b/spec/models/on/destroy_spec.rb new file mode 100644 index 00000000..15b8d28a --- /dev/null +++ b/spec/models/on/destroy_spec.rb @@ -0,0 +1,27 @@ +require "spec_helper" +require_dependency "on/destroy" + +module On + RSpec.describe Destroy, type: :model, versioning: true do + describe "#versions" do + it "only creates one version record, for the destroy event" do + record = described_class.create(name: "Alice") + record.update_attributes(name: "blah") + record.destroy + expect(record.versions.length).to(eq(1)) + expect(record.versions.last.event).to(eq("destroy")) + end + end + + context "#paper_trail_event" do + it "rembembers the custom event name" do + record = described_class.create(name: "Alice") + record.paper_trail_event = "banana" + record.update_attributes(name: "blah") + record.destroy + expect(record.versions.length).to(eq(1)) + expect(record.versions.last.event).to(eq("banana")) + end + end + end +end diff --git a/spec/models/on/empty_array_spec.rb b/spec/models/on/empty_array_spec.rb new file mode 100644 index 00000000..05cddd4e --- /dev/null +++ b/spec/models/on/empty_array_spec.rb @@ -0,0 +1,30 @@ +require "spec_helper" +require_dependency "on/empty_array" + +module On + RSpec.describe EmptyArray, type: :model, versioning: true do + describe "#create" do + it "does not create any version records" do + record = described_class.create(name: "Alice") + expect(record.versions.length).to(eq(0)) + end + end + + describe "#touch_with_version" do + it "creates a version record" do + record = described_class.create(name: "Alice") + record.paper_trail.touch_with_version + expect(record.versions.length).to(eq(1)) + expect(record.versions.first.event).to(eq("update")) + end + end + + describe "#update_attributes" do + it "does not create any version records" do + record = described_class.create(name: "Alice") + record.update_attributes(name: "blah") + expect(record.versions.length).to(eq(0)) + end + end + end +end diff --git a/spec/models/on/update_spec.rb b/spec/models/on/update_spec.rb new file mode 100644 index 00000000..738f0a31 --- /dev/null +++ b/spec/models/on/update_spec.rb @@ -0,0 +1,27 @@ +require "spec_helper" +require_dependency "on/update" + +module On + RSpec.describe Update, type: :model, versioning: true do + describe "#versions" do + it "only creates one version record, for the update event" do + record = described_class.create(name: "Alice") + record.update_attributes(name: "blah") + record.destroy + expect(record.versions.length).to(eq(1)) + expect(record.versions.last.event).to(eq("update")) + end + end + + context "#paper_trail_event" do + it "rembembers the custom event name" do + record = described_class.create(name: "Alice") + record.paper_trail_event = "banana" + record.update_attributes(name: "blah") + record.destroy + expect(record.versions.length).to(eq(1)) + expect(record.versions.last.event).to(eq("banana")) + end + end + end +end diff --git a/spec/models/translation_spec.rb b/spec/models/translation_spec.rb new file mode 100644 index 00000000..06f954ad --- /dev/null +++ b/spec/models/translation_spec.rb @@ -0,0 +1,70 @@ +require "spec_helper" + +RSpec.describe Translation, type: :model, versioning: true do + context "for non-US translations" do + it "not change the number of versions" do + described_class.create!(headline: "Headline") + expect(PaperTrail::Version.count).to(eq(0)) + end + + context "after update" do + it "not change the number of versions" do + translation = described_class.create!(headline: "Headline") + translation.update_attributes(content: "Content") + expect(PaperTrail::Version.count).to(eq(0)) + end + end + + context "after destroy" do + it "not change the number of versions" do + translation = described_class.create!(headline: "Headline") + translation.destroy + expect(PaperTrail::Version.count).to(eq(0)) + end + end + end + + context "for US translations" do + context "that are drafts" do + it "creation does not change the number of versions" do + translation = described_class.new(headline: "Headline") + translation.language_code = "US" + translation.type = "DRAFT" + translation.save! + expect(PaperTrail::Version.count).to(eq(0)) + end + + it "update does not change the number of versions" do + translation = described_class.new(headline: "Headline") + translation.language_code = "US" + translation.type = "DRAFT" + translation.save! + translation.update_attributes(content: "Content") + expect(PaperTrail::Version.count).to(eq(0)) + end + end + + context "that are not drafts" do + it "create changes the number of versions" do + described_class.create!(headline: "Headline", language_code: "US") + expect(PaperTrail::Version.count).to(eq(1)) + end + + it "update does not change the number of versions" do + translation = described_class.create!(headline: "Headline", language_code: "US") + translation.update_attributes(content: "Content") + expect(PaperTrail::Version.count).to(eq(2)) + expect(translation.versions.size).to(eq(2)) + end + + it "destroy does not change the number of versions" do + translation = described_class.new(headline: "Headline") + translation.language_code = "US" + translation.save! + translation.destroy + expect(PaperTrail::Version.count).to(eq(2)) + expect(translation.versions.size).to(eq(2)) + end + end + end +end diff --git a/spec/paper_trail/config_spec.rb b/spec/paper_trail/config_spec.rb index 07228bf1..9ef1fa05 100644 --- a/spec/paper_trail/config_spec.rb +++ b/spec/paper_trail/config_spec.rb @@ -29,5 +29,17 @@ module PaperTrail end end end + + describe ".version_limit", versioning: true do + after { PaperTrail.config.version_limit = nil } + + it "limits the number of versions to 3 (2 plus the created at event)" do + PaperTrail.config.version_limit = 2 + widget = Widget.create!(name: "Henry") + 6.times { widget.update_attribute(:name, FFaker::Lorem.word) } + expect(widget.versions.first.event).to(eq("create")) + expect(widget.versions.size).to(eq(3)) + end + end end end diff --git a/spec/paper_trail/model_spec.rb b/spec/paper_trail/model_spec.rb index 39e3ba66..8f083e97 100644 --- a/spec/paper_trail/model_spec.rb +++ b/spec/paper_trail/model_spec.rb @@ -1,262 +1,6 @@ require "spec_helper" RSpec.describe(::PaperTrail, versioning: true) do - context "A record with defined 'only' and 'ignore' attributes" do - before { @article = Article.create } - - it "creation should change the number of versions" do - expect(PaperTrail::Version.count).to(eq(1)) - end - - context "which updates an ignored column" do - it "not change the number of versions" do - @article.update_attributes(title: "My first title") - expect(PaperTrail::Version.count).to(eq(1)) - end - end - - context "which updates an ignored column with truly Proc" do - it "not change the number of versions" do - @article.update_attributes(abstract: "ignore abstract") - expect(PaperTrail::Version.count).to(eq(1)) - end - end - - context "which updates an ignored column with falsy Proc" do - it "change the number of versions" do - @article.update_attributes(abstract: "do not ignore abstract!") - expect(PaperTrail::Version.count).to(eq(2)) - end - end - - context "which updates an ignored column, ignored with truly Proc and a selected column" do - before do - @article.update_attributes( - title: "My first title", - content: "Some text here.", - abstract: "ignore abstract" - ) - end - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@article.versions.size).to(eq(2)) - end - - it "have stored only non-ignored attributes" do - expected = { "content" => [nil, "Some text here."] } - expect(@article.versions.last.changeset).to(eq(expected)) - end - end - - context "which updates an ignored column, ignored with falsy Proc and a selected column" do - before do - @article.update_attributes( - title: "My first title", - content: "Some text here.", - abstract: "do not ignore abstract" - ) - end - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@article.versions.size).to(eq(2)) - end - - it "have stored only non-ignored attributes" do - expected = { - "content" => [nil, "Some text here."], - "abstract" => [nil, "do not ignore abstract"] - } - expect(@article.versions.last.changeset).to(eq(expected)) - end - end - - context "which updates a selected column" do - before { @article.update_attributes(content: "Some text here.") } - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@article.versions.size).to(eq(2)) - end - end - - context "which updates a non-ignored and non-selected column" do - it "not change the number of versions" do - @article.update_attributes(abstract: "Other abstract") - expect(PaperTrail::Version.count).to(eq(1)) - end - end - - context "which updates a skipped column" do - it "not change the number of versions" do - @article.update_attributes(file_upload: "Your data goes here") - expect(PaperTrail::Version.count).to(eq(1)) - end - end - - context "which updates a skipped column and a selected column" do - before do - @article.update_attributes( - file_upload: "Your data goes here", - content: "Some text here." - ) - end - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@article.versions.size).to(eq(2)) - end - - it "have stored only non-skipped attributes" do - expect( - @article.versions.last.changeset - ).to(eq("content" => [nil, "Some text here."])) - end - - context "and when updated again" do - before do - @article.update_attributes( - file_upload: "More data goes here", - content: "More text here." - ) - @old_article = @article.versions.last - end - - it "have removed the skipped attributes when saving the previous version" do - expect( - PaperTrail.serializer.load(@old_article.object)["file_upload"] - ).to(be_nil) - end - - it "have kept the non-skipped attributes in the previous version" do - expect( - PaperTrail.serializer.load(@old_article.object)["content"] - ).to(eq("Some text here.")) - end - end - end - - context "which gets destroyed" do - before { @article.destroy } - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@article.versions.size).to(eq(2)) - end - end - end - - context "A record with defined 'ignore' attribute" do - before { @legacy_widget = LegacyWidget.create } - - context "which updates an ignored column" do - before { @legacy_widget.update_attributes(version: 1) } - - it "not change the number of versions" do - expect(PaperTrail::Version.count).to(eq(1)) - end - end - end - - context "A record with defined \"if\" and \"unless\" attributes" do - before { @translation = Translation.new(headline: "Headline") } - - context "for non-US translations" do - before { @translation.save } - - it "not change the number of versions" do - expect(PaperTrail::Version.count).to(eq(0)) - end - - context "after update" do - before { @translation.update_attributes(content: "Content") } - - it "not change the number of versions" do - expect(PaperTrail::Version.count).to(eq(0)) - end - end - - context "after destroy" do - before { @translation.destroy } - - it "not change the number of versions" do - expect(PaperTrail::Version.count).to(eq(0)) - end - end - end - - context "for US translations" do - before { @translation.language_code = "US" } - - context "that are drafts" do - before do - @translation.type = "DRAFT" - @translation.save - end - - it "not change the number of versions" do - expect(PaperTrail::Version.count).to(eq(0)) - end - - context "after update" do - before { @translation.update_attributes(content: "Content") } - - it "not change the number of versions" do - expect(PaperTrail::Version.count).to(eq(0)) - end - end - end - - context "that are not drafts" do - before { @translation.save } - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(1)) - end - - context "after update" do - before { @translation.update_attributes(content: "Content") } - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@translation.versions.size).to(eq(2)) - end - end - - context "after destroy" do - before { @translation.destroy } - - it "change the number of versions" do - expect(PaperTrail::Version.count).to(eq(2)) - end - - it "show the new version in the model's `versions` association" do - expect(@translation.versions.size).to(eq(2)) - end - end - end - end - end - context "A new record" do before { @widget = Widget.new } @@ -1245,177 +989,4 @@ RSpec.describe(::PaperTrail, versioning: true) do expect(@widget.versions.empty?).to(eq(true)) end end - - context "A model with a custom association" do - before do - @doc = Document.create - @doc.update_attributes(name: "Doc 1") - end - - it "not respond to versions method" do - expect(!@doc.respond_to?(:versions)).to(be_truthy) - end - - it "create a new version record" do - expect(@doc.paper_trail_versions.length).to(eq(2)) - end - - it "respond to `next_version` as normal" do - reified = @doc.paper_trail_versions.last.reify - expect(@doc.name).to(eq(reified.paper_trail.next_version.name)) - end - - it "respond to `previous_version` as normal" do - @doc.update_attributes(name: "Doc 2") - expect(@doc.paper_trail_versions.length).to(eq(3)) - expect(@doc.paper_trail.previous_version.name).to(eq("Doc 1")) - end - end - - context "The `on` option" do - context "on create" do - it "only have a version for the create event" do - record = ::On::Create.create(name: "Alice") - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("create")) - end - end - - context "on update" do - it "only have a version for the update event" do - record = ::On::Update.create(name: "Alice") - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("update")) - end - end - - context "on destroy" do - it "only have a version for the destroy event" do - record = ::On::Destroy.create(name: "Alice") - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("destroy")) - end - end - - context "on []" do - before do - @record = ::On::EmptyArray.create(name: "Alice") - @record.update_attributes(name: "blah") - end - - after { @record.destroy } - - it "not have any versions" do - expect(@record.versions.length).to(eq(0)) - end - - it "still respond to touch_with_version" do - @record.paper_trail.touch_with_version - expect(@record.versions.length).to(eq(1)) - end - end - - context "allows a symbol to be passed" do - it "only have a version for hte create event" do - record = ::On::Create.create(name: "Alice") - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("create")) - end - end - end - - context "A model with column version and custom version_method" do - before do - @legacy_widget = LegacyWidget.create(name: "foo", version: 2) - end - - it "set version on create" do - expect(@legacy_widget.version).to(eq(2)) - end - - it "allow version updates" do - @legacy_widget.update_attributes(version: 3) - expect(@legacy_widget.version).to(eq(3)) - end - - it "create a new version record" do - expect(@legacy_widget.versions.size).to(eq(1)) - end - end - - context "A reified item with a column -version- and custom version_method" do - before do - widget = LegacyWidget.create(name: "foo", version: 2) - %w[bar baz].each { |name| widget.update_attributes(name: name) } - @version = widget.versions.last - @widget = @version.reify - end - - it "know which version it came from" do - expect(@widget.custom_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 - - context "custom events" do - context "on create" do - it "only have a version for the created event" do - record = ::On::Create.new.tap { |model| model.paper_trail_event = "created" } - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("created")) - end - end - - context "on update" do - it "only have a version for the name_updated event" do - record = ::On::Update.create(name: "Alice").tap do |model| - model.paper_trail_event = "name_updated" - end - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("name_updated")) - end - end - - context "on destroy" do - it "only have a version for the destroy event" do - record = ::On::Destroy.create(name: "Alice").tap do |model| - model.paper_trail_event = "destroyed" - end - record.update_attributes(name: "blah") - record.destroy - expect(record.versions.length).to(eq(1)) - expect(record.versions.last.event).to(eq("destroyed")) - end - end - end - - context "`PaperTrail::Config.version_limit` set" do - before do - PaperTrail.config.version_limit = 2 - @widget = Widget.create!(name: "Henry") - 6.times { @widget.update_attribute(:name, FFaker::Lorem.word) } - end - - after { PaperTrail.config.version_limit = nil } - - it "limit the number of versions to 3 (2 plus the created at event)" do - expect(@widget.versions.first.event).to(eq("create")) - expect(@widget.versions.size).to(eq(3)) - end - end end