Merge pull request #349 from Shopify/render_bang_allow_not_safe_contexts

render! will properly force rethrow of errors if context is passed as an argument
This commit is contained in:
Thierry Joyal
2014-05-01 12:52:06 -04:00
5 changed files with 26 additions and 3 deletions

View File

@@ -1,7 +1,8 @@
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 2.1
- 2.1.1
- jruby-19mode
- jruby-head
- rbx-19mode

View File

@@ -3,6 +3,7 @@
## 3.0.0 / not yet released / branch "master"
* ...
* Properly set context rethrow_errors on render! #349 [Thierry Joyal, tjoyal]
* Fix broken rendering of variables which are equal to false, see #345 [Florian Weingarten, fw42]
* Remove ActionView template handler [Dylan Thacker-Smith, dylanahsmith]
* Freeze lots of string literals for new Ruby 2.1 optimization, see #297 [Florian Weingarten, fw42]

View File

@@ -15,6 +15,8 @@ module Liquid
class Context
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
attr_accessor :rethrow_errors
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = {})
@environments = [environments].flatten
@scopes = [(outer_scope || {})]

View File

@@ -113,7 +113,9 @@ module Liquid
context = case args.first
when Liquid::Context
args.shift
c = args.shift
c.rethrow_errors = true if @rethrow_errors
c
when Liquid::Drop
drop = args.shift
drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
@@ -156,7 +158,8 @@ module Liquid
end
def render!(*args)
@rethrow_errors = true; render(*args)
@rethrow_errors = true
render(*args)
end
private

View File

@@ -22,6 +22,12 @@ class SomethingWithLength
liquid_methods :length
end
class ErroneousDrop < Liquid::Drop
def bad_method
raise 'ruby error in drop'
end
end
class TemplateTest < Test::Unit::TestCase
include Liquid
@@ -137,4 +143,14 @@ class TemplateTest < Test::Unit::TestCase
assert_equal 'bar', t.parse('{{bar}}').render!(drop)
assert_equal 'haha', t.parse("{{baz}}").render!(drop)
end
def test_render_bang_force_rethrow_errors_on_passed_context
context = Context.new({'drop' => ErroneousDrop.new})
t = Template.new.parse('{{ drop.bad_method }}')
e = assert_raises RuntimeError do
t.render!(context)
end
assert_equal 'ruby error in drop', e.message
end
end