2017-12-11 04:05:11 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-30 04:58:26 +00:00
|
|
|
require "spec_helper"
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
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
|
2015-10-09 08:54:42 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
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
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-08-23 22:16:02 +00:00
|
|
|
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
|
2017-04-02 00:13:08 +00:00
|
|
|
end
|
2017-08-23 22:16:02 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
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
|
2015-09-10 15:40:59 +00:00
|
|
|
end
|
2017-04-02 00:13:08 +00:00
|
|
|
end
|
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
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
|
2015-10-09 08:54:42 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
it "creates a version" do
|
|
|
|
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2018-12-04 21:10:35 +00:00
|
|
|
modifier.update! some_content: "modified"
|
2017-04-02 00:13:08 +00:00
|
|
|
expect(modifier.versions.last.event).to eq "update"
|
|
|
|
end
|
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
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
|
2015-10-09 08:54:42 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
it "creates a version" do
|
|
|
|
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
|
|
|
|
expect(modifier.versions.last.event).to eq "create"
|
|
|
|
end
|
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
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)
|
2018-03-16 22:21:45 +00:00
|
|
|
expect(modifier.paper_trail_options[:on]).to eq %i[create update destroy touch]
|
2017-04-02 00:13:08 +00:00
|
|
|
end
|
2015-10-09 08:54:42 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
it "tracks destroy" do
|
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
|
|
|
modifier.destroy
|
|
|
|
expect(modifier.versions.last.event).to eq "destroy"
|
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
it "tracks update" do
|
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
2018-12-04 21:10:35 +00:00
|
|
|
modifier.update! some_content: "modified"
|
2017-04-02 00:13:08 +00:00
|
|
|
expect(modifier.versions.last.event).to eq "update"
|
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
it "tracks create" do
|
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
|
|
|
expect(modifier.versions.last.event).to eq "create"
|
|
|
|
end
|
|
|
|
end
|
2015-09-10 15:40:59 +00:00
|
|
|
|
2017-04-02 00:13:08 +00:00
|
|
|
context "when only one callback-method" do
|
|
|
|
it "does only track the corresponding event" do
|
|
|
|
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2018-12-04 21:10:35 +00:00
|
|
|
modifier.update!(some_content: "modified")
|
2017-04-02 00:13:08 +00:00
|
|
|
modifier.test_destroy
|
|
|
|
expect(modifier.versions.collect(&:event)).to eq ["create"]
|
2015-09-10 15:40:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|