Class: AlgoliaSearch::IndexSettings
- Inherits:
-
Object
- Object
- AlgoliaSearch::IndexSettings
- Defined in:
- lib/algoliasearch-rails.rb
Constant Summary collapse
- OPTIONS =
AlgoliaSearch settings
[:minWordSizefor1Typo, :minWordSizefor2Typos, :hitsPerPage, :attributesToRetrieve, :attributesToHighlight, :attributesToSnippet, :attributesToIndex, :highlightPreTag, :highlightPostTag, :ranking, :customRanking, :queryType, :attributesForFaceting, :separatorsToIndex, :optionalWords, :attributeForDistinct, :synonyms, :placeholders]
Instance Method Summary collapse
- #add_attribute(*names, &block) ⇒ Object (also: #add_attributes)
- #add_index(index_name, options = {}, &block) ⇒ Object
- #add_slave(index_name, options = {}, &block) ⇒ Object
- #additional_indexes ⇒ Object
- #attribute(*names, &block) ⇒ Object (also: #attributes)
- #encode_attributes(v) ⇒ Object
- #geoloc(lat_attr, lng_attr) ⇒ Object
- #get_attributes(object) ⇒ Object
- #get_setting(name) ⇒ Object
-
#initialize(options, block) ⇒ IndexSettings
constructor
A new instance of IndexSettings.
- #sanitize_attributes(v, sanitizer) ⇒ Object
- #tags(*args, &block) ⇒ Object
- #to_settings ⇒ Object
Constructor Details
#initialize(options, block) ⇒ IndexSettings
Returns a new instance of IndexSettings.
63 64 65 66 |
# File 'lib/algoliasearch-rails.rb', line 63 def initialize(, block) @options = instance_exec(&block) if block end |
Instance Method Details
#add_attribute(*names, &block) ⇒ Object Also known as: add_attributes
78 79 80 81 82 83 84 85 |
# File 'lib/algoliasearch-rails.rb', line 78 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 slave index') if @options[:slave] @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 |
#add_index(index_name, options = {}, &block) ⇒ Object
181 182 183 184 185 186 187 188 |
# File 'lib/algoliasearch-rails.rb', line 181 def add_index(index_name, = {}, &block) raise ArgumentError.new('Cannot specify additional index on a slave index') if @options[:slave] 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 [:auto_index] || [:auto_remove] [:index_name] = index_name @additional_indexes ||= {} @additional_indexes[] = IndexSettings.new(, Proc.new) end |
#add_slave(index_name, options = {}, &block) ⇒ Object
190 191 192 193 194 |
# File 'lib/algoliasearch-rails.rb', line 190 def add_slave(index_name, = {}, &block) raise ArgumentError.new('Cannot specify additional slaves on a slave index') if @options[:slave] raise ArgumentError.new('No block given') if !block_given? add_index(index_name, .merge({ :slave => true }), &block) end |
#additional_indexes ⇒ Object
196 197 198 |
# File 'lib/algoliasearch-rails.rb', line 196 def additional_indexes @additional_indexes || {} end |
#attribute(*names, &block) ⇒ Object Also known as: attributes
68 69 70 71 72 73 74 75 |
# File 'lib/algoliasearch-rails.rb', line 68 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 slave index') if @options[:slave] @attributes ||= {} names.each do |name| @attributes[name.to_s] = block_given? ? Proc.new { |o| o.instance_eval(&block) } : Proc.new { |o| o.send(name) } end end |
#encode_attributes(v) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/algoliasearch-rails.rb', line 135 def encode_attributes(v) case v when String v.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, lng_attr) ⇒ Object
148 149 150 151 152 153 |
# File 'lib/algoliasearch-rails.rb', line 148 def geoloc(lat_attr, lng_attr) raise ArgumentError.new('Cannot specify additional attributes on a slave index') if @options[:slave] add_attribute :_geoloc do |o| { :lat => o.send(lat_attr).to_f, :lng => o.send(lng_attr).to_f } end end |
#get_attributes(object) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/algoliasearch-rails.rb', line 88 def get_attributes(object) clazz = object.class attributes = if defined?(::Mongoid::Document) && clazz.include?(::Mongoid::Document) # work-around mongoid 2.4's unscoped method, not accepting a block res = @attributes.nil? || @attributes.length == 0 ? object.attributes : Hash[@attributes.map { |name, value| [name.to_s, value.call(object) ] }] @additional_attributes.each { |name, value| res[name.to_s] = value.call(object) } if @additional_attributes res else object.class.unscoped do res = @attributes.nil? || @attributes.length == 0 ? object.attributes : Hash[@attributes.map { |name, value| [name.to_s, value.call(object) ] }] @additional_attributes.each { |name, value| res[name.to_s] = value.call(object) } if @additional_attributes res end end if @options[:sanitize] sanitizer = begin ::HTML::FullSanitizer.new rescue NameError # from rails 4.2 ::Rails::Html::FullSanitizer.new end attributes = sanitize_attributes(attributes, sanitizer) end if @options[:force_utf8_encoding] && Object.const_defined?(:RUBY_VERSION) && RUBY_VERSION.to_f > 1.8 attributes = encode_attributes(attributes) end attributes end |
#get_setting(name) ⇒ Object
163 164 165 |
# File 'lib/algoliasearch-rails.rb', line 163 def get_setting(name) instance_variable_get("@#{name}") end |
#sanitize_attributes(v, sanitizer) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/algoliasearch-rails.rb', line 122 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 |
#tags(*args, &block) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/algoliasearch-rails.rb', line 155 def (*args, &block) raise ArgumentError.new('Cannot specify additional attributes on a slave index') if @options[:slave] add_attribute :_tags do |o| v = block_given? ? o.instance_eval(&block) : args v.is_a?(Array) ? v : [v] end end |
#to_settings ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/algoliasearch-rails.rb', line 167 def to_settings settings = {} OPTIONS.each do |k| v = get_setting(k) settings[k] = v if !v.nil? end settings[:slaves] = additional_indexes.select { |, s| [:slave] }.map do |, s| name = [:index_name] name = "#{name}_#{Rails.env.to_s}" if [:per_environment] name end if !@options[:slave] settings end |