Skip to content

Commit

Permalink
use to_i to parse parameters of tablerow tag
Browse files Browse the repository at this point in the history
  • Loading branch information
ggmichaelgo committed Jan 17, 2023
1 parent 0f11c97 commit e889a9d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 18 deletions.
25 changes: 11 additions & 14 deletions lib/liquid/tags/table_row.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,13 @@ def initialize(tag_name, markup, options)
def render_to_output_buffer(context, output)
(collection = context.evaluate(@collection_name)) || (return '')

from = if @attributes.key?('offset')
Utils.to_integer(context.evaluate(@attributes['offset']), allow_nil: true)
else
0
end

to = if @attributes.key?('limit')
from + Utils.to_integer(context.evaluate(@attributes['limit']), allow_nil: true)
end
from = @attributes.key?('offset') ? to_integer(context.evaluate(@attributes['offset'])) : 0
to = @attributes.key?('limit') ? from + to_integer(context.evaluate(@attributes['limit'])) : nil

collection = Utils.slice_collection(collection, from, to)
length = collection.length

cols = if @attributes.key?('cols')
Utils.to_integer(context.evaluate(@attributes['cols']), allow_nil: true)
else
length
end
cols = @attributes.key?('cols') ? to_integer(context.evaluate(@attributes['cols'])) : length

output << "<tr class=\"row1\">\n"
context.stack do
Expand Down Expand Up @@ -93,6 +82,14 @@ def children
super + @node.attributes.values + [@node.collection_name]
end
end

private

def to_integer(value)
value.to_i
rescue NoMethodError
raise Liquid::ArgumentError, "invalid integer"
end
end

Template.register_tag('tablerow', TableRow)
Expand Down
5 changes: 1 addition & 4 deletions lib/liquid/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@ def self.slice_collection_using_each(collection, from, to)
segments
end

def self.to_integer(num, allow_nil: false)
def self.to_integer(num)
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
begin
Integer(num)
Expand Down

0 comments on commit e889a9d

Please sign in to comment.