test/unit/model_test.rb in paper_trail-6.0.2 vs test/unit/model_test.rb in paper_trail-7.0.0

- old
+ new

@@ -1,11 +1,9 @@ require "test_helper" require "time_travel_helper" class HasPaperTrailModelTest < ActiveSupport::TestCase - extend CleanupCallbacks - context "A record with defined 'only' and 'ignore' attributes" do setup { @article = Article.create } should "creation should change the number of versions" do assert_equal(1, PaperTrail::Version.count) @@ -317,13 +315,11 @@ should "record the correct event" do assert_match(/update/i, @widget.versions.last.event) end should "have versions that are not live" do - assert @widget.versions.map(&:reify).compact.all? { |w| - !w.paper_trail.live? - } + assert(@widget.versions.map(&:reify).compact.all? { |w| !w.paper_trail.live? }) end should "have stored changes" do # Behavior for ActiveRecord 4 is different than ActiveRecord 3; # AR4 includes the `updated_at` column in changes for updates, which @@ -1267,118 +1263,66 @@ end end context "The `on` option" do context "on create" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [:create] - END - @fluxor = Fluxor.create - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for the create event" do - assert_equal 1, @fluxor.versions.length - assert_equal "create", @fluxor.versions.last.event + record = ::On::Create.create(name: "Alice") + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "create", record.versions.last.event end end context "on update" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [:update] - END - @fluxor = Fluxor.create - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for the update event" do - assert_equal 1, @fluxor.versions.length - assert_equal "update", @fluxor.versions.last.event + record = ::On::Update.create(name: "Alice") + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "update", record.versions.last.event end end context "on destroy" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [:destroy] - END - @fluxor = Fluxor.create - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for the destroy event" do - assert_equal 1, @fluxor.versions.length - assert_equal "destroy", @fluxor.versions.last.event + record = ::On::Destroy.create(name: "Alice") + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "destroy", record.versions.last.event end end context "on []" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [] - END - @fluxor = Fluxor.create - @fluxor.update_attributes name: "blah" + @record = ::On::EmptyArray.create(name: "Alice") + @record.update_attributes name: "blah" end teardown do - @fluxor.destroy + @record.destroy end should "not have any versions" do - assert_equal 0, @fluxor.versions.length + assert_equal 0, @record.versions.length end should "still respond to touch_with_version" do - @fluxor.paper_trail.touch_with_version - assert_equal 1, @fluxor.versions.length + @record.paper_trail.touch_with_version + assert_equal 1, @record.versions.length end end context "allows a symbol to be passed" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => :create - END - @fluxor = Fluxor.create - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for hte create event" do - assert_equal 1, @fluxor.versions.length - assert_equal "create", @fluxor.versions.last.event + record = ::On::Create.create(name: "Alice") + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "create", record.versions.last.event end end end context "A model with column version and custom version_method" do @@ -1417,68 +1361,41 @@ end end context "custom events" do context "on create" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [:create] - END - @fluxor = Fluxor.new.tap { |model| model.paper_trail_event = "created" } - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for the created event" do - assert_equal 1, @fluxor.versions.length - assert_equal "created", @fluxor.versions.last.event + record = ::On::Create.new.tap { |model| + model.paper_trail_event = "created" + } + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "created", record.versions.last.event end end context "on update" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [:update] - END - @fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "name_updated" } - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for the name_updated event" do - assert_equal 1, @fluxor.versions.length - assert_equal "name_updated", @fluxor.versions.last.event + record = ::On::Update.create(name: "Alice").tap { |model| + model.paper_trail_event = "name_updated" + } + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "name_updated", record.versions.last.event end end context "on destroy" do - cleanup_callbacks(Fluxor, :create) - cleanup_callbacks(Fluxor, :update) - cleanup_callbacks(Fluxor, :destroy) - cleanup_callbacks(Fluxor, :save) - - setup do - Fluxor.instance_eval <<-END - has_paper_trail :on => [:destroy] - END - @fluxor = Fluxor.create.tap { |model| model.paper_trail_event = "destroyed" } - @fluxor.update_attributes name: "blah" - @fluxor.destroy - end - should "only have a version for the destroy event" do - assert_equal 1, @fluxor.versions.length - assert_equal "destroyed", @fluxor.versions.last.event + record = ::On::Destroy.create(name: "Alice").tap { |model| + model.paper_trail_event = "destroyed" + } + record.update_attributes name: "blah" + record.destroy + assert_equal 1, record.versions.length + assert_equal "destroyed", record.versions.last.event end end end context "`PaperTrail::Config.version_limit` set" do