Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pg_search_scope accepts a second argument to override options #270

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/pg_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
9 changes: 9 additions & 0 deletions spec/integration/pg_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down