use to_i to parse parameters of tablerow tag

This commit is contained in:
Michael Go
2023-01-17 17:48:16 -04:00
parent 0f11c97623
commit e889a9da0b
2 changed files with 12 additions and 18 deletions
+11 -14
View File
@@ -45,24 +45,13 @@ module Liquid
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)
(collection = context.evaluate(@collection_name)) || (return '') (collection = context.evaluate(@collection_name)) || (return '')
from = if @attributes.key?('offset') from = @attributes.key?('offset') ? to_integer(context.evaluate(@attributes['offset'])) : 0
Utils.to_integer(context.evaluate(@attributes['offset']), allow_nil: true) to = @attributes.key?('limit') ? from + to_integer(context.evaluate(@attributes['limit'])) : nil
else
0
end
to = if @attributes.key?('limit')
from + Utils.to_integer(context.evaluate(@attributes['limit']), allow_nil: true)
end
collection = Utils.slice_collection(collection, from, to) collection = Utils.slice_collection(collection, from, to)
length = collection.length length = collection.length
cols = if @attributes.key?('cols') cols = @attributes.key?('cols') ? to_integer(context.evaluate(@attributes['cols'])) : length
Utils.to_integer(context.evaluate(@attributes['cols']), allow_nil: true)
else
length
end
output << "<tr class=\"row1\">\n" output << "<tr class=\"row1\">\n"
context.stack do context.stack do
@@ -93,6 +82,14 @@ module Liquid
super + @node.attributes.values + [@node.collection_name] super + @node.attributes.values + [@node.collection_name]
end end
end end
private
def to_integer(value)
value.to_i
rescue NoMethodError
raise Liquid::ArgumentError, "invalid integer"
end
end end
Template.register_tag('tablerow', TableRow) Template.register_tag('tablerow', TableRow)
+1 -4
View File
@@ -39,11 +39,8 @@ module Liquid
segments segments
end end
def self.to_integer(num, allow_nil: false) def self.to_integer(num)
return num if num.is_a?(Integer) return num if num.is_a?(Integer)
# with allow_nil param, return 0 which is equal to nil.to_i
return 0 if num.nil? && allow_nil
num = num.to_s num = num.to_s
begin begin
Integer(num) Integer(num)