Merge pull request #234 from Shopify/fix_mapping_procs

Fix mapping over procs
This commit is contained in:
Florian Weingarten
2013-07-26 05:45:56 -07:00
committed by Arthur Neves
parent 4207d1f086
commit 6cb5a9b7cc
2 changed files with 38 additions and 4 deletions

View File

@@ -101,11 +101,12 @@ module Liquid
def map(input, property)
ary = [input].flatten
ary.map do |e|
if e.respond_to?(:to_liquid)
e = e.to_liquid
end
e = e.call if e.is_a?(Proc)
e = e.to_liquid if e.respond_to?(:to_liquid)
if e.respond_to?('[]')
if property == "to_liquid"
e
elsif e.respond_to?(:[])
e[property]
end
end

View File

@@ -6,6 +6,27 @@ class Filters
include Liquid::StandardFilters
end
class TestThing
def initialize
@foo = 0
end
def to_s
"woot: #{@foo}"
end
def to_liquid
@foo += 1
self
end
end
class TestDrop < Liquid::Drop
def test
"testfoo"
end
end
class StandardFiltersTest < Test::Unit::TestCase
include Liquid
@@ -102,6 +123,18 @@ class StandardFiltersTest < Test::Unit::TestCase
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "inspect" }}').render
end
def test_map_calls_to_liquid
t = TestThing.new
assert_equal "woot: 1", Liquid::Template.parse('{{ foo }}').render("foo" => t)
end
def test_map_over_proc
drop = TestDrop.new
p = Proc.new{ drop }
templ = '{{ procs | map: "test" }}'
assert_equal "testfoo", Liquid::Template.parse(templ).render("procs" => [p])
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")