Skip to content

Commit d44c4cc

Browse files
committed
Restored backwards compatibility for aggs method - #1739
1 parent 7be8d5b commit d44c4cc

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/searchkick/relation.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ def aggs(*args, **kwargs)
3838
def aggs!(*args, **kwargs)
3939
check_loaded
4040
@options[:aggs] ||= {}
41-
@options[:aggs].merge!(args.to_h { |arg| [arg, {}] })
41+
args.flatten.each do |arg|
42+
if arg.is_a?(Hash)
43+
@options[:aggs].merge!(arg)
44+
else
45+
@options[:aggs][arg] = {}
46+
end
47+
end
4248
@options[:aggs].merge!(kwargs)
4349
self
4450
end

test/aggs_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,15 @@ def test_body_options
156156
end
157157

158158
def test_relation
159+
assert_aggs ({"store_id" => {1 => 1, 2 => 2}}), Product.search("Product").aggs(:store_id)
159160
assert_aggs ({"store_id" => {1 => 1}}), Product.search("Product").aggs(store_id: {where: {in_stock: true}})
161+
assert_aggs ({"store_id" => {1 => 1}}), Product.search("Product").aggs({store_id: {where: {in_stock: true}}})
162+
end
163+
164+
def test_relation_multiple
165+
expected = {"store_id" => {1 => 1, 2 => 2}, "color" => {"blue" => 1, "green" => 1, "red" => 1}}
166+
assert_aggs expected, Product.search("Product").aggs(:store_id, :color)
167+
assert_aggs expected, Product.search("Product").aggs([:store_id, :color])
160168
end
161169

162170
def test_relation_smart_aggs_false

0 commit comments

Comments
 (0)