lib/pg_search/features/tsearch.rb in pg_search-2.3.6 vs lib/pg_search/features/tsearch.rb in pg_search-2.3.7

- old
+ new

@@ -1,13 +1,13 @@ # frozen_string_literal: true require "active_support/core_ext/module/delegation" -require 'active_support/deprecation' +require "active_support/deprecation" module PgSearch module Features - class TSearch < Feature # rubocop:disable Metrics/ClassLength + class TSearch < Feature def self.valid_options super + %i[dictionary prefix negation any_word normalization tsvector_column highlight] end def conditions @@ -34,16 +34,15 @@ Arel::Nodes.build_quoted(ts_headline_options) ]).to_sql end def ts_headline_options - return '' unless options[:highlight].is_a?(Hash) + return "" unless options[:highlight].is_a?(Hash) headline_options .merge(deprecated_headline_options) - .map { |key, value| "#{key} = #{value}" unless value.nil? } - .compact + .filter_map { |key, value| "#{key} = #{value}" unless value.nil? } .join(", ") end def headline_options indifferent_options = options.with_indifferent_access @@ -57,11 +56,11 @@ hash[key] = ts_headline_option_value(value) end end end - def deprecated_headline_options # rubocop:disable Metrics/MethodLength + def deprecated_headline_options indifferent_options = options.with_indifferent_access %w[ start_sel stop_sel max_fragments max_words min_words short_word fragment_delimiter highlight_all ].reduce({}) do |hash, deprecated_key| @@ -69,13 +68,15 @@ value = indifferent_options[:highlight][deprecated_key] unless value.nil? key = deprecated_key.camelize - ActiveSupport::Deprecation.warn( + warn( "pg_search 3.0 will no longer accept :#{deprecated_key} as an argument to :ts_headline, " \ - "use :#{key} instead." + "use :#{key} instead.", + category: :deprecated, + uplevel: 1 ) hash[key] = ts_headline_option_value(value) end end @@ -93,15 +94,15 @@ else value end end - DISALLOWED_TSQUERY_CHARACTERS = /['?\\:‘’]/.freeze + DISALLOWED_TSQUERY_CHARACTERS = /['?\\:‘’ʻʼ]/ def tsquery_for_term(unsanitized_term) if options[:negation] && unsanitized_term.start_with?("!") - unsanitized_term[0] = '' + unsanitized_term[0] = "" negated = true end sanitized_term = unsanitized_term.gsub(DISALLOWED_TSQUERY_CHARACTERS, " ") @@ -115,11 +116,11 @@ # After this, the SQL expression evaluates to a string containing the term surrounded by single-quotes. # If :prefix is true, then the term will have :* appended to the end. # If :negated is true, then the term will have ! prepended to the front. def tsquery_expression(term_sql, negated:, prefix:) terms = [ - (Arel::Nodes.build_quoted('!') if negated), + (Arel::Nodes.build_quoted("!") if negated), Arel::Nodes.build_quoted("' "), term_sql, Arel::Nodes.build_quoted(" '"), (Arel::Nodes.build_quoted(":*") if prefix) ].compact @@ -132,11 +133,11 @@ def tsquery return "''" if query.blank? query_terms = query.split.compact tsquery_terms = query_terms.map { |term| tsquery_for_term(term) } - tsquery_terms.join(options[:any_word] ? ' || ' : ' && ') + tsquery_terms.join(options[:any_word] ? " || " : " && ") end def tsdocument tsdocument_terms = (columns_to_use || []).map do |search_column| column_to_tsvector(search_column) @@ -150,10 +151,10 @@ "#{quoted_table_name}.#{column_name}" end end - tsdocument_terms.join(' || ') + tsdocument_terms.join(" || ") end # From http://www.postgresql.org/docs/8.3/static/textsearch-controls.html # 0 (the default) ignores the document length # 1 divides the rank by 1 + the logarithm of the document length