From 3344b243a844a95fb4316074ca7cf89d238d21f4 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Sat, 28 Feb 2015 14:54:51 -0800 Subject: [PATCH] Correct use of && and || --- lib/searchkick/index.rb | 6 +++--- lib/searchkick/query.rb | 20 ++++++++++---------- lib/searchkick/reindex_job.rb | 2 +- lib/searchkick/reindex_v2_job.rb | 2 +- lib/searchkick/results.rb | 8 ++++---- test/sql_test.rb | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index bb31f08..a3048c0 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -76,7 +76,7 @@ module Searchkick end def reindex_record(record) - if record.destroyed? or !record.should_index? + if record.destroyed? || !record.should_index? begin remove(record) rescue Elasticsearch::Transport::Transport::Errors::NotFound @@ -200,7 +200,7 @@ module Searchkick def index_options options = @options - if options[:mappings] and !options[:merge_mappings] + if options[:mappings] && !options[:merge_mappings] settings = options[:settings] || {} mappings = options[:mappings] else @@ -492,7 +492,7 @@ module Searchkick # conversions conversions_field = options[:conversions] - if conversions_field and source[conversions_field] + if conversions_field && source[conversions_field] source[conversions_field] = source[conversions_field].map{|k, v| {query: k, count: v} } end diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 2ee0923..edde659 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -98,7 +98,7 @@ module Searchkick boost: factor } - if field == "_all" or field.end_with?(".analyzed") + if field == "_all" || field.end_with?(".analyzed") shared_options[:cutoff_frequency] = 0.001 unless operator == "and" qs.concat [ shared_options.merge(boost: 10 * factor, analyzer: "searchkick_search"), @@ -130,7 +130,7 @@ module Searchkick } end - if conversions_field and options[:conversions] != false + if conversions_field && options[:conversions] != false # wrap payload in a bool query script_score = if below12 @@ -191,14 +191,14 @@ module Searchkick end boost_where = options[:boost_where] || {} - if options[:user_id] and personalize_field + if options[:user_id] && personalize_field boost_where[personalize_field] = options[:user_id] end if options[:personalize] boost_where = boost_where.merge(options[:personalize]) end boost_where.each do |field, value| - if value.is_a?(Array) and value.first.is_a?(Hash) + if value.is_a?(Array) && value.first.is_a?(Hash) value.each do |value_factor| value, factor = value_factor[:value], value_factor[:factor] custom_filters << custom_filter(field, value, factor) @@ -215,7 +215,7 @@ module Searchkick boost_by_distance = options[:boost_by_distance] if boost_by_distance boost_by_distance = {function: :gauss, scale: "5mi"}.merge(boost_by_distance) - if !boost_by_distance[:field] or !boost_by_distance[:origin] + if !boost_by_distance[:field] || !boost_by_distance[:origin] raise ArgumentError, "boost_by_distance requires :field and :origin" end function_params = boost_by_distance.select{|k,v| [:origin, :scale, :offset, :decay].include?(k) } @@ -364,7 +364,7 @@ module Searchkick payload[:fields] = [] end - if options[:type] or klass != searchkick_klass + if options[:type] || klass != searchkick_klass @type = [options[:type] || klass].flatten.map{|v| searchkick_index.klass_document_type(v) } end end @@ -405,10 +405,10 @@ module Searchkick status_code = e.message[1..3].to_i if status_code == 404 raise MissingIndexError, "Index missing - run #{searchkick_klass.name}.reindex" - elsif status_code == 500 and ( - e.message.include?("IllegalArgumentException[minimumSimilarity >= 1]") or - e.message.include?("No query registered for [multi_match]") or - e.message.include?("[match] query does not support [cutoff_frequency]]") or + elsif status_code == 500 && ( + e.message.include?("IllegalArgumentException[minimumSimilarity >= 1]") || + e.message.include?("No query registered for [multi_match]") || + e.message.include?("[match] query does not support [cutoff_frequency]]") || e.message.include?("No query registered for [function_score]]") ) diff --git a/lib/searchkick/reindex_job.rb b/lib/searchkick/reindex_job.rb index 62ce9b1..ffa43ce 100644 --- a/lib/searchkick/reindex_job.rb +++ b/lib/searchkick/reindex_job.rb @@ -10,7 +10,7 @@ module Searchkick model = @klass.constantize record = model.find(@id) rescue nil # TODO fix lazy coding index = model.searchkick_index - if !record or !record.should_index? + if !record || !record.should_index? # hacky record ||= model.new record.id = @id diff --git a/lib/searchkick/reindex_v2_job.rb b/lib/searchkick/reindex_v2_job.rb index 715b74f..689eb10 100644 --- a/lib/searchkick/reindex_v2_job.rb +++ b/lib/searchkick/reindex_v2_job.rb @@ -6,7 +6,7 @@ module Searchkick model = klass.constantize record = model.find(id) rescue nil # TODO fix lazy coding index = model.searchkick_index - if !record or !record.should_index? + if !record || !record.should_index? # hacky record ||= model.new record.id = id diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 637486c..a436461 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -24,7 +24,7 @@ module Searchkick hits.group_by{|hit, i| hit["_type"] }.each do |type, grouped_hits| records = type.camelize.constantize if options[:includes] - if defined?(NoBrainer::Document) and records < NoBrainer::Document + if defined?(NoBrainer::Document) && records < NoBrainer::Document records = records.preload(options[:includes]) else records = records.includes(options[:includes]) @@ -138,16 +138,16 @@ module Searchkick private def results_query(records, grouped_hits) - if records.respond_to?(:primary_key) and records.primary_key + if records.respond_to?(:primary_key) && records.primary_key # ActiveRecord records.where(records.primary_key => grouped_hits.map{|hit| hit["_id"] }).to_a - elsif records.respond_to?(:all) and records.all.respond_to?(:for_ids) + elsif records.respond_to?(:all) && records.all.respond_to?(:for_ids) # Mongoid 2 records.all.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a elsif records.respond_to?(:queryable) # Mongoid 3+ records.queryable.for_ids(grouped_hits.map{|hit| hit["_id"] }).to_a - elsif records.respond_to?(:unscoped) and records.all.respond_to?(:preload) + elsif records.respond_to?(:unscoped) && records.all.respond_to?(:preload) # Nobrainer records.unscoped.where(:id.in => grouped_hits.map{|hit| hit["_id"] }).to_a else diff --git a/test/sql_test.rb b/test/sql_test.rb index 1c43996..97b5484 100644 --- a/test/sql_test.rb +++ b/test/sql_test.rb @@ -312,7 +312,7 @@ class TestSql < Minitest::Test end # TODO see if Mongoid is loaded - unless defined?(Mongoid) or defined?(NoBrainer) + unless defined?(Mongoid) || defined?(NoBrainer) def test_include store_names ["Product A"] assert Product.search("product", include: [:store]).first.association(:store).loaded? -- libgit2 0.21.0