1
0
Fork 0
mirror of https://github.com/paper-trail-gem/paper_trail.git synced 2022-11-09 11:33:19 -05:00
paper-trail-gem--paper_trail/spec/models/callback_modifier_spec.rb
Edouard Chin 2b479a7f08 Support rails 6.0.0 (#1172)
* Change update_attributes to update

In Rails 6.0 update_attributes/update_attributes! is considered deprecated. Method update/update! is the replacement.

* CI: Don't use Bundler 1.16.1

- Bundler 1.16.1 has bug where dependencies can't be resolved properly
  when a gem is a release candidate or an alpha version.
  The underlying bundler issue can be found here https://github.com/bundler/bundler/issues/6449

* Disable eager_load in test env:

- In Rails 6.0, rails/rails@3b95478 made a change to eagerly define
  attribute methods of a Model when `eager_load` is enabled.
  This breaks our test suite because of the way we run migration.

  The TL;DR is that doing `People.attribute_names` will return an
  empty array instead of `[:id, time_zone, ...]`.
  You can find a failing build here https://travis-ci.org/paper-trail-gem/paper_trail/jobs/463369634

  Basically what happens is:

  1) The dummy app boot, attribute methods of each model are defined
     but since migration didn't run yet, the tables aren't even
     created resulting in a empty attribute set.
  2) Migration runs, but it's already too late.

  In this commit I disabled eager_loading in test, AFAIT there isn't
  much benefit in eager_loading the dummy app anyway.
  Also renaming the `user.rb` file to `postgres_user.rb` in order for
  rails autoloading to work correctly.
2018-12-04 16:10:35 -05:00

99 lines
3.4 KiB
Ruby

# frozen_string_literal: true
require "spec_helper"
RSpec.describe CallbackModifier, type: :model, versioning: true do
describe "paper_trail_on_destroy" do
it "adds :destroy to paper_trail_options[:on]" do
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq([:destroy])
end
context "when :before" do
it "creates the version before destroy" do
modifier = BeforeDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).not_to be_flagged_deleted
end
end
if ActiveRecord.gem_version < Gem::Version.new("5") ||
!ActiveRecord::Base.belongs_to_required_by_default
context "when :after" do
it "creates the version after destroy" do
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).to be_flagged_deleted
end
end
end
context "when no argument" do
it "defaults to before destroy" do
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.test_destroy
expect(modifier.versions.last.reify).not_to be_flagged_deleted
end
end
end
describe "paper_trail_on_update" do
it "adds :update to paper_trail_options[:on]" do
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:update]
end
it "creates a version" do
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update! some_content: "modified"
expect(modifier.versions.last.event).to eq "update"
end
end
describe "paper_trail_on_create" do
it "adds :create to paper_trail_options[:on]" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq [:create]
end
it "creates a version" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.versions.last.event).to eq "create"
end
end
context "when no callback-method used" do
it "sets paper_trail_options[:on] to [:create, :update, :destroy]" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.paper_trail_options[:on]).to eq %i[create update destroy touch]
end
it "tracks destroy" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.destroy
expect(modifier.versions.last.event).to eq "destroy"
end
it "tracks update" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update! some_content: "modified"
expect(modifier.versions.last.event).to eq "update"
end
it "tracks create" do
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
expect(modifier.versions.last.event).to eq "create"
end
end
context "when only one callback-method" do
it "does only track the corresponding event" do
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
modifier.update!(some_content: "modified")
modifier.test_destroy
expect(modifier.versions.collect(&:event)).to eq ["create"]
end
end
end