Module: Tire::Model::Search

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

Overview

Main module containing the search infrastructure for ActiveModel classes.

By including this module, you’ll provide the model with facilities to perform searches against index, define index settings and mappings, access the index object, etc.

All the Tire methods are accessible via the “proxy” class and instance methods of the model, named ‘tire`, eg. `Article.tire.search ’foo’‘.

When there’s no clash with a method in the class (your own, defined by another gem, etc) Tire will bring these methods to the top-level namespace of the class, eg. ‘Article.search ’foo’‘.

You’ll find the relevant methods in the ClassMethods and InstanceMethods module.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, Loader Classes: ClassMethodsProxy, InstanceMethodsProxy

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

A hook triggered by the ‘include Tire::Model::Search` statement in the model.



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/tire/model/search.rb', line 240

def self.included(base)
  base.class_eval do

    # Returns proxy to the _Tire's_ class methods.
    #
    def self.tire &block
      @__tire__ ||= ClassMethodsProxy.new(self)

      @__tire__.instance_eval(&block) if block_given?
      @__tire__
    end

    # Returns proxy to the _Tire's_ instance methods.
    #
    def tire &block
      @__tire__ ||= InstanceMethodsProxy.new(self)

      @__tire__.instance_eval(&block) if block_given?
      @__tire__
    end

    # Define _Tire's_ callbacks (<after|before>_update_elasticsearch_index).
    #
    define_model_callbacks(:update_elasticsearch_index, :only => [:after, :before]) if \
      respond_to?(:define_model_callbacks)

    # Serialize the model as a Hash.
    #
    # Uses `serializable_hash` representation of the model,
    # unless implemented in the model already.
    #
    def to_hash
      self.serializable_hash
    end unless instance_methods.map(&:to_sym).include?(:to_hash)

  end

  # Alias _Tire's_ class methods in the top-level namespace of the model,
  # unless there's a conflict with existing method.
  #
  ClassMethodsProxy::INTERFACE.each do |method|
    base.class_eval <<-"end;", __FILE__, __LINE__ unless base.public_methods.map(&:to_sym).include?(method.to_sym)
      def self.#{method}(*args, &block)                     # def search(*args, &block)
        tire.__send__(#{method.inspect}, *args, &block)     #   tire.__send__(:search, *args, &block)
      end                                                   # end
    end;
  end

  # Alias _Tire's_ instance methods in the top-level namespace of the model,
  # unless there's a conflict with existing method
  InstanceMethodsProxy::INTERFACE.each do |method|
    base.class_eval <<-"end;", __FILE__, __LINE__ unless base.instance_methods.map(&:to_sym).include?(method.to_sym)
      def #{method}(*args, &block)                          # def to_indexed_json(*args, &block)
        tire.__send__(#{method.inspect}, *args, &block)     #   tire.__send__(:to_indexed_json, *args, &block)
      end                                                   # end
    end;
  end

  # Include the `load` functionality in Results::Item
  #
  Results::Item.send :include, Loader
end

.index_prefix(*args) ⇒ Object

Alias for Tire::Model::Naming::ClassMethods.index_prefix



24
25
26
# File 'lib/tire/model/search.rb', line 24

def self.index_prefix(*args)
  Naming::ClassMethods.index_prefix(*args)
end