mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Ensure sum filter calls to_liquid
This commit is contained in:
@@ -878,19 +878,22 @@ module Liquid
|
||||
# @liquid_return [number]
|
||||
def sum(input, property = nil)
|
||||
ary = InputIterator.new(input, context)
|
||||
return 0 if ary.empty?
|
||||
|
||||
if ary.empty?
|
||||
0
|
||||
elsif property.nil?
|
||||
ary.sum do |item|
|
||||
Utils.to_number(item)
|
||||
end
|
||||
else
|
||||
ary.sum do |item|
|
||||
item.respond_to?(:[]) ? Utils.to_number(item[property]) : 0
|
||||
rescue TypeError
|
||||
raise_property_error(property)
|
||||
values_for_sum = ary.map do |item|
|
||||
if property.nil?
|
||||
item
|
||||
elsif item.respond_to?(:[])
|
||||
item[property]
|
||||
else
|
||||
0
|
||||
end
|
||||
rescue TypeError
|
||||
raise_property_error(property)
|
||||
end
|
||||
|
||||
InputIterator.new(values_for_sum, context).sum do |item|
|
||||
Utils.to_number(item)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -982,6 +982,18 @@ class StandardFiltersTest < Minitest::Test
|
||||
end
|
||||
end
|
||||
|
||||
def test_sum_without_property_calls_to_liquid
|
||||
t = TestThing.new
|
||||
Liquid::Template.parse('{{ foo | sum }}').render("foo" => [t])
|
||||
assert(t.foo > 0)
|
||||
end
|
||||
|
||||
def test_sum_with_property_calls_to_liquid_on_property_values
|
||||
t = TestThing.new
|
||||
Liquid::Template.parse('{{ foo | sum: "quantity" }}').render("foo" => [{ "quantity" => t }])
|
||||
assert(t.foo > 0)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def with_timezone(tz)
|
||||
|
||||
Reference in New Issue
Block a user