2016-03-05 17:07:32 -05:00
|
|
|
require "rails_helper"
|
2015-09-10 11:40:59 -04:00
|
|
|
|
2016-02-15 22:32:40 -05:00
|
|
|
describe CallbackModifier, type: :model do
|
2015-09-10 11:40:59 -04:00
|
|
|
with_versioning do
|
2016-03-05 17:07:32 -05:00
|
|
|
describe "callback-methods", versioning: true do
|
|
|
|
describe "paper_trail_on_destroy" do
|
|
|
|
it "should add :destroy to paper_trail_options[:on]" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-10-09 04:54:42 -04:00
|
|
|
expect(modifier.paper_trail_options[:on]).to eq [:destroy]
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
context "when :before" do
|
|
|
|
it "should create the version before destroy" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = BeforeDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-09-10 11:40:59 -04:00
|
|
|
modifier.test_destroy
|
|
|
|
expect(modifier.versions.last.reify).not_to be_flagged_deleted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
context "when :after" do
|
|
|
|
it "should create the version after destroy" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = AfterDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-09-10 11:40:59 -04:00
|
|
|
modifier.test_destroy
|
|
|
|
expect(modifier.versions.last.reify).to be_flagged_deleted
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
context "when no argument" do
|
|
|
|
it "should default to before destroy" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = NoArgDestroyModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-09-10 11:40:59 -04:00
|
|
|
modifier.test_destroy
|
2016-01-02 11:51:02 -05:00
|
|
|
expect(modifier.versions.last.reify).not_to be_flagged_deleted
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
describe "paper_trail_on_update" do
|
|
|
|
it "should add :update to paper_trail_options[:on]" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-10-09 04:54:42 -04:00
|
|
|
expect(modifier.paper_trail_options[:on]).to eq [:update]
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should create a version" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = UpdateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2016-03-05 17:07:32 -05:00
|
|
|
modifier.update_attributes! some_content: "modified"
|
|
|
|
expect(modifier.versions.last.event).to eq "update"
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
describe "paper_trail_on_create" do
|
|
|
|
it "should add :create to paper_trail_options[:on]" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-10-09 04:54:42 -04:00
|
|
|
expect(modifier.paper_trail_options[:on]).to eq [:create]
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should create a version" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2016-03-05 17:07:32 -05:00
|
|
|
expect(modifier.versions.last.event).to eq "create"
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
context "when no callback-method used" do
|
|
|
|
it "should set paper_trail_options[:on] to [:create, :update, :destroy]" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-10-09 04:54:42 -04:00
|
|
|
expect(modifier.paper_trail_options[:on]).to eq [:create, :update, :destroy]
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should default to track destroy" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
2015-09-10 11:40:59 -04:00
|
|
|
modifier.destroy
|
2016-03-05 17:07:32 -05:00
|
|
|
expect(modifier.versions.last.event).to eq "destroy"
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should default to track update" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
2016-03-05 17:07:32 -05:00
|
|
|
modifier.update_attributes! some_content: "modified"
|
|
|
|
expect(modifier.versions.last.event).to eq "update"
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
it "should default to track create" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = DefaultModifier.create!(some_content: FFaker::Lorem.sentence)
|
2016-03-05 17:07:32 -05:00
|
|
|
expect(modifier.versions.last.event).to eq "create"
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-05 17:07:32 -05:00
|
|
|
context "when only one callback-method" do
|
|
|
|
it "does only track the corresponding event" do
|
2016-02-15 22:32:40 -05:00
|
|
|
modifier = CreateModifier.create!(some_content: FFaker::Lorem.sentence)
|
2016-03-05 17:07:32 -05:00
|
|
|
modifier.update_attributes!(some_content: "modified")
|
2015-09-10 11:40:59 -04:00
|
|
|
modifier.test_destroy
|
2016-03-05 17:07:32 -05:00
|
|
|
expect(modifier.versions.collect(&:event)).to eq ["create"]
|
2015-09-10 11:40:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|