-
Notifications
You must be signed in to change notification settings - Fork 899
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from Ninigi/master
Enable control over the order of AR callbacks
- Loading branch information
Showing
9 changed files
with
248 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
require 'rails_helper' | ||
require 'support/callback_modifier' | ||
|
||
describe CallbackModifier, :type => :model do | ||
with_versioning do | ||
describe 'callback-methods', :versioning => true do | ||
describe 'paper_trail_on_destroy' do | ||
it 'should add :destroy to paper_trail_options[:on]' do | ||
modifier = NoArgDestroyModifier.create!(:some_content => Faker::Lorem.sentence) | ||
expect(modifier.paper_trail_options[:on]).to eq [:destroy] | ||
end | ||
|
||
context 'when :before' do | ||
it 'should create the version before destroy' do | ||
modifier = BeforeDestroyModifier.create!(:some_content => Faker::Lorem.sentence) | ||
modifier.test_destroy | ||
expect(modifier.versions.last.reify).not_to be_flagged_deleted | ||
end | ||
end | ||
|
||
context 'when :after' do | ||
it 'should create the version after destroy' do | ||
modifier = AfterDestroyModifier.create!(:some_content => Faker::Lorem.sentence) | ||
modifier.test_destroy | ||
expect(modifier.versions.last.reify).to be_flagged_deleted | ||
end | ||
end | ||
|
||
context 'when no argument' do | ||
it 'should default to after destroy' do | ||
modifier = NoArgDestroyModifier.create!(:some_content => Faker::Lorem.sentence) | ||
modifier.test_destroy | ||
expect(modifier.versions.last.reify).to be_flagged_deleted | ||
end | ||
end | ||
end | ||
|
||
describe 'paper_trail_on_update' do | ||
it 'should add :update to paper_trail_options[:on]' do | ||
modifier = UpdateModifier.create!(:some_content => Faker::Lorem.sentence) | ||
expect(modifier.paper_trail_options[:on]).to eq [:update] | ||
end | ||
|
||
it 'should create a version' do | ||
modifier = UpdateModifier.create!(:some_content => Faker::Lorem.sentence) | ||
modifier.update_attributes! :some_content => 'modified' | ||
expect(modifier.versions.last.event).to eq 'update' | ||
end | ||
end | ||
|
||
describe 'paper_trail_on_create' do | ||
it 'should add :create to paper_trail_options[:on]' do | ||
modifier = CreateModifier.create!(:some_content => Faker::Lorem.sentence) | ||
expect(modifier.paper_trail_options[:on]).to eq [:create] | ||
end | ||
|
||
it 'should create a version' do | ||
modifier = CreateModifier.create!(:some_content => Faker::Lorem.sentence) | ||
expect(modifier.versions.last.event).to eq 'create' | ||
end | ||
end | ||
|
||
context 'when no callback-method used' do | ||
it 'should set paper_trail_options[:on] to [:create, :update, :destroy]' do | ||
modifier = DefaultModifier.create!(:some_content => Faker::Lorem.sentence) | ||
expect(modifier.paper_trail_options[:on]).to eq [:create, :update, :destroy] | ||
end | ||
|
||
it 'should default to track destroy' do | ||
modifier = DefaultModifier.create!(:some_content => Faker::Lorem.sentence) | ||
modifier.destroy | ||
expect(modifier.versions.last.event).to eq 'destroy' | ||
end | ||
|
||
it 'should default to track update' do | ||
modifier = DefaultModifier.create!(:some_content => Faker::Lorem.sentence) | ||
modifier.update_attributes! :some_content => 'modified' | ||
expect(modifier.versions.last.event).to eq 'update' | ||
end | ||
|
||
it 'should default to track create' do | ||
modifier = DefaultModifier.create!(:some_content => Faker::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 => Faker::Lorem.sentence) | ||
modifier.update_attributes!(:some_content => 'modified') | ||
modifier.test_destroy | ||
expect(modifier.versions.collect(&:event)).to eq ['create'] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class BeforeDestroyModifier < CallbackModifier | ||
has_paper_trail :on => [] | ||
paper_trail_on_destroy :before | ||
end | ||
|
||
class AfterDestroyModifier < CallbackModifier | ||
has_paper_trail :on => [] | ||
paper_trail_on_destroy :after | ||
end | ||
|
||
class NoArgDestroyModifier < CallbackModifier | ||
has_paper_trail :on => [] | ||
paper_trail_on_destroy | ||
end | ||
|
||
class UpdateModifier < CallbackModifier | ||
has_paper_trail :on => [] | ||
paper_trail_on_update | ||
end | ||
|
||
class CreateModifier < CallbackModifier | ||
has_paper_trail :on => [] | ||
paper_trail_on_create | ||
end | ||
|
||
class DefaultModifier < CallbackModifier | ||
has_paper_trail | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class CallbackModifier < ActiveRecord::Base | ||
has_paper_trail :on => [] | ||
|
||
def test_destroy | ||
transaction do | ||
run_callbacks(:destroy) do | ||
self.deleted = true | ||
save! | ||
end | ||
end | ||
end | ||
|
||
def flagged_deleted? | ||
deleted? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters