Ruby 1.8 compatibility fix: Ensure for-loop on an empty string does not enter for-body.

This commit is contained in:
Dennis Theisen
2012-02-29 14:41:21 -05:00
parent 1b2d0198ea
commit 3d7c1c80a0
2 changed files with 8 additions and 1 deletions

View File

@@ -76,7 +76,7 @@ module Liquid
collection = collection.to_a if collection.is_a?(Range)
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
return render_else(context) unless collection.respond_to?(:each) or collection.is_a?(String)
return render_else(context) unless iterable?(collection)
from = if @attributes['offset'] == 'continue'
context.registers[:for][@name].to_i
@@ -150,6 +150,9 @@ module Liquid
return @else_block ? [render_all(@else_block, context)] : ''
end
def iterable?(collection)
collection.respond_to?(:each) || (collection.is_a?(String) && collection != '')
end
end
Template.register_tag('for', For)

View File

@@ -195,4 +195,8 @@ HERE
'{{val}}{%endfor%}',
'string' => "test string")
end
def test_blank_string_not_iterable
assert_template_result('', "{% for char in characters %}I WILL NOT BE OUTPUT{% endfor %}", 'characters' => '')
end
end