mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 17:25:41 +03:00
Merge pull request #232 from Shopify/to_liquid_stuff
Always call 'to_liquid' on stuff in map filter and allow to_liquid to be...
This commit is contained in:
@@ -54,7 +54,7 @@ module Liquid
|
||||
|
||||
# Check for method existence without invoking respond_to?, which creates symbols
|
||||
def self.invokable?(method_name)
|
||||
@invokable_methods ||= Set.new((public_instance_methods - Liquid::Drop.public_instance_methods).map(&:to_s))
|
||||
@invokable_methods ||= Set.new(["to_liquid"] + (public_instance_methods - Liquid::Drop.public_instance_methods).map(&:to_s))
|
||||
@invokable_methods.include?(method_name.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -101,7 +101,13 @@ module Liquid
|
||||
def map(input, property)
|
||||
ary = [input].flatten
|
||||
ary.map do |e|
|
||||
e.respond_to?('[]') ? e[property] : nil
|
||||
if e.respond_to?(:to_liquid)
|
||||
e = e.to_liquid
|
||||
end
|
||||
|
||||
if e.respond_to?('[]')
|
||||
e[property]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -86,6 +86,11 @@ class DropsTest < Test::Unit::TestCase
|
||||
assert_equal "", Liquid::Template.parse('{{ product | map: "whatever" }}').render('product' => ProductDrop.new)
|
||||
end
|
||||
|
||||
def test_drops_respond_to_to_liquid
|
||||
assert_equal "text1", Liquid::Template.parse("{{ product.to_liquid.texts.text }}").render('product' => ProductDrop.new)
|
||||
assert_equal "text1", Liquid::Template.parse('{{ product | map: "to_liquid" | map: "texts" | map: "text" }}').render('product' => ProductDrop.new)
|
||||
end
|
||||
|
||||
def test_text_drop
|
||||
output = Liquid::Template.parse( ' {{ product.texts.text }} ' ).render('product' => ProductDrop.new)
|
||||
assert_equal ' text1 ', output
|
||||
|
||||
Reference in New Issue
Block a user