removed cleanup callback chain

This commit is contained in:
Ninigi 2015-09-23 17:45:45 +02:00
parent d63eb4e320
commit 0f40d74026
5 changed files with 43 additions and 120 deletions

View File

@ -337,7 +337,7 @@ track the according events - so `paper_trail_on_create` is basically the same as
```ruby
class Article < ActiveRecord::Base
has_paper_trail
has_paper_trail :on => []
paper_trail_on_destroy
paper_trail_on_update
paper_trail_on_create

View File

@ -1,100 +0,0 @@
module PaperTrail
module Callbacks
# The cleanup_* methods are only used to provide backward-compatibility
# They should be removed as soon as the "traditional" way of using
# PaperTrail `has_papertrail :on => [...]` with or without the :on option
# and not setting the paper_trail_* methods is no longer supported.
def setup_callbacks_from_options(options_on = [])
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)
fail ArgumentError, 'recording order can only be "after" or "before"'
end
cleanup_callback_chain
send "#{recording_order}_destroy",
:record_destroy,
:if => :save_version?
end
# Record version after "update" event
def paper_trail_on_update
cleanup_callback_chain
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_on_create
cleanup_callback_chain
after_create :record_create,
:if => :save_version?
end
private
# The cleanup_* methods are only used to provide backward-compatibility
# They should be removed as soon as the "traditional" way of using
# PaperTrail `has_papertrail :on => [...]` with or without the :on option
# and not setting the paper_trail_* methods is no longer supported.
def cleanup_callback_chain
on_options = paper_trail_options.try(:delete, :on) || []
on_options.each do |on_option|
send "cleanup_#{on_option}_callbacks"
end
end
def cleanup_create_callbacks
callback =
_create_callbacks.find do |cb|
cb.filter.eql?(:record_create) && cb.kind.eql?(:after)
end
_create_callbacks.delete(callback)
end
def cleanup_update_callbacks
callback =
_save_callbacks.find do |cb|
cb.filter.eql?(:reset_timestamp_attrs_for_update_if_needed!) && cb.kind.eql?(:before)
end
_save_callbacks.delete(callback)
callback =
_update_callbacks.find do |cb|
cb.filter.eql?(:record_update) && cb.kind.eql?(:after)
end
_update_callbacks.delete(callback)
callback =
_update_callbacks.find do |cb|
cb.filter.eql?(:clear_version_instance!) && cb.kind.eql?(:after)
end
_update_callbacks.delete(callback)
end
def cleanup_destroy_callbacks
callback =
_destroy_callbacks.find do |cb|
cb.filter.eql?(:record_destroy)
end
_destroy_callbacks.delete(callback)
end
end
end

View File

@ -1,5 +1,4 @@
require 'active_support/core_ext/object' # provides the `try` method
require 'paper_trail/callbacks'
module PaperTrail
module Model
@ -9,7 +8,6 @@ module PaperTrail
end
module ClassMethods
include Callbacks
# Declare this in your model to track every create, update, and destroy.
# Each version of the model is available in the `versions` association.
#
@ -48,26 +46,15 @@ module PaperTrail
# column if it exists. Default is true
#
def has_paper_trail(options = {})
# Initializing paper_trail_options with an empty hash before setting
# up the callback-methods is important to make them compatible with the
# traditional has_paper_trail method.
# This can move to setup_model_for_paper_trail, as soon as the
# cleanup_callback and setup_callbacks_from_options method is no longer
# needed.
class_attribute :paper_trail_options
self.paper_trail_options = {}
# TODO: Add a deprecation warning / info to use callback methods instead
# of the :on option?
options[:on] ||= [:create, :update, :destroy]
# Wrap the :on option in an array if necessary. This allows a single
# symbol to be passed in.
options_on = Array(options[:on])
setup_callbacks_from_options options_on
setup_model_for_paper_trail(options)
setup_callbacks_from_options options_on
end
def setup_model_for_paper_trail(options = {})
@ -84,6 +71,8 @@ module PaperTrail
class_attribute :version_class_name
self.version_class_name = options[:class_name] || 'PaperTrail::Version'
class_attribute :paper_trail_options
self.paper_trail_options = options.dup
[:ignore, :skip, :only].each do |k|
@ -117,6 +106,40 @@ module PaperTrail
after_rollback :clear_rolled_back_versions
end
def setup_callbacks_from_options(options_on = [])
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)
fail ArgumentError, 'recording order can only be "after" or "before"'
end
send "#{recording_order}_destroy",
:record_destroy,
:if => :save_version?
end
# Record version after "update" event
def paper_trail_on_update
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_on_create
after_create :record_create,
:if => :save_version?
end
# Switches PaperTrail off for this class.
def paper_trail_off!
PaperTrail.enabled_for_model(self, false)

View File

@ -1,15 +1,15 @@
class BeforeDestroyModifier < CallbackModifier
has_paper_trail
has_paper_trail :on => []
paper_trail_on_destroy :before
end
class AfterDestroyModifier < CallbackModifier
has_paper_trail
has_paper_trail :on => []
paper_trail_on_destroy :after
end
class NoArgDestroyModifier < CallbackModifier
has_paper_trail
has_paper_trail :on => []
paper_trail_on_destroy
end

View File

@ -1,5 +1,5 @@
class CallbackModifier < ActiveRecord::Base
has_paper_trail
has_paper_trail :on => []
def test_destroy
transaction do