From 21eccec4df0a508c8610960c502ce815e61d7fa4 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 18 Mar 2020 03:41:49 -0700 Subject: [PATCH] Pluck empty [skip ci] --- lib/searchkick/relation.rb | 9 ++++++++- test/relation_test.rb | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/searchkick/relation.rb b/lib/searchkick/relation.rb index dcd6eee..41e1702 100644 --- a/lib/searchkick/relation.rb +++ b/lib/searchkick/relation.rb @@ -116,11 +116,18 @@ module Searchkick # TODO make more efficient if loaded def pluck(*fields) - result = select(*fields).load(false) + result = load(false) + if fields.empty? + result.select!(true) + else + result.select!(*fields) + end + if fields.size == 1 field = fields.first result.map { |v| v[field] } else + fields = result.first.keys if fields.empty? && result.any? result.map { |v| fields.map { |f| v[f] } } end end diff --git a/test/relation_test.rb b/test/relation_test.rb index bf4b698..847461b 100644 --- a/test/relation_test.rb +++ b/test/relation_test.rb @@ -45,6 +45,13 @@ class RelationTest < Minitest::Test assert_equal [["Blue", nil], ["Red", nil]], Product.search.order(:name).pluck(:name, :store_id) end + def test_pluck_empty + Product.search_index.refresh + assert_equal [], Product.search.pluck + store_names ["Blue", "Red"] + assert Product.search.pluck + end + def test_parameters skip unless defined?(ActiveRecord) require "action_controller" -- libgit2 0.21.0