From 75da855702d3a4cab120d05b9976e98396fdd5d4 Mon Sep 17 00:00:00 2001 From: Diego Carrion Date: Fri, 23 Oct 2015 16:25:40 -0200 Subject: [PATCH] pg_search_scope accepts a second argument to override options --- README.md | 17 +++++++++++++++++ lib/pg_search.rb | 2 +- spec/integration/pg_search_spec.rb | 9 +++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a5225bc3..279764f3 100644 --- a/README.md +++ b/README.md @@ -579,6 +579,23 @@ robin = Superhero.create :name => 'Robin' Superhero.whose_name_starts_with("Bat") # => [batman, batgirl] ``` + +Note that you can override the prefix option on an especific query like this: + +```ruby +class Superhero < ActiveRecord::Base + include PgSearch + pg_search_scope :search, + :against => :name, + :using => { + :tsearch => {:prefix => true} + } +end + +Superhero.create :name => 'Batman' +Superhero.search("Bat", using: { :tsearch => { :prefix => false }}) # => [] +``` + ##### :negation PostgreSQL's full text search matches all search terms by default. If you want diff --git a/lib/pg_search.rb b/lib/pg_search.rb index 2366fd09..75dacfda 100644 --- a/lib/pg_search.rb +++ b/lib/pg_search.rb @@ -30,7 +30,7 @@ def pg_search_scope(name, options) unless options.respond_to?(:merge) raise ArgumentError, "pg_search_scope expects a Hash or Proc" end - ->(query) { {:query => query}.merge(options) } + ->(query, override_options = {}) { {:query => query}.merge(options).deep_merge(override_options) } end define_singleton_method(name) do |*args| diff --git a/spec/integration/pg_search_spec.rb b/spec/integration/pg_search_spec.rb index 82ef556f..0b5f8541 100644 --- a/spec/integration/pg_search_spec.rb +++ b/spec/integration/pg_search_spec.rb @@ -596,6 +596,15 @@ expect(results).to include(included) expect(results).not_to include(excluded) end + + context "with an { using: { tsearch: { prefix: false }}} options" do + it "returns rows that match the query but not rows that are prefixed by the query" do + excluded = ModelWithPgSearch.create!(:title => 'prefix') + + results = ModelWithPgSearch.search_title_with_prefixes("pre", using: { tsearch: { prefix: false }}) + expect(results).not_to include(excluded) + end + end end end