updated README, set spec_helper back to original, append option to paper_trail_options[:on] when using callback-methods
This commit is contained in:
parent
0f40d74026
commit
8fe6ada23e
|
@ -330,6 +330,8 @@ a.versions.size # 3
|
|||
a.versions.last.event # 'update'
|
||||
```
|
||||
|
||||
### Controlling the Order of AR Callbacks
|
||||
|
||||
You can also use the corresponding callback-methods seperately instead of using
|
||||
the :on option. If you choose to use the callback-methods, PaperTrail will only
|
||||
track the according events - so `paper_trail_on_create` is basically the same as
|
||||
|
@ -344,7 +346,7 @@ class Article < ActiveRecord::Base
|
|||
end
|
||||
```
|
||||
|
||||
The `paper_trail_destroy` method can be configured to be called `:before` or `:after` the
|
||||
The `paper_trail_on_destroy` method can be configured to be called `:before` or `:after` the
|
||||
destroy event. This can be usefull if you are using a third party tool that alters the
|
||||
destroy method (for example paranoia). If you do not pass an argument, it will default
|
||||
to after_destroy.
|
||||
|
|
|
@ -50,11 +50,11 @@ module PaperTrail
|
|||
|
||||
# Wrap the :on option in an array if necessary. This allows a single
|
||||
# symbol to be passed in.
|
||||
options_on = Array(options[:on])
|
||||
options[:on] = Array(options[:on])
|
||||
|
||||
setup_model_for_paper_trail(options)
|
||||
|
||||
setup_callbacks_from_options options_on
|
||||
setup_callbacks_from_options options[:on]
|
||||
end
|
||||
|
||||
def setup_model_for_paper_trail(options = {})
|
||||
|
@ -110,19 +110,20 @@ module PaperTrail
|
|||
options_on.each do |option|
|
||||
send "paper_trail_on_#{option}"
|
||||
end
|
||||
|
||||
paper_trail_options[:on] = options_on
|
||||
end
|
||||
|
||||
# Record version before or after "destroy" event
|
||||
def paper_trail_on_destroy(recording_order = 'after')
|
||||
unless %(after before).include?(recording_order.to_s)
|
||||
unless %w[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?
|
||||
|
||||
return if paper_trail_options[:on].include?(:destroy)
|
||||
paper_trail_options[:on] << :destroy
|
||||
end
|
||||
|
||||
# Record version after "update" event
|
||||
|
@ -132,12 +133,18 @@ module PaperTrail
|
|||
after_update :record_update,
|
||||
:if => :save_version?
|
||||
after_update :clear_version_instance!
|
||||
|
||||
return if paper_trail_options[:on].include?(:update)
|
||||
paper_trail_options[:on] << :update
|
||||
end
|
||||
|
||||
# Record version after "create" event
|
||||
def paper_trail_on_create
|
||||
after_create :record_create,
|
||||
:if => :save_version?
|
||||
|
||||
return if paper_trail_options[:on].include?(:create)
|
||||
paper_trail_options[:on] << :create
|
||||
end
|
||||
|
||||
# Switches PaperTrail off for this class.
|
||||
|
|
|
@ -4,7 +4,12 @@ require 'support/callback_modifier'
|
|||
describe CallbackModifier, :type => :model do
|
||||
with_versioning do
|
||||
describe 'callback-methods', :versioning => true do
|
||||
describe 'paper_trail_destroy' 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)
|
||||
|
@ -30,7 +35,12 @@ describe CallbackModifier, :type => :model do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'paper_trail_update' do
|
||||
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'
|
||||
|
@ -38,7 +48,12 @@ describe CallbackModifier, :type => :model do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'paper_trail_create' do
|
||||
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'
|
||||
|
@ -46,6 +61,11 @@ describe CallbackModifier, :type => :model do
|
|||
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
|
||||
|
|
|
@ -37,8 +37,6 @@ RSpec.configure do |config|
|
|||
# `true` in RSpec 4.
|
||||
mocks.verify_partial_doubles = true
|
||||
end
|
||||
config.filter_run :focus
|
||||
config.run_all_when_everything_filtered = true
|
||||
|
||||
# The settings below are suggested to provide a good initial experience
|
||||
# with RSpec, but feel free to customize to your heart's content.
|
||||
|
@ -47,6 +45,8 @@ RSpec.configure do |config|
|
|||
# to individual examples or groups you care about by tagging them with
|
||||
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
||||
# get run.
|
||||
config.filter_run :focus
|
||||
config.run_all_when_everything_filtered = true
|
||||
|
||||
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
||||
# For more details, see:
|
||||
|
|
|
@ -14,15 +14,15 @@ class NoArgDestroyModifier < CallbackModifier
|
|||
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
|
||||
# Because of the way I set up the destroy method for testing
|
||||
# has_paper_trail has to be initialized in this model seperately
|
||||
has_paper_trail
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue