mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Make 'map' filter work on Enumerable drops
This commit is contained in:
@@ -99,7 +99,14 @@ module Liquid
|
|||||||
|
|
||||||
# map/collect on a given property
|
# map/collect on a given property
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
ary = [input].flatten
|
ary = if input.is_a?(Array)
|
||||||
|
input.flatten
|
||||||
|
elsif !input.class.include?(Enumerable)
|
||||||
|
[input].flatten
|
||||||
|
else
|
||||||
|
input
|
||||||
|
end
|
||||||
|
|
||||||
ary.map do |e|
|
ary.map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
e = e.to_liquid if e.respond_to?(:to_liquid)
|
e = e.to_liquid if e.respond_to?(:to_liquid)
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ class TestDrop < Liquid::Drop
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class TestEnumerable < Liquid::Drop
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
|
def each(&block)
|
||||||
|
[ { "foo" => 1 }, { "foo" => 2 }, { "foo" => 3 } ].each(&block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class StandardFiltersTest < Test::Unit::TestCase
|
class StandardFiltersTest < Test::Unit::TestCase
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
@@ -135,6 +143,10 @@ class StandardFiltersTest < Test::Unit::TestCase
|
|||||||
assert_equal "testfoo", Liquid::Template.parse(templ).render("procs" => [p])
|
assert_equal "testfoo", Liquid::Template.parse(templ).render("procs" => [p])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_map_works_on_enumerables
|
||||||
|
assert_equal "123", Liquid::Template.parse('{{ foo | map: "foo" }}').render!("foo" => TestEnumerable.new)
|
||||||
|
end
|
||||||
|
|
||||||
def test_date
|
def test_date
|
||||||
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
|
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
|
||||||
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
|
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")
|
||||||
|
|||||||
Reference in New Issue
Block a user