Merge pull request #601 from Shopify/safe-to-integer

Use to_integer instead of to_i on arguments
This commit is contained in:
David Cornu
2015-06-16 11:31:20 -04:00
2 changed files with 9 additions and 3 deletions

View File

@@ -114,3 +114,7 @@ Style/PerlBackrefs:
Style/WordArray:
Enabled: false
Style/ModuleLength:
Exclude:
- lib/liquid/standardfilters.rb

View File

@@ -59,15 +59,17 @@ module Liquid
# Truncate a string down to x characters
def truncate(input, length = 50, truncate_string = "...".freeze)
return if input.nil?
l = length.to_i - truncate_string.length
length = to_integer(length)
l = length - truncate_string.length
l = 0 if l < 0
input.length > length.to_i ? input[0...l] + truncate_string : input
input.length > length ? input[0...l] + truncate_string : input
end
def truncatewords(input, words = 15, truncate_string = "...".freeze)
return if input.nil?
wordlist = input.to_s.split
l = words.to_i - 1
words = to_integer(words)
l = words - 1
l = 0 if l < 0
wordlist.length > l ? wordlist[0..l].join(" ".freeze) + truncate_string : input
end