Module: Tire::Model::AsyncCallbacks

Defined in:
lib/tire/model/async_callbacks.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tire/model/async_callbacks.rb', line 4

def self.included(base)
  if base.respond_to?(:after_save) && base.respond_to?(:after_destroy)
    base.send :after_save,    lambda { 
      case TireAsyncIndex.engine
      when :sidekiq
        SidekiqUpdateIndexWorker.perform_async(base.name, id)
      when :resque
        Resque.enqueue(ResqueUpdateIndexJob, base.name, id)
      else
        tire.update_index
      end
      self 
    }
    base.send :after_destroy, lambda { tire.update_index }, :order => :first
  end

  if base.respond_to?(:before_destroy) && !base.instance_methods.map(&:to_sym).include?(:destroyed?)
    base.class_eval do
      before_destroy  { @destroyed = true }
      def destroyed?; !!@destroyed; end
    end
  end

end