lib/meilisearch-rails.rb in meilisearch-rails-0.1.0 vs lib/meilisearch-rails.rb in meilisearch-rails-0.2.0

- old
+ new

@@ -50,20 +50,29 @@ DEFAULT_PRIMARY_KEY = 'id' # MeiliSearch settings OPTIONS = [ - :searchableAttributes, :attributesForFaceting, :displayedAttributes, :distinctAttribute, - :synonyms, :stopWords, :rankingRules, + :searchableAttributes, + :attributesForFaceting, + :displayedAttributes, + :distinctAttribute, + :synonyms, + :stopWords, + :rankingRules, :attributesToHighlight, - :attributesToCrop, :cropLength + :attributesToCrop, + :cropLength, ] - OPTIONS.each do |k| - define_method k do |v| - instance_variable_set("@#{k}", v) + OPTIONS.each do |option| + define_method option do |value| + instance_variable_set("@#{option}", value) end + + underscored_name = option.to_s.gsub(/(.)([A-Z])/, '\1_\2').downcase + alias_method underscored_name, option if underscored_name != option end def initialize(options, &block) @options = options instance_exec(&block) if block_given? @@ -313,11 +322,11 @@ base.cattr_accessor :meilisearch_options, :meilisearch_settings end def meilisearch(options = {}, &block) self.meilisearch_settings = IndexSettings.new(options, &block) - self.meilisearch_options = { :type => ms_full_const_get(model_name.to_s), :per_page => meilisearch_settings.get_setting(:hitsPerPage) || 20, :page => 1 }.merge(options) + self.meilisearch_options = { type: ms_full_const_get(model_name.to_s), per_page: meilisearch_settings.get_setting(:hitsPerPage) || 20, page: 1 }.merge(options) attr_accessor :formatted if options[:synchronous] == true if defined?(::Sequel) && self < Sequel::Model @@ -550,12 +559,10 @@ if !meilisearch_settings.get_setting(:attributesToCrop).nil? params[:attributesToCrop] = meilisearch_settings.get_setting(:attributesToCrop) params[:cropLength] = meilisearch_settings.get_setting(:cropLength) if !meilisearch_settings.get_setting(:cropLength).nil? end index = ms_index(index_uid) - # index = ms_index(ms_index_uid) - # index.search(q, Hash[params.map { |k,v| [k.to_s, v.to_s] }]) index.search(q, Hash[params.map { |k,v| [k, v] }]) end module AdditionalMethods def self.extended(base) @@ -588,18 +595,10 @@ params.delete(:page) params.delete(:hitsPerPage) params[:limit] = 200 end - if !meilisearch_settings.get_setting(:attributesToHighlight).nil? - params[:attributesToHighlight] = meilisearch_settings.get_setting(:attributesToHighlight) - end - - if !meilisearch_settings.get_setting(:attributesToCrop).nil? - params[:attributesToCrop] = meilisearch_settings.get_setting(:attributesToCrop) - params[:cropLength] = meilisearch_settings.get_setting(:cropLength) if !meilisearch_settings.get_setting(:cropLength).nil? - end # Returns raw json hits as follows: # {"hits"=>[{"id"=>"13", "href"=>"apple", "name"=>"iphone"}], "offset"=>0, "limit"=>|| 20, "nbHits"=>1, "exhaustiveNbHits"=>false, "processingTimeMs"=>0, "query"=>"iphone"} json = ms_raw_search(q, params) # Returns the ids of the hits: 13 @@ -630,11 +629,11 @@ total_hits = json['hits'].length hits_per_page ||= 20 page ||= 1 - res = MeiliSearch::Pagination.create(results, total_hits, meilisearch_options.merge({ :page => page , :per_page => hits_per_page })) + res = MeiliSearch::Pagination.create(results, total_hits, meilisearch_options.merge({ page: page , per_page: hits_per_page })) res.extend(AdditionalMethods) res.send(:ms_init_raw_answer, json) res end @@ -821,10 +820,10 @@ raise ArgumentError, "Unknown constraint type: #{constraint} (#{constraint.class})" end def ms_find_in_batches(batch_size, &block) if (defined?(::ActiveRecord) && ancestors.include?(::ActiveRecord::Base)) || respond_to?(:find_in_batches) - find_in_batches(:batch_size => batch_size, &block) + find_in_batches(batch_size: batch_size, &block) elsif defined?(::Sequel) && self < Sequel::Model dataset.extension(:pagination).each_page(batch_size, &block) else # don't worry, mongoid has its own underlying cursor/streaming mechanism items = []