Sha256: 273c92e6fcec2e265ff6919692f69d552236392963efd22ab7ba17297579b619

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

module Copyable
  class ModelHooks

    # Disabling callbacks automatically disables any registered observers,
    # since observers use the callback mechanism internally.

    def self.disable!(klass)
      disable_all_callbacks(klass)
    end

    def self.reenable!(klass)
      reenable_all_callbacks(klass)
    end

  private

    def self.disable_all_callbacks(klass)
      klass.class_eval do
        return if self.method_defined? :old_save! # Don't do this more than once

        @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
        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 :save!, :old_save!
        remove_method :old_save!
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
copyable-0.3.5 lib/copyable/model_hooks.rb
copyable-0.3.3 lib/copyable/model_hooks.rb
copyable-0.3.2 lib/copyable/model_hooks.rb
copyable-0.3.1 lib/copyable/model_hooks.rb
copyable-0.3.0 lib/copyable/model_hooks.rb