Module: Tire::Model::Callbacks

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

Overview

Main module containing the infrastructure for automatic updating of the Elasticsearch index on model instance create, update or delete.

Include it in your model: include Tire::Model::Callbacks

The model must respond to after_save and after_destroy callbacks (ActiveModel and ActiveRecord models do so, by default).

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

A hook triggered by the include Tire::Model::Callbacks statement in the model.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tire/model/callbacks.rb', line 16

def self.included(base)

  # Update index on model instance change or destroy.
  #
  if base.respond_to?(:after_save) && base.respond_to?(:after_destroy)
    base.send :after_save,    lambda { tire.update_index }
    base.send :after_destroy, lambda { tire.update_index }
  end

  # Add neccessary infrastructure for the model, when missing in
  # some half-baked ActiveModel implementations.
  #
  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