Commit 20f164ddaf7a9e4a0971bcbbf8d0354bb1830126
1 parent
e829de29
Exists in
relation
Added one? and many?
Showing
3 changed files
with
25 additions
and
1 deletions
Show diff stats
lib/searchkick/relation.rb
@@ -9,7 +9,8 @@ module Searchkick | @@ -9,7 +9,8 @@ module Searchkick | ||
9 | :took, :error, :model_name, :entry_name, :total_count, :total_entries, | 9 | :took, :error, :model_name, :entry_name, :total_count, :total_entries, |
10 | :current_page, :per_page, :limit_value, :total_pages, :num_pages, | 10 | :current_page, :per_page, :limit_value, :total_pages, :num_pages, |
11 | :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, | 11 | :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, |
12 | - :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights | 12 | + :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights, |
13 | + :one?, :many? | ||
13 | 14 | ||
14 | def_delegators :query, :params | 15 | def_delegators :query, :params |
15 | 16 |
lib/searchkick/results.rb
@@ -197,6 +197,14 @@ module Searchkick | @@ -197,6 +197,14 @@ module Searchkick | ||
197 | current_page > total_pages | 197 | current_page > total_pages |
198 | end | 198 | end |
199 | 199 | ||
200 | + def one? | ||
201 | + size == 1 | ||
202 | + end | ||
203 | + | ||
204 | + def many? | ||
205 | + size > 1 | ||
206 | + end | ||
207 | + | ||
200 | def hits | 208 | def hits |
201 | if error | 209 | if error |
202 | raise Searchkick::Error, "Query error - use the error method to view it" | 210 | raise Searchkick::Error, "Query error - use the error method to view it" |
@@ -0,0 +1,15 @@ | @@ -0,0 +1,15 @@ | ||
1 | +require_relative "test_helper" | ||
2 | + | ||
3 | +class ResultsTest < Minitest::Test | ||
4 | + def test_one | ||
5 | + store_names ["Red", "Blue"] | ||
6 | + assert !Product.search("*").one? | ||
7 | + assert Product.search("blue").one? | ||
8 | + end | ||
9 | + | ||
10 | + def test_many | ||
11 | + store_names ["Red", "Blue"] | ||
12 | + assert Product.search("*").many? | ||
13 | + assert !Product.search("blue").many? | ||
14 | + end | ||
15 | +end |