mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Merge pull request #1726 from Shopify/klingbaum/ensure_sum_filter_calls_#to_liquid
Ensure `sum` filter calls `to_liquid` on evaluated property value
This commit is contained in:
@@ -878,19 +878,22 @@ module Liquid
|
|||||||
# @liquid_return [number]
|
# @liquid_return [number]
|
||||||
def sum(input, property = nil)
|
def sum(input, property = nil)
|
||||||
ary = InputIterator.new(input, context)
|
ary = InputIterator.new(input, context)
|
||||||
|
return 0 if ary.empty?
|
||||||
|
|
||||||
if ary.empty?
|
values_for_sum = ary.map do |item|
|
||||||
0
|
if property.nil?
|
||||||
elsif property.nil?
|
item
|
||||||
ary.sum do |item|
|
elsif item.respond_to?(:[])
|
||||||
Utils.to_number(item)
|
item[property]
|
||||||
end
|
else
|
||||||
else
|
0
|
||||||
ary.sum do |item|
|
|
||||||
item.respond_to?(:[]) ? Utils.to_number(item[property]) : 0
|
|
||||||
rescue TypeError
|
|
||||||
raise_property_error(property)
|
|
||||||
end
|
end
|
||||||
|
rescue TypeError
|
||||||
|
raise_property_error(property)
|
||||||
|
end
|
||||||
|
|
||||||
|
InputIterator.new(values_for_sum, context).sum do |item|
|
||||||
|
Utils.to_number(item)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -982,6 +982,18 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
def with_timezone(tz)
|
def with_timezone(tz)
|
||||||
|
|||||||
Reference in New Issue
Block a user