From b699c93bae1051975d7fb9294294cf53a6f51d70 Mon Sep 17 00:00:00 2001 From: Daniel Huckstep Date: Wed, 13 Feb 2013 16:35:39 -0700 Subject: [PATCH 1/4] Allow a Liquid::Drop to be passed into Template#render --- lib/liquid/template.rb | 2 ++ test/liquid/template_test.rb | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index a4895d8..19c2bfa 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -93,6 +93,8 @@ module Liquid context = case args.first when Liquid::Context args.shift + when Liquid::Drop + Context.new(args.shift, instance_assigns, registers, @rethrow_errors) when Hash Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) when nil diff --git a/test/liquid/template_test.rb b/test/liquid/template_test.rb index 6fb68e8..5207f18 100644 --- a/test/liquid/template_test.rb +++ b/test/liquid/template_test.rb @@ -1,5 +1,15 @@ require 'test_helper' +class TemplateContextDrop < Liquid::Drop + def before_method(method) + method + end + + def foo + 'foo' + end +end + class TemplateTest < Test::Unit::TestCase include Liquid @@ -120,4 +130,11 @@ class TemplateTest < Test::Unit::TestCase assert t.resource_limits[:render_score_current] > 0 assert t.resource_limits[:render_length_current] > 0 end + + def test_can_use_drop_as_context + t = Template.new + drop = TemplateContextDrop.new + assert_equal 'foo', t.parse('{{foo}}').render(drop) + assert_equal 'bar', t.parse('{{bar}}').render(drop) + end end # TemplateTest From ba5e65f68518e28b1810040a2a00d58839f465de Mon Sep 17 00:00:00 2001 From: Daniel Huckstep Date: Mon, 27 May 2013 18:17:13 -0600 Subject: [PATCH 2/4] Better test, resuse Hash block --- lib/liquid/template.rb | 4 +--- test/liquid/template_test.rb | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 19c2bfa..3585dfc 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -93,9 +93,7 @@ module Liquid context = case args.first when Liquid::Context args.shift - when Liquid::Drop - Context.new(args.shift, instance_assigns, registers, @rethrow_errors) - when Hash + when Hash, Liquid::Drop Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) when nil Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits) diff --git a/test/liquid/template_test.rb b/test/liquid/template_test.rb index 5207f18..22e1eda 100644 --- a/test/liquid/template_test.rb +++ b/test/liquid/template_test.rb @@ -6,7 +6,7 @@ class TemplateContextDrop < Liquid::Drop end def foo - 'foo' + 'fizzbuzz' end end @@ -134,7 +134,7 @@ class TemplateTest < Test::Unit::TestCase def test_can_use_drop_as_context t = Template.new drop = TemplateContextDrop.new - assert_equal 'foo', t.parse('{{foo}}').render(drop) + assert_equal 'fizzbuzz', t.parse('{{foo}}').render(drop) assert_equal 'bar', t.parse('{{bar}}').render(drop) end end # TemplateTest From 076ae903c0dc9c838297dce4609e9f97fbaf383f Mon Sep 17 00:00:00 2001 From: Daniel Huckstep Date: Wed, 5 Jun 2013 20:27:14 -0600 Subject: [PATCH 3/4] Make sure the context gets set --- lib/liquid/template.rb | 7 +++++-- test/liquid/template_test.rb | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 3585dfc..a65063c 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -93,8 +93,11 @@ module Liquid context = case args.first when Liquid::Context args.shift - when Hash, Liquid::Drop - Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) + when Liquid::Drop + drop = args.shift + drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) + when Hash + Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors) when nil Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits) else diff --git a/test/liquid/template_test.rb b/test/liquid/template_test.rb index 22e1eda..04f1e50 100644 --- a/test/liquid/template_test.rb +++ b/test/liquid/template_test.rb @@ -8,6 +8,10 @@ class TemplateContextDrop < Liquid::Drop def foo 'fizzbuzz' end + + def baz + @context.registers['lulz'] + end end class TemplateTest < Test::Unit::TestCase @@ -133,8 +137,10 @@ class TemplateTest < Test::Unit::TestCase def test_can_use_drop_as_context t = Template.new + t.registers['lulz'] = 'haha' drop = TemplateContextDrop.new assert_equal 'fizzbuzz', t.parse('{{foo}}').render(drop) assert_equal 'bar', t.parse('{{bar}}').render(drop) + assert_equal 'haha', t.parse("{{baz}}").render(drop) end end # TemplateTest From f8288546f872357ff69c2ef45544fadd9280762e Mon Sep 17 00:00:00 2001 From: Daniel Huckstep Date: Thu, 6 Jun 2013 10:20:34 -0600 Subject: [PATCH 4/4] Missed in the rebase conflicts --- lib/liquid/template.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index a65063c..5e0675f 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -97,7 +97,7 @@ module Liquid drop = args.shift drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) when Hash - Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors) + Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits) when nil Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits) else