From 0f40d740260d338e02f079b18411244217542982 Mon Sep 17 00:00:00 2001 From: Ninigi Date: Wed, 23 Sep 2015 17:45:45 +0200 Subject: [PATCH] removed cleanup callback chain --- README.md | 2 +- lib/paper_trail/callbacks.rb | 100 --------------------- lib/paper_trail/has_paper_trail.rb | 53 +++++++---- spec/support/callback_modifier.rb | 6 +- test/dummy/app/models/callback_modifier.rb | 2 +- 5 files changed, 43 insertions(+), 120 deletions(-) delete mode 100644 lib/paper_trail/callbacks.rb diff --git a/README.md b/README.md index fff9506e..629eea67 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/paper_trail/callbacks.rb b/lib/paper_trail/callbacks.rb deleted file mode 100644 index dc0299f7..00000000 --- a/lib/paper_trail/callbacks.rb +++ /dev/null @@ -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 diff --git a/lib/paper_trail/has_paper_trail.rb b/lib/paper_trail/has_paper_trail.rb index bfbb0462..d976e3ae 100644 --- a/lib/paper_trail/has_paper_trail.rb +++ b/lib/paper_trail/has_paper_trail.rb @@ -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) diff --git a/spec/support/callback_modifier.rb b/spec/support/callback_modifier.rb index 14914f0f..04de7529 100644 --- a/spec/support/callback_modifier.rb +++ b/spec/support/callback_modifier.rb @@ -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 diff --git a/test/dummy/app/models/callback_modifier.rb b/test/dummy/app/models/callback_modifier.rb index ab97000b..0233899c 100644 --- a/test/dummy/app/models/callback_modifier.rb +++ b/test/dummy/app/models/callback_modifier.rb @@ -1,5 +1,5 @@ class CallbackModifier < ActiveRecord::Base - has_paper_trail + has_paper_trail :on => [] def test_destroy transaction do