From 31ad3adf831c1971214c732665b58f1c716f226f Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Mon, 18 Dec 2017 09:47:00 -0500 Subject: [PATCH 1/3] Rails 5.2.0.beta2 compatibility Remove deprecation warnings by explicitly passing `Arel.sql` whenever necessary. It looks like https://github.com/rails/rails/commit/510428ff64ce19340913145140986119c64c8b7d now caches associations on the reflection a little more aggressively, so the tests were breaking because previously stubbed constants were stored in the statement cache and then it attempted to compare against newly stubbed constants and determined they were not equal. This just clears the cache such that they can be compared on a test-by-test basis. --- lib/pg_search/scope_options.rb | 2 +- spec/integration/associations_spec.rb | 2 +- spec/lib/pg_search_spec.rb | 22 ++++++++++++++++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/pg_search/scope_options.rb b/lib/pg_search/scope_options.rb index b8825fa0..6da54855 100644 --- a/lib/pg_search/scope_options.rb +++ b/lib/pg_search/scope_options.rb @@ -16,7 +16,7 @@ def apply(scope) scope .joins(rank_join(rank_table_alias)) - .order("#{rank_table_alias}.rank DESC, #{order_within_rank}") + .order(Arel.sql("#{rank_table_alias}.rank DESC, #{order_within_rank}")) .extend(DisableEagerLoading) .extend(WithPgSearchRank) .extend(WithPgSearchHighlight[feature_for(:tsearch)]) diff --git a/spec/integration/associations_spec.rb b/spec/integration/associations_spec.rb index 098439d1..68a6a591 100644 --- a/spec/integration/associations_spec.rb +++ b/spec/integration/associations_spec.rb @@ -131,7 +131,7 @@ results = ModelWithHasMany .limit(1) - .order("#{ModelWithHasMany.quoted_table_name}.id ASC") + .order(Arel.sql("#{ModelWithHasMany.quoted_table_name}.id ASC")) .with_associated('foo bar') expect(results.map(&:title)).to match_array(included.map(&:title)) diff --git a/spec/lib/pg_search_spec.rb b/spec/lib/pg_search_spec.rb index 2b0705c9..acbe06eb 100644 --- a/spec/lib/pg_search_spec.rb +++ b/spec/lib/pg_search_spec.rb @@ -1,6 +1,16 @@ require "spec_helper" +module ClearSearchableCache + refine PgSearch::Document.singleton_class do + def clear_searchable_cache + reflect_on_association(:searchable).clear_association_scope_cache + end + end +end + describe PgSearch do + using ClearSearchableCache + describe ".multisearch" do with_table "pg_search_documents", {}, &DOCUMENTS_SCHEMA @@ -18,7 +28,10 @@ context "with PgSearch.multisearch_options set to a Hash" do before { allow(PgSearch).to receive(:multisearch_options).and_return(:using => :dmetaphone) } - subject { PgSearch.multisearch(query).map(&:searchable) } + subject do + PgSearch::Document.clear_searchable_cache + PgSearch.multisearch(query).map(&:searchable) + end with_model :MultisearchableModel do table do |t| @@ -36,7 +49,10 @@ end context "with PgSearch.multisearch_options set to a Proc" do - subject { PgSearch.multisearch(query, soundalike).map(&:searchable) } + subject do + PgSearch::Document.clear_searchable_cache + PgSearch.multisearch(query, soundalike).map(&:searchable) + end before do allow(PgSearch).to receive(:multisearch_options) do @@ -140,6 +156,7 @@ PgSearch::Multisearch.rebuild(SearchableSubclassModel) + PgSearch::Document.clear_searchable_cache expect(PgSearch::Document.count).to be 1 expect(PgSearch::Document.first.searchable.class).to be SearchableSubclassModel expect(PgSearch::Document.first.searchable).to eq expected @@ -153,6 +170,7 @@ PgSearch::Multisearch.rebuild(SearchableSubclassModel) expect(PgSearch::Document.count).to be 2 + PgSearch::Document.clear_searchable_cache classes = PgSearch::Document.all.collect {|d| d.searchable.class } expect(classes).to include SearchableSubclassModel expect(classes).to include AnotherSearchableSubclassModel From de22bdbb7557595f1c314d070913acdf4599cc46 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Tue, 19 Dec 2017 09:38:57 -0500 Subject: [PATCH 2/3] Only clear cache on >= Rails 5 --- spec/lib/pg_search_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/spec/lib/pg_search_spec.rb b/spec/lib/pg_search_spec.rb index acbe06eb..0ae636f8 100644 --- a/spec/lib/pg_search_spec.rb +++ b/spec/lib/pg_search_spec.rb @@ -1,16 +1,18 @@ require "spec_helper" -module ClearSearchableCache - refine PgSearch::Document.singleton_class do +# For AR 5 and greater, the association reflection's cache needs be cleared +# because we're stubbing the related constants. +class << PgSearch::Document + if ActiveRecord::VERSION::MAJOR >= 5 def clear_searchable_cache reflect_on_association(:searchable).clear_association_scope_cache end + else + def clear_searchable_cache; end end end describe PgSearch do - using ClearSearchableCache - describe ".multisearch" do with_table "pg_search_documents", {}, &DOCUMENTS_SCHEMA From 0a270a773f0fddcd38b92fa61b23df9f2618e45d Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 21 Dec 2017 10:52:09 -0500 Subject: [PATCH 3/3] Fix style violations --- .rubocop.yml | 5 +++++ lib/pg_search.rb | 24 ++++++++++++------------ lib/pg_search/configuration.rb | 4 +--- lib/pg_search/multisearch/rebuilder.rb | 8 ++------ spec/lib/pg_search_spec.rb | 5 +++-- spec/support/database.rb | 4 ++-- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index a4c40466..70a45721 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -45,3 +45,8 @@ Bundler/DuplicatedGem: Style/EmptyMethod: EnforcedStyle: expanded + +# Waiting on false positives to go away with: +# https://github.com/bbatsov/rubocop/pull/5230 +Style/FormatStringToken: + Enabled: false diff --git a/lib/pg_search.rb b/lib/pg_search.rb index 4d2b5132..802408c9 100644 --- a/lib/pg_search.rb +++ b/lib/pg_search.rb @@ -22,14 +22,13 @@ module PgSearch module ClassMethods def pg_search_scope(name, options) - options_proc = if options.respond_to?(:call) - options - else - unless options.respond_to?(:merge) - raise ArgumentError, "pg_search_scope expects a Hash or Proc" - end - ->(query) { {:query => query}.merge(options) } - end + options_proc = + if options.respond_to?(:call) + options + else + raise ArgumentError, "pg_search_scope expects a Hash or Proc" unless options.respond_to?(:merge) + ->(query) { {:query => query}.merge(options) } + end define_singleton_method(name) do |*args| config = Configuration.new(options_proc.call(*args), self) @@ -39,7 +38,7 @@ def pg_search_scope(name, options) end def multisearchable(options = {}) - include PgSearch::Multisearchable + include PgSearch::Multisearchable # rubocop:disable Style/MixinUsage class_attribute :pg_search_multisearchable_options self.pg_search_multisearchable_options = options end @@ -92,14 +91,15 @@ def respond_to_missing?(symbol, *args) class PgSearchRankNotSelected < StandardError def message - "You must chain .with_pg_search_rank after the pg_search_scope to access the pg_search_rank attribute on returned records" # rubocop:disable Metrics/LineLength + "You must chain .with_pg_search_rank after the pg_search_scope to access " \ + "the pg_search_rank attribute on returned records" end end class PgSearchHighlightNotSelected < StandardError - # rubocop:disable Metrics/LineLength def message - "You must chain .with_pg_search_highlight after the pg_search_scope to access the pg_search_highlight attribute on returned records" + "You must chain .with_pg_search_highlight after the pg_search_scope to access " \ + "the pg_search_highlight attribute on returned records" end end end diff --git a/lib/pg_search/configuration.rb b/lib/pg_search/configuration.rb index 6dee94f1..2286a9f9 100644 --- a/lib/pg_search/configuration.rb +++ b/lib/pg_search/configuration.rb @@ -96,9 +96,7 @@ def assert_valid_options(options) VALID_VALUES.each do |key, values_for_key| Array(options[key]).each do |value| - unless values_for_key.include?(value) - raise ArgumentError, ":#{key} cannot accept #{value}" - end + raise ArgumentError, ":#{key} cannot accept #{value}" unless values_for_key.include?(value) end end end diff --git a/lib/pg_search/multisearch/rebuilder.rb b/lib/pg_search/multisearch/rebuilder.rb index 33d51bd2..3f891736 100644 --- a/lib/pg_search/multisearch/rebuilder.rb +++ b/lib/pg_search/multisearch/rebuilder.rb @@ -2,9 +2,7 @@ module PgSearch module Multisearch class Rebuilder def initialize(model, time_source = Time.method(:now)) - unless model.respond_to?(:pg_search_multisearchable_options) - raise ModelNotMultisearchable.new(model) - end + raise ModelNotMultisearchable.new(model) unless model.respond_to?(:pg_search_multisearchable_options) @model = model @time_source = time_source @@ -65,9 +63,7 @@ def sti_clause clause = "" if model.column_names.include? model.inheritance_column clause = "WHERE" - if model.base_class == model - clause = "#{clause} #{model.inheritance_column} IS NULL OR" - end + clause = "#{clause} #{model.inheritance_column} IS NULL OR" if model.base_class == model clause = "#{clause} #{model.inheritance_column} = #{model_name}" end clause diff --git a/spec/lib/pg_search_spec.rb b/spec/lib/pg_search_spec.rb index 0ae636f8..cceec94c 100644 --- a/spec/lib/pg_search_spec.rb +++ b/spec/lib/pg_search_spec.rb @@ -8,7 +8,8 @@ def clear_searchable_cache reflect_on_association(:searchable).clear_association_scope_cache end else - def clear_searchable_cache; end + def clear_searchable_cache + end end end @@ -243,7 +244,7 @@ def clear_searchable_cache; end @multisearch_enabled_inside = PgSearch.multisearch_enabled? raise end - rescue # rubocop:disable Lint/RescueWithoutErrorClass + rescue # rubocop:disable Style/RescueStandardError end @multisearch_enabled_after = PgSearch.multisearch_enabled? diff --git a/spec/support/database.rb b/spec/support/database.rb index a3f92425..34f2d830 100644 --- a/spec/support/database.rb +++ b/spec/support/database.rb @@ -42,7 +42,7 @@ def install_extension(name) extension = connection.execute "SELECT * FROM pg_catalog.pg_extension WHERE extname = '#{name}';" return unless extension.none? connection.execute "CREATE EXTENSION #{name};" -rescue => exception # rubocop:disable Lint/RescueWithoutErrorClass +rescue => exception # rubocop:disable Style/RescueStandardError at_exit do puts "-" * 80 puts "Please install the #{name} extension" @@ -54,7 +54,7 @@ def install_extension(name) def install_extension_if_missing(name, query, expected_result) result = ActiveRecord::Base.connection.select_value(query) raise "Unexpected output for #{query}: #{result.inspect}" unless result.downcase == expected_result.downcase -rescue # rubocop:disable Lint/RescueWithoutErrorClass +rescue # rubocop:disable Style/RescueStandardError install_extension(name) end