Skip to content

Commit 7be8d5b

Browse files
Refactor aggs method to accept positional and keyword arguments (#1739)
1 parent 5b49353 commit 7be8d5b

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

lib/searchkick/relation.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,19 @@ def inspect
2727
"#<#{self.class.name} [#{entries.join(', ')}]>"
2828
end
2929

30-
def aggs(value = NO_DEFAULT_VALUE)
31-
if value == NO_DEFAULT_VALUE
30+
def aggs(*args, **kwargs)
31+
if args.empty? && kwargs.empty?
3232
private_execute.aggs
3333
else
34-
clone.aggs!(value)
34+
clone.aggs!(*args, **kwargs)
3535
end
3636
end
3737

38-
def aggs!(value)
38+
def aggs!(*args, **kwargs)
3939
check_loaded
40-
(@options[:aggs] ||= {}).merge!(value)
40+
@options[:aggs] ||= {}
41+
@options[:aggs].merge!(args.to_h { |arg| [arg, {}] })
42+
@options[:aggs].merge!(kwargs)
4143
self
4244
end
4345

test/aggs_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ def test_relation_smart_aggs_false
163163
assert_aggs ({"store_id" => {2 => 2}}), Product.search("Product").where(color: "red").aggs(store_id: {where: {in_stock: false}}).smart_aggs(false)
164164
end
165165

166+
def test_relation_with_positional_args
167+
aggs = Product.search("Product").aggs(:color, store_id: {where: {in_stock: true}}).aggs
168+
assert_equal ({"blue" => 1, "green" => 1, "red" => 1}), buckets_as_hash(aggs["color"])
169+
assert_equal ({1 => 1}), buckets_as_hash(aggs["store_id"])
170+
end
171+
166172
protected
167173

168174
def assert_aggs(expected, options)

0 commit comments

Comments
 (0)