From 20f164ddaf7a9e4a0971bcbbf8d0354bb1830126 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 18 Mar 2020 02:47:19 -0700 Subject: [PATCH] Added one? and many? --- lib/searchkick/relation.rb | 3 ++- lib/searchkick/results.rb | 8 ++++++++ test/results_test.rb | 15 +++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 test/results_test.rb diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb index 4be8696..ce6388e 100644 --- a/lib/searchkick/relation.rb +++ b/lib/searchkick/relation.rb @@ -9,7 +9,8 @@ module Searchkick :took, :error, :model_name, :entry_name, :total_count, :total_entries, :current_page, :per_page, :limit_value, :total_pages, :num_pages, :offset_value, :previous_page, :prev_page, :next_page, :first_page?, :last_page?, - :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights + :out_of_range?, :hits, :response, :to_a, :first, :highlights, :group_by, :misspellings?, :with_highlights, + :one?, :many? def_delegators :query, :params diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 7c5a81e..e9aa933 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -197,6 +197,14 @@ module Searchkick current_page > total_pages end + def one? + size == 1 + end + + def many? + size > 1 + end + def hits if error raise Searchkick::Error, "Query error - use the error method to view it" diff --git a/test/results_test.rb b/test/results_test.rb new file mode 100644 index 0000000..a30a87f --- /dev/null +++ b/test/results_test.rb @@ -0,0 +1,15 @@ +require_relative "test_helper" + +class ResultsTest < Minitest::Test + def test_one + store_names ["Red", "Blue"] + assert !Product.search("*").one? + assert Product.search("blue").one? + end + + def test_many + store_names ["Red", "Blue"] + assert Product.search("*").many? + assert !Product.search("blue").many? + end +end -- libgit2 0.21.0