Make 'map' filter work on Enumerable drops

This commit is contained in:
Florian Weingarten
2013-07-25 17:21:50 -04:00
parent 554675d1f8
commit 65dfd57bb5
2 changed files with 20 additions and 1 deletions

View File

@@ -99,7 +99,14 @@ module Liquid
# map/collect on a given 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|
e = e.call if e.is_a?(Proc)
e = e.to_liquid if e.respond_to?(:to_liquid)

View File

@@ -27,6 +27,14 @@ class TestDrop < Liquid::Drop
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
include Liquid
@@ -135,6 +143,10 @@ class StandardFiltersTest < Test::Unit::TestCase
assert_equal "testfoo", Liquid::Template.parse(templ).render("procs" => [p])
end
def test_map_works_on_enumerables
assert_equal "123", Liquid::Template.parse('{{ foo | map: "foo" }}').render!("foo" => TestEnumerable.new)
end
def test_date
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")