mirror of
https://github.com/kemko/liquid.git
synced 2026-01-07 10:45:42 +03: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/condition'
|
||||
require 'liquid/module_ex'
|
||||
require 'liquid/utils'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
|
||||
@@ -20,11 +20,10 @@ module Liquid
|
||||
def render(context)
|
||||
collection = context[@collection_name] or return ''
|
||||
|
||||
if @attributes['limit'] or @attributes['offset']
|
||||
limit = context[@attributes['limit']] || -1
|
||||
offset = context[@attributes['offset']] || 0
|
||||
collection = collection[offset.to_i..(limit.to_i + offset.to_i - 1)]
|
||||
end
|
||||
from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0
|
||||
to = @attributes['limit'] ? from + context[@attributes['limit']].to_i - 1 : nil
|
||||
|
||||
collection = Utils.slice_collection_using_each(collection, from, to)
|
||||
|
||||
length = collection.length
|
||||
|
||||
|
||||
@@ -86,10 +86,10 @@ module Liquid
|
||||
|
||||
limit = context[@attributes['limit']]
|
||||
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?
|
||||
|
||||
segment.reverse! if @reversed
|
||||
@@ -119,30 +119,6 @@ module Liquid
|
||||
end
|
||||
result
|
||||
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
|
||||
|
||||
@@ -151,11 +127,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def iterable?(collection)
|
||||
collection.respond_to?(:each) || non_blank_string?(collection)
|
||||
end
|
||||
|
||||
def non_blank_string?(collection)
|
||||
collection.is_a?(String) && collection != ''
|
||||
collection.respond_to?(:each) || Utils.non_blank_string?(collection)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
31
lib/liquid/utils.rb
Normal file
31
lib/liquid/utils.rb
Normal file
@@ -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
|
||||
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
|
||||
|
||||
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]})
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user