Sha256: 9cfd6e3811104cd11f5960886439fd5e3435ff1ed1419b6b96aebba13f0c5f0f

Contents?: true

Size: 1.32 KB

Versions: 7

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true
require 'active_record'

module ActiveModelCachers::Hook
  module OnModelDelete
    module InstanceMethods
      def delete
        self.class.delete(id, self) if persisted?
        @destroyed = true
        freeze
      end
    end

    module ClassMethods
      def prepend_before_delete(&callback)
        before_delete_hooks.unshift(callback)
      end

      def before_delete(&callback)
        before_delete_hooks << callback
      end

      def after_delete(&callback)
        after_delete_hooks << callback
      end

      def before_delete_hooks
        @before_delete_hooks ||= []
      end

      def after_delete_hooks
        @after_delete_hooks ||= []
      end

      def delete(id, model = nil)
        before_delete_hooks.each{|s| s.call(id, model) }
        result = super(id)
        after_delete_hooks.each{|s| s.call(id, model) }
        return result
      end

      def nullify_hooks_at(column)
        @nullify_hooks ||= Hash.new{|h, k| h[k] = [] }
        return @nullify_hooks[column]
      end

      def on_nullify(column, &callback)
        nullify_hooks_at(column) << callback
      end
    end
  end
end

ActiveRecord::Base.send(:include, ActiveModelCachers::Hook::OnModelDelete::InstanceMethods)
ActiveRecord::Base.send(:extend, ActiveModelCachers::Hook::OnModelDelete::ClassMethods)

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
active_model_cachers-2.1.2 lib/active_model_cachers/hook/on_model_delete.rb
active_model_cachers-2.1.1 lib/active_model_cachers/hook/on_model_delete.rb
active_model_cachers-2.1.0 lib/active_model_cachers/hook/on_model_delete.rb
active_model_cachers-2.0.3 lib/active_model_cachers/hook/on_model_delete.rb
active_model_cachers-2.0.2 lib/active_model_cachers/hook/on_model_delete.rb
active_model_cachers-2.0.1 lib/active_model_cachers/hook/on_model_delete.rb
active_model_cachers-2.0.0 lib/active_model_cachers/hook/on_model_delete.rb