class AlgoliaSearch::IndexSettings

Constants

DEFAULT_BATCH_SIZE
OPTIONS

AlgoliaSearch settings

Public Class Methods

new(options, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 91
def initialize(options, &block)
  @options = options
  instance_exec(&block) if block_given?
end

Public Instance Methods

add_attribute(*names, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 110
def add_attribute(*names, &block)
  raise ArgumentError.new('Cannot pass multiple attribute names if block given') if block_given? and names.length > 1
  raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:replica]
  @additional_attributes ||= {}
  names.each do |name|
    @additional_attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
  end
end
Also aliased as: add_attributes
add_attributes(*names, &block)
Alias for: add_attribute
add_index(index_name, options = {}, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 271
def add_index(index_name, options = {}, &block)
  raise ArgumentError.new('Cannot specify additional index on a replica index') if @options[:replica]
  raise ArgumentError.new('No block given') if !block_given?
  raise ArgumentError.new('Options auto_index and auto_remove cannot be set on nested indexes') if options[:auto_index] || options[:auto_remove]
  @additional_indexes ||= {}
  options[:index_name] = index_name
  @additional_indexes[options] = IndexSettings.new(options, &block)
end
add_replica(index_name, options = {}, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 280
def add_replica(index_name, options = {}, &block)
  raise ArgumentError.new('Cannot specify additional replicas on a replica index') if @options[:replica]
  raise ArgumentError.new('No block given') if !block_given?
  add_index(index_name, options.merge({ :replica => true, :primary_settings => self }), &block)
end
additional_indexes() click to toggle source
# File lib/algoliasearch-rails.rb, line 286
def additional_indexes
  @additional_indexes || {}
end
attribute(*names, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 100
def attribute(*names, &block)
  raise ArgumentError.new('Cannot pass multiple attribute names if block given') if block_given? and names.length > 1
  raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:replica]
  @attributes ||= {}
  names.flatten.each do |name|
    @attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) }
  end
end
Also aliased as: attributes
attributes(*names, &block)
Alias for: attribute
attributes_to_hash(attributes, object) click to toggle source
# File lib/algoliasearch-rails.rb, line 149
def attributes_to_hash(attributes, object)
  if attributes
    Hash[attributes.map { |name, value| [name.to_s, value.call(object) ] }]
  else
    {}
  end
end
encode_attributes(v) click to toggle source
# File lib/algoliasearch-rails.rb, line 201
def encode_attributes(v)
  case v
  when String
    v.dup.force_encoding('utf-8')
  when Hash
    v.each { |key, value| v[key] = encode_attributes(value) }
  when Array
    v.map { |x| encode_attributes(x) }
  else
    v
  end
end
geoloc(lat_attr = nil, lng_attr = nil, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 214
def geoloc(lat_attr = nil, lng_attr = nil, &block)
  raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:replica]
  add_attribute :_geoloc do |o|
    block_given? ? o.instance_eval(&block) : { :lat => o.send(lat_attr).to_f, :lng => o.send(lng_attr).to_f }
  end
end
get_attribute_names(object) click to toggle source
# File lib/algoliasearch-rails.rb, line 145
def get_attribute_names(object)
  get_attributes(object).keys
end
get_attributes(object) click to toggle source
# File lib/algoliasearch-rails.rb, line 157
def get_attributes(object)
  # If a serializer is set, we ignore attributes
  # everything should be done via the serializer
  if not @serializer.nil?
    attributes = @serializer.new(object).attributes
  else
    if @attributes.nil? || @attributes.length == 0
      # no `attribute ...` have been configured, use the default attributes of the model
      attributes = get_default_attributes(object)
    else
      # at least 1 `attribute ...` has been configured, therefore use ONLY the one configured
      if is_active_record?(object)
        object.class.unscoped do
          attributes = attributes_to_hash(@attributes, object)
        end
      else
        attributes = attributes_to_hash(@attributes, object)
      end
    end
  end

  attributes.merge!(attributes_to_hash(@additional_attributes, object)) if @additional_attributes
  attributes = sanitize_attributes(attributes, Rails::Html::FullSanitizer.new) if @options[:sanitize]

  if @options[:force_utf8_encoding] && Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f > 1.8
    attributes = encode_attributes(attributes)
  end

  attributes
end
get_default_attributes(object) click to toggle source
# File lib/algoliasearch-rails.rb, line 132
def get_default_attributes(object)
  if is_mongoid?(object)
    # work-around mongoid 2.4's unscoped method, not accepting a block
    object.attributes
  elsif is_sequel?(object)
    object.to_hash
  else
    object.class.unscoped do
      object.attributes
    end
  end
end
get_setting(name) click to toggle source
# File lib/algoliasearch-rails.rb, line 229
def get_setting(name)
  instance_variable_get("@#{name}")
end
is_active_record?(object) click to toggle source
# File lib/algoliasearch-rails.rb, line 128
def is_active_record?(object)
  !is_mongoid?(object) && !is_sequel?(object)
end
is_mongoid?(object) click to toggle source
# File lib/algoliasearch-rails.rb, line 120
def is_mongoid?(object)
  defined?(::Mongoid::Document) && object.class.include?(::Mongoid::Document)
end
is_sequel?(object) click to toggle source
# File lib/algoliasearch-rails.rb, line 124
def is_sequel?(object)
  defined?(::Sequel) && defined?(::Sequel::Model) && object.class < ::Sequel::Model
end
sanitize_attributes(v, sanitizer) click to toggle source
# File lib/algoliasearch-rails.rb, line 188
def sanitize_attributes(v, sanitizer)
  case v
  when String
    sanitizer.sanitize(v)
  when Hash
    v.each { |key, value| v[key] = sanitize_attributes(value, sanitizer) }
  when Array
    v.map { |x| sanitize_attributes(x, sanitizer) }
  else
    v
  end
end
setting_name(name) click to toggle source
# File lib/algoliasearch-rails.rb, line 263
def setting_name(name)
  name.to_s.gsub(/::/, '/').
      gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
      gsub(/([a-z\d])([A-Z])/,'\1_\2').
      tr("-", "_").
      downcase
end
tags(*args, &block) click to toggle source
# File lib/algoliasearch-rails.rb, line 221
def tags(*args, &block)
  raise ArgumentError.new('Cannot specify additional attributes on a replica index') if @options[:replica]
  add_attribute :_tags do |o|
    v = block_given? ? o.instance_eval(&block) : args
    v.is_a?(Array) ? v : [v]
  end
end
to_hash() click to toggle source
# File lib/algoliasearch-rails.rb, line 243
def to_hash
  settings = {}
  OPTIONS.each do |k|
    v = get_setting(k)
    settings[setting_name(k)] = v if !v.nil?
  end

  if !@options[:replica]
    settings[:replicas] = additional_indexes.select { |opts, s| opts[:replica] }.map do |opts, s|
      name = opts[:index_name]
      name = "#{name}_#{Rails.env.to_s}" if opts[:per_environment]
      name = "virtual(#{name})" if opts[:virtual]
      name
    end
    settings.delete(:replicas) if settings[:replicas].empty?
  end

  settings
end
to_settings() click to toggle source
# File lib/algoliasearch-rails.rb, line 233
def to_settings
  settings = to_hash

  # Remove the synonyms setting since those need to be set separately
  settings.delete(:synonyms)
  settings.delete("synonyms")

  Algolia::Search::IndexSettings.new(settings)
end
use_serializer(serializer) click to toggle source
# File lib/algoliasearch-rails.rb, line 96
def use_serializer(serializer)
  @serializer = serializer
end