module RediSearch::RediSearchable::ClassMethods

Attributes

redisearch_index[R]
redisearch_index_options[R]
redisearch_index_serializer[R]

Public Instance Methods

redisearch(*args, schema:, **options) click to toggle source
# File lib/redisearch-rails/redisearchable/class_methods.rb, line 5
def redisearch(*args, schema:, **options)
  options = RediSearch.model_options.merge(options)

  raise "Only call redisearch once per model" if respond_to?(:redisearch_index)

  prefix = options[:prefix] || RediSearch.index_prefix
  prefix = prefix.call if prefix.respond_to?(:call)

  suffix = options[:suffix] || RediSearch.index_suffix
  suffix = suffix.call if suffix.respond_to?(:call)

  callbacks = options.key?(:callbacks) ? options[:callbacks] : :inline
  unless [:inline, true, false, :async].include?(callbacks)
    raise ArgumentError, "#{callbacks} its not permited value for callbacks"
  end

  class << self
    attr_reader :redisearch_index, :redisearch_index_serializer, :redisearch_index_options
  end

  index_name = [prefix, model_name.plural, suffix].compact.join("_")
  @redisearch_index_serializer = options[:index_serializer]
  @redisearch_index = RediSearch.client.generate_index(index_name, schema)

  RediSearch.models << self

  @redisearch_index_options = options

  scope :redisearch_import, -> { all }

  # always add callbacks, even when callbacks is false
  # so Model.callbacks block can be used
  if respond_to?(:after_commit)
    after_commit :reindex, if: -> { RediSearch.callbacks?(default: callbacks) }
  elsif respond_to?(:after_save)
    after_save :reindex, if: -> { RediSearch.callbacks?(default: callbacks) }
    after_destroy :reindex, if: -> { RediSearch.callbacks?(default: callbacks) }
  end

  include InstanceMethods
  extend RediSearchClassMethods
end