From ef6b0199fe694300a8316e891f1b20e4b96bd4f9 Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Mon, 12 Feb 2024 16:37:50 +0100 Subject: [PATCH] Fix linter problems --- lib/meilisearch/rails/multi_search/result.rb | 18 +++++----- lib/meilisearch/rails/utilities.rb | 4 +-- spec/multi_search/result_spec.rb | 38 ++++++++++---------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/meilisearch/rails/multi_search/result.rb b/lib/meilisearch/rails/multi_search/result.rb index 20118f7e..72d9fb44 100644 --- a/lib/meilisearch/rails/multi_search/result.rb +++ b/lib/meilisearch/rails/multi_search/result.rb @@ -23,12 +23,12 @@ def initialize(searches, raw_results) include Enumerable - def each_hit + def each_hit(&block) @results.each do |_index_target, results| - results.each { |res| yield res } + results.each(&block) end end - alias_method :each, :each_hit + alias each each_hit def each_result @results.each @@ -37,20 +37,18 @@ def each_result def to_a @results.values.flatten(1) end - alias_method :to_ary, :to_a + alias to_ary to_a def to_h @results end - alias_method :to_hash, :to_h + alias to_hash to_h private def load_results(klass, result) pk_method = klass.ms_primary_key_method - pk_method = pk_method.in if Utilities.is_mongo_model?(klass) - - ms_pk = klass.meilisearch_options[:primary_key] || IndexSettings::DEFAULT_PRIMARY_KEY + pk_method = pk_method.in if Utilities.mongo_model?(klass) condition_key = pk_is_virtual?(klass, pk_method) ? klass.primary_key : pk_method @@ -78,8 +76,8 @@ def load_results(klass, result) def pk_is_virtual?(model_class, pk_method) model_class.columns - .map(&(Utilities.is_sequel_model?(model_class) ? :to_s : :name)) - .exclude?(pk_method.to_s) + .map(&(Utilities.sequel_model?(model_class) ? :to_s : :name)) + .exclude?(pk_method.to_s) end end end diff --git a/lib/meilisearch/rails/utilities.rb b/lib/meilisearch/rails/utilities.rb index e15bdc91..bd918b7b 100644 --- a/lib/meilisearch/rails/utilities.rb +++ b/lib/meilisearch/rails/utilities.rb @@ -48,11 +48,11 @@ def indexable?(record, options) true end - def is_mongo_model?(model_class) + def mongo_model?(model_class) defined?(::Mongoid::Document) && model_class.include?(::Mongoid::Document) end - def is_sequel_model?(model_class) + def sequel_model?(model_class) defined?(::Sequel::Model) && model_class < Sequel::Model end diff --git a/spec/multi_search/result_spec.rb b/spec/multi_search/result_spec.rb index 74372cf3..ff63dc11 100644 --- a/spec/multi_search/result_spec.rb +++ b/spec/multi_search/result_spec.rb @@ -1,30 +1,27 @@ require 'spec_helper' -describe MeiliSearch::Rails::MultiSearchResult do - it 'is enumerable' do - expect(described_class).to include(Enumerable) - end - +describe MeiliSearch::Rails::MultiSearchResult do # rubocop:todo RSpec/FilePath let(:raw_results) do [ { 'indexUid' => 'books_index', 'hits' => [{ 'name' => 'Steve Jobs', 'id' => '3', 'author' => 'Walter Isaacson', 'premium' => nil, 'released' => nil, 'genre' => nil }], - 'query' => 'Steve', 'processingTimeMs' => 0, 'limit' => 20, 'offset' => 0, 'estimatedTotalHits' => 1 - }, + 'query' => 'Steve', 'processingTimeMs' => 0, 'limit' => 20, 'offset' => 0, 'estimatedTotalHits' => 1 }, { 'indexUid' => 'products_index', 'hits' => [{ 'id' => '4', 'href' => 'ebay', 'name' => 'palm pixi plus' }], - 'query' => 'palm', 'processingTimeMs' => 0, 'limit' => 1, 'offset' => 0, 'estimatedTotalHits' => 2 - }, + 'query' => 'palm', 'processingTimeMs' => 0, 'limit' => 1, 'offset' => 0, 'estimatedTotalHits' => 2 }, { 'indexUid' => 'color_index', 'hits' => [ { 'name' => 'black', 'id' => '5', 'short_name' => 'bla', 'hex' => 0 }, { 'name' => 'blue', 'id' => '4', 'short_name' => 'blu', 'hex' => 255 } ], - 'query' => 'bl', 'processingTimeMs' => 0, 'limit' => 20, 'offset' => 0, 'estimatedTotalHits' => 2 - } + 'query' => 'bl', 'processingTimeMs' => 0, 'limit' => 20, 'offset' => 0, 'estimatedTotalHits' => 2 } ] end + it 'is enumerable' do + expect(described_class).to include(Enumerable) + end + context 'with index name keys' do subject(:result) { described_class.new(searches, raw_results) } @@ -48,13 +45,16 @@ it 'enumerates through the hits of each result with #each_result' do expect(result.each_result).to be_an(Enumerator) expect(result.each_result).to contain_exactly( - [ 'books_index', contain_exactly( - a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs')) ], - [ 'products_index', contain_exactly( - a_hash_including('name' => 'palm pixi plus')) ], - [ 'color_index', contain_exactly( - a_hash_including('name' => 'blue', 'short_name' => 'blu'), - a_hash_including('name' => 'black', 'short_name' => 'bla')) ] + ['books_index', contain_exactly( + a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs') + )], + ['products_index', contain_exactly( + a_hash_including('name' => 'palm pixi plus') + )], + ['color_index', contain_exactly( + a_hash_including('name' => 'blue', 'short_name' => 'blu'), + a_hash_including('name' => 'black', 'short_name' => 'bla') + )] ) end @@ -69,7 +69,7 @@ end it 'aliases as #to_ary' do - expect(subject.method(:to_ary).original_name).to eq :to_a + expect(result.method(:to_ary).original_name).to eq :to_a end end