module Searchkick::Model

Public Class Methods

clean_indices() click to toggle source
# File lib/searchkick/model.rb, line 57
def clean_indices
  searchkick_index.clean_indices
end
disable_search_callbacks() click to toggle source
# File lib/searchkick/model.rb, line 38
def disable_search_callbacks
  class_variable_set :@@searchkick_callbacks, false
end
enable_search_callbacks() click to toggle source
# File lib/searchkick/model.rb, line 34
def enable_search_callbacks
  class_variable_set :@@searchkick_callbacks, true
end
reindex(options = {})
Alias for: searchkick_reindex
search_callbacks?() click to toggle source
# File lib/searchkick/model.rb, line 42
def search_callbacks?
  class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks?
end
searchkick_create_index() click to toggle source
# File lib/searchkick/model.rb, line 65
def searchkick_create_index
  searchkick_index.create_index
end
searchkick_import(options = {}) click to toggle source
# File lib/searchkick/model.rb, line 61
def searchkick_import(options = {})
  (options[:index] || searchkick_index).import_scope(searchkick_klass)
end
searchkick_index() click to toggle source
# File lib/searchkick/model.rb, line 28
def searchkick_index
  index = class_variable_get :@@searchkick_index
  index = index.call if index.respond_to? :call
  Searchkick::Index.new(index, searchkick_options)
end
searchkick_index_options() click to toggle source
# File lib/searchkick/model.rb, line 69
def searchkick_index_options
  searchkick_index.index_options
end
searchkick_reindex(options = {}) click to toggle source
# File lib/searchkick/model.rb, line 46
def searchkick_reindex(options = {})
  unless options[:accept_danger]
    if (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) ||
      (respond_to?(:queryable) && queryable != unscoped.with_default_scope)
      raise Searchkick::DangerousOperation, "Only call reindex on models, not relations. Pass `accept_danger: true` if this is your intention."
    end
  end
  searchkick_index.reindex_scope(searchkick_klass, options)
end
Also aliased as: reindex

Public Instance Methods

reindex() click to toggle source
# File lib/searchkick/model.rb, line 83
def reindex
  self.class.searchkick_index.reindex_record(self)
end
reindex_async() click to toggle source
# File lib/searchkick/model.rb, line 87
def reindex_async
  self.class.searchkick_index.reindex_record_async(self)
end
search_data() click to toggle source
# File lib/searchkick/model.rb, line 95
def search_data
  respond_to?(:to_hash) ? to_hash : serializable_hash
end
searchkick(options = {}) click to toggle source
# File lib/searchkick/model.rb, line 5
def searchkick(options = {})
  raise "Only call searchkick once per model" if respond_to?(:searchkick_index)

  Searchkick.models << self

  class_eval do
    cattr_reader :searchkick_options, :searchkick_klass

    callbacks = options.key?(:callbacks) ? options[:callbacks] : true

    class_variable_set :@@searchkick_options, options.dup
    class_variable_set :@@searchkick_klass, self
    class_variable_set :@@searchkick_callbacks, callbacks
    class_variable_set :@@searchkick_index, options[:index_name] ||
      (options[:index_prefix].respond_to?(:call) && proc { [options[:index_prefix].call, model_name.plural, Searchkick.env].compact.join("_") }) ||
      [options[:index_prefix], model_name.plural, Searchkick.env].compact.join("_")

    class << self
      def searchkick_search(term = nil, options = {}, &block)
        searchkick_index.search_model(self, term, options, &block)
      end
      alias_method Searchkick.search_method_name, :searchkick_search if Searchkick.search_method_name

      def searchkick_index
        index = class_variable_get :@@searchkick_index
        index = index.call if index.respond_to? :call
        Searchkick::Index.new(index, searchkick_options)
      end

      def enable_search_callbacks
        class_variable_set :@@searchkick_callbacks, true
      end

      def disable_search_callbacks
        class_variable_set :@@searchkick_callbacks, false
      end

      def search_callbacks?
        class_variable_get(:@@searchkick_callbacks) && Searchkick.callbacks?
      end

      def searchkick_reindex(options = {})
        unless options[:accept_danger]
          if (respond_to?(:current_scope) && respond_to?(:default_scoped) && current_scope && current_scope.to_sql != default_scoped.to_sql) ||
            (respond_to?(:queryable) && queryable != unscoped.with_default_scope)
            raise Searchkick::DangerousOperation, "Only call reindex on models, not relations. Pass `accept_danger: true` if this is your intention."
          end
        end
        searchkick_index.reindex_scope(searchkick_klass, options)
      end
      alias_method :reindex, :searchkick_reindex unless method_defined?(:reindex)

      def clean_indices
        searchkick_index.clean_indices
      end

      def searchkick_import(options = {})
        (options[:index] || searchkick_index).import_scope(searchkick_klass)
      end

      def searchkick_create_index
        searchkick_index.create_index
      end

      def searchkick_index_options
        searchkick_index.index_options
      end
    end
    extend Searchkick::Reindex # legacy for Searchjoy

    callback_name = callbacks == :async ? :reindex_async : :reindex
    if respond_to?(:after_commit)
      after_commit callback_name, if: proc { self.class.search_callbacks? }
    elsif respond_to?(:after_save)
      after_save callback_name, if: proc { self.class.search_callbacks? }
      after_destroy callback_name, if: proc { self.class.search_callbacks? }
    end

    def reindex
      self.class.searchkick_index.reindex_record(self)
    end unless method_defined?(:reindex)

    def reindex_async
      self.class.searchkick_index.reindex_record_async(self)
    end unless method_defined?(:reindex_async)

    def similar(options = {})
      self.class.searchkick_index.similar_record(self, options)
    end unless method_defined?(:similar)

    def search_data
      respond_to?(:to_hash) ? to_hash : serializable_hash
    end unless method_defined?(:search_data)

    def should_index?
      true
    end unless method_defined?(:should_index?)
  end
end
should_index?() click to toggle source
# File lib/searchkick/model.rb, line 99
def should_index?
  true
end
similar(options = {}) click to toggle source
# File lib/searchkick/model.rb, line 91
def similar(options = {})
  self.class.searchkick_index.similar_record(self, options)
end