Skip to content

Commit

Permalink
Add search_none/none scopes
Browse files Browse the repository at this point in the history
When called directly on a model, the method is `search_none`:

    Article.search_none

But otherwise, it’s available as just `none`, either on ThinkingSphinx,
or through an existing Thinking Sphinx scope:

    ThinkingSphinx.none
    Article.search(“something”).none
  • Loading branch information
pat committed Jul 7, 2024
1 parent 4d962e5 commit 1e8a666
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/thinking_sphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def self.search_for_ids(query = '', options = {})
ThinkingSphinx::Search::Merger.new(search).merge! nil, :ids_only => true
end

def self.none
ThinkingSphinx::Search.new nil, :none => true
end

def self.before_index_hooks
@before_index_hooks
end
Expand Down
4 changes: 4 additions & 0 deletions lib/thinking_sphinx/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def sphinx_search_for_ids(query = nil, options = {})
).merge! nil, :ids_only => true
end

def sphinx_search_none
merge ThinkingSphinx.search, nil, none: true
end

private

def default_sphinx_scope?
Expand Down
6 changes: 6 additions & 0 deletions lib/thinking_sphinx/masks/scopes_mask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def search_for_ids(query = nil, options = {})
search query, options.merge(:ids_only => true)
end

def none
ThinkingSphinx::Search::Merger.new(@search).merge! nil, :none => true
end

alias_method :search_none, :none

private

def apply_scope(scope, *args)
Expand Down
4 changes: 2 additions & 2 deletions lib/thinking_sphinx/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class ThinkingSphinx::Search < Array
[
:classes, :conditions, :excerpts, :geo, :group_by, :ids_only,
:ignore_scopes, :indices, :limit, :masks, :max_matches, :middleware,
:offset, :order, :order_group_by, :page, :per_page, :populate,
:none, :offset, :order, :order_group_by, :page, :per_page, :populate,
:retry_stale, :select, :skip_sti, :sql, :star, :with, :with_all, :without,
:without_ids
] +
Expand Down Expand Up @@ -92,7 +92,7 @@ def per_page(value = nil)
def populate
return self if @populated

middleware.call [context]
middleware.call [context] unless options[:none]
@populated = true

self
Expand Down
1 change: 1 addition & 0 deletions lib/thinking_sphinx/search/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def initialize(search, configuration = nil)
@search = search
@configuration = configuration || ThinkingSphinx::Configuration.instance
@memory = {
:raw => [],
:results => [],
:panes => ThinkingSphinx::Configuration::Defaults::PANES.clone
}
Expand Down
7 changes: 7 additions & 0 deletions spec/acceptance/searching_across_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@
expect(ThinkingSphinx.search(:classes => [User, Article]).to_a).
to match_array([article, user])
end

it "has a 'none' default scope" do
article = Article.create! :title => 'Pancakes'
index

expect(ThinkingSphinx.none).to be_empty
end
end
7 changes: 7 additions & 0 deletions spec/acceptance/searching_within_a_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@

expect(Article.sphinx_search.first).to eq(article)
end

it "has a 'none' default scope" do
article = Article.create! :title => 'Pancakes'
index

expect(Article.search_none).to be_empty
end
end

describe 'Searching within a model with a realtime index', :live => true do
Expand Down
7 changes: 7 additions & 0 deletions spec/acceptance/sphinx_scopes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@
ThinkingSphinx::PopulatedResultsError
)
end

it "handles a chainable 'none' scope and returns nothing" do
Book.create! :title => 'Small Gods'
index

expect(Book.by_query('gods').none).to be_empty
end
end

0 comments on commit 1e8a666

Please sign in to comment.