lib/copyable/model_hooks.rb in copyable-0.2.0 vs lib/copyable/model_hooks.rb in copyable-0.3.0

- old
+ new

@@ -14,33 +14,36 @@ private def self.disable_all_callbacks(klass) klass.class_eval do - # Don't do it if it was already done - return if self.method_defined? :__disabled__run_callbacks + return if self.method_defined? :old_save! # Don't do this more than once - alias_method :__disabled__run_callbacks, :run_callbacks - # We are violently duck-punching ActiveRecord because ActiveRecord - # gives us no way to turn off callbacks. My apologies to the - # squeamish. - def run_callbacks(kind, *args, &block) - if block_given? - yield - else - true - end + @all_callbacks_disabled = true + class << self + attr_reader :all_callbacks_disabled end + + alias_method :old_save!, :save! + + # Hold my beer while we bypass all the Rails callbacks + # in favor of some raw SQL + def save! + Copyable::Saver.save!(self) + end end end def self.reenable_all_callbacks(klass) klass.class_eval do - # Only do it if the disabled callbacks are defined - return unless self.method_defined? :__disabled__run_callbacks + return unless self.method_defined? :old_save! # Don't do this more than once + @all_callbacks_disabled = false + class << self + attr_reader :all_callbacks_disabled + end - alias_method :run_callbacks, :__disabled__run_callbacks - remove_method :__disabled__run_callbacks + alias_method :save!, :old_save! + remove_method :old_save! end end end end