mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Allow tablerow to work with any Enumerable. Closes #132
This commit is contained in:
@@ -60,6 +60,7 @@ require 'liquid/htmltags'
|
|||||||
require 'liquid/standardfilters'
|
require 'liquid/standardfilters'
|
||||||
require 'liquid/condition'
|
require 'liquid/condition'
|
||||||
require 'liquid/module_ex'
|
require 'liquid/module_ex'
|
||||||
|
require 'liquid/utils'
|
||||||
|
|
||||||
# Load all the tags of the standard library
|
# Load all the tags of the standard library
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -20,11 +20,10 @@ module Liquid
|
|||||||
def render(context)
|
def render(context)
|
||||||
collection = context[@collection_name] or return ''
|
collection = context[@collection_name] or return ''
|
||||||
|
|
||||||
if @attributes['limit'] or @attributes['offset']
|
from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0
|
||||||
limit = context[@attributes['limit']] || -1
|
to = @attributes['limit'] ? from + context[@attributes['limit']].to_i - 1 : nil
|
||||||
offset = context[@attributes['offset']] || 0
|
|
||||||
collection = collection[offset.to_i..(limit.to_i + offset.to_i - 1)]
|
collection = Utils.slice_collection_using_each(collection, from, to)
|
||||||
end
|
|
||||||
|
|
||||||
length = collection.length
|
length = collection.length
|
||||||
|
|
||||||
|
|||||||
+5
-33
@@ -86,10 +86,10 @@ module Liquid
|
|||||||
|
|
||||||
limit = context[@attributes['limit']]
|
limit = context[@attributes['limit']]
|
||||||
to = limit ? limit.to_i + from : nil
|
to = limit ? limit.to_i + from : nil
|
||||||
|
|
||||||
|
|
||||||
segment = slice_collection_using_each(collection, from, to)
|
segment = Utils.slice_collection_using_each(collection, from, to)
|
||||||
|
|
||||||
return render_else(context) if segment.empty?
|
return render_else(context) if segment.empty?
|
||||||
|
|
||||||
segment.reverse! if @reversed
|
segment.reverse! if @reversed
|
||||||
@@ -119,30 +119,6 @@ module Liquid
|
|||||||
end
|
end
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def slice_collection_using_each(collection, from, to)
|
|
||||||
segments = []
|
|
||||||
index = 0
|
|
||||||
yielded = 0
|
|
||||||
|
|
||||||
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
|
||||||
return [collection] if non_blank_string?(collection)
|
|
||||||
|
|
||||||
collection.each do |item|
|
|
||||||
|
|
||||||
if to && to <= index
|
|
||||||
break
|
|
||||||
end
|
|
||||||
|
|
||||||
if from <= index
|
|
||||||
segments << item
|
|
||||||
end
|
|
||||||
|
|
||||||
index += 1
|
|
||||||
end
|
|
||||||
|
|
||||||
segments
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
@@ -151,11 +127,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def iterable?(collection)
|
def iterable?(collection)
|
||||||
collection.respond_to?(:each) || non_blank_string?(collection)
|
collection.respond_to?(:each) || Utils.non_blank_string?(collection)
|
||||||
end
|
|
||||||
|
|
||||||
def non_blank_string?(collection)
|
|
||||||
collection.is_a?(String) && collection != ''
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
module Liquid
|
||||||
|
module Utils
|
||||||
|
def self.slice_collection_using_each(collection, from, to)
|
||||||
|
segments = []
|
||||||
|
index = 0
|
||||||
|
yielded = 0
|
||||||
|
|
||||||
|
# Maintains Ruby 1.8.7 String#each behaviour on 1.9
|
||||||
|
return [collection] if non_blank_string?(collection)
|
||||||
|
|
||||||
|
collection.each do |item|
|
||||||
|
|
||||||
|
if to && to <= index
|
||||||
|
break
|
||||||
|
end
|
||||||
|
|
||||||
|
if from <= index
|
||||||
|
segments << item
|
||||||
|
end
|
||||||
|
|
||||||
|
index += 1
|
||||||
|
end
|
||||||
|
|
||||||
|
segments
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.non_blank_string?(collection)
|
||||||
|
collection.is_a?(String) && collection != ''
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -3,6 +3,18 @@ require 'test_helper'
|
|||||||
class HtmlTagTest < Test::Unit::TestCase
|
class HtmlTagTest < Test::Unit::TestCase
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
|
class ArrayDrop < Liquid::Drop
|
||||||
|
include Enumerable
|
||||||
|
|
||||||
|
def initialize(array)
|
||||||
|
@array = array
|
||||||
|
end
|
||||||
|
|
||||||
|
def each(&block)
|
||||||
|
@array.each(&block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_html_table
|
def test_html_table
|
||||||
|
|
||||||
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
|
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
|
||||||
@@ -36,4 +48,10 @@ class HtmlTagTest < Test::Unit::TestCase
|
|||||||
'collections' => {'frontpage' => [1,2,3,4,5,6]})
|
'collections' => {'frontpage' => [1,2,3,4,5,6]})
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_enumerable_drop
|
||||||
|
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 4 </td><td class=\"col2\"> 5 </td><td class=\"col3\"> 6 </td></tr>\n",
|
||||||
|
'{% tablerow n in numbers cols:3%} {{n}} {% endtablerow %}',
|
||||||
|
'numbers' => ArrayDrop.new([1,2,3,4,5,6]))
|
||||||
|
end
|
||||||
end # HtmlTagTest
|
end # HtmlTagTest
|
||||||
|
|||||||
Reference in New Issue
Block a user