-
Notifications
You must be signed in to change notification settings - Fork 895
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ninigi
committed
Sep 10, 2015
1 parent
a97850a
commit 9008d67
Showing
10 changed files
with
243 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module PaperTrail | ||
module Callbacks | ||
def setup_callbacks_from_options(options_on, options = {}) | ||
options_on.each do |option| | ||
send "paper_trail_#{option}", options | ||
end | ||
end | ||
|
||
# Record version before or after "destroy" event | ||
def paper_trail_destroy(options = {}) | ||
setup_model_if_necessary options | ||
recording_order = options[:recording_order] || 'after' | ||
|
||
unless %(after before).include?(recording_order.to_s) | ||
fail ArgumentError, 'recording order can only be "after" or "before"' | ||
end | ||
|
||
send "#{recording_order}_destroy", | ||
:record_destroy, | ||
:if => :save_version? | ||
end | ||
|
||
# Record version after "destroy" event | ||
def paper_trail_after_destroy(options = {}) | ||
options[:recording_order] = :after | ||
paper_trail_destroy options | ||
end | ||
|
||
# Record version before "destroy" event | ||
def paper_trail_before_destroy(options = {}) | ||
options[:recording_order] = :before | ||
paper_trail_destroy options | ||
end | ||
|
||
# Record version after "update" event | ||
def paper_trail_update(options = {}) | ||
setup_model_if_necessary options | ||
before_save :reset_timestamp_attrs_for_update_if_needed!, | ||
:on => :update | ||
after_update :record_update, | ||
:if => :save_version? | ||
after_update :clear_version_instance! | ||
end | ||
|
||
# Record version after "create" event | ||
def paper_trail_create(options = {}) | ||
setup_model_if_necessary options | ||
after_create :record_create, | ||
:if => :save_version? | ||
end | ||
|
||
private | ||
|
||
def setup_model_if_necessary(options) | ||
return true if model_set_up? | ||
|
||
setup_model_for_paper_trail options | ||
@_set_up = true | ||
end | ||
|
||
def model_set_up? | ||
@_set_up | ||
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
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,77 @@ | ||
require 'rails_helper' | ||
require 'support/callback_modifier' | ||
|
||
describe CallbackModifier, :type => :model do | ||
with_versioning do | ||
describe 'callback-methods', :versioning => true do | ||
describe 'paper_trail_destroy' do | ||
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_update' do | ||
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_create' do | ||
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 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.last.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,23 @@ | ||
class BeforeDestroyModifier < CallbackModifier | ||
paper_trail_before_destroy | ||
end | ||
|
||
class AfterDestroyModifier < CallbackModifier | ||
paper_trail_after_destroy | ||
end | ||
|
||
class NoArgDestroyModifier < CallbackModifier | ||
paper_trail_destroy | ||
end | ||
|
||
class UpdateModifier < CallbackModifier | ||
paper_trail_update | ||
end | ||
|
||
class CreateModifier < CallbackModifier | ||
paper_trail_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,18 @@ | ||
class CallbackModifier < ActiveRecord::Base | ||
# This will not be directly instantiated, but we need to set paper_trail up | ||
# before we can run tests on fake classes inheriting from CallbackModifier | ||
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