From 40335c5f4d886b263c0ac4c7742456e0fac2ea86 Mon Sep 17 00:00:00 2001 From: Travis Hunter Date: Fri, 17 Dec 2021 11:42:08 -0500 Subject: [PATCH] Don't require against if tsvector_column is specified --- lib/pg_search/configuration.rb | 11 +++++++++-- spec/integration/pg_search_spec.rb | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/pg_search/configuration.rb b/lib/pg_search/configuration.rb index 2687b57b..e24e06b9 100644 --- a/lib/pg_search/configuration.rb +++ b/lib/pg_search/configuration.rb @@ -92,8 +92,8 @@ def default_options }.freeze def assert_valid_options(options) - unless options[:against] || options[:associated_against] - raise ArgumentError, "the search scope #{@name} must have :against or :associated_against in its options" + unless options[:against] || options[:associated_against] || using_tsvector_column?(options[:using]) + raise ArgumentError, "the search scope #{@name} must have :against or :associated_against in its options or specify a tsvector_column" end options.assert_valid_keys(VALID_KEYS) @@ -104,5 +104,12 @@ def assert_valid_options(options) end end end + + def using_tsvector_column?(options) + return unless options.is_a?(Hash) + + options.dig(:dmetaphone, :tsvector_column).present? || + options.dig(:tsearch, :tsvector_column).present? + end end end diff --git a/spec/integration/pg_search_spec.rb b/spec/integration/pg_search_spec.rb index 70cd84e1..6ab50bac 100644 --- a/spec/integration/pg_search_spec.rb +++ b/spec/integration/pg_search_spec.rb @@ -140,6 +140,22 @@ }.to raise_error(ArgumentError, /against/) end end + + context "when a tsvector column is specified" do + it "does not raise an exception when invoked" do + ModelWithPgSearch.pg_search_scope :with_unknown_ignoring, { + using: { + tsearch: { + tsvector_column: "tsv" + } + } + } + + expect { + ModelWithPgSearch.with_unknown_ignoring("foo") + }.not_to raise_error + end + end end end end