From 182d3fefb6868b1d494883acfca00b0e960b1ab6 Mon Sep 17 00:00:00 2001 From: Florian Weingarten Date: Thu, 25 Jul 2013 16:56:08 -0400 Subject: [PATCH] Always call 'to_liquid' on staff in map filter and allow to_liquid to be called on drops --- lib/liquid/drop.rb | 2 +- lib/liquid/standardfilters.rb | 8 +++++++- test/liquid/drop_test.rb | 5 +++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/liquid/drop.rb b/lib/liquid/drop.rb index 88adb6b..e8eb233 100644 --- a/lib/liquid/drop.rb +++ b/lib/liquid/drop.rb @@ -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 diff --git a/lib/liquid/standardfilters.rb b/lib/liquid/standardfilters.rb index 080fc97..f03fc3b 100644 --- a/lib/liquid/standardfilters.rb +++ b/lib/liquid/standardfilters.rb @@ -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 diff --git a/test/liquid/drop_test.rb b/test/liquid/drop_test.rb index 235a52f..fc972aa 100644 --- a/test/liquid/drop_test.rb +++ b/test/liquid/drop_test.rb @@ -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