DEV: Replace custom Onebox blank implementation with ActiveSupport (#23827)

We have a custom implementation of #blank? in our Onebox helpers. This is likely a legacy from when Onebox was a standalone gem. This change replaces all usages with respective incarnations of #blank?, #present?, and #presence from ActiveSupport. It changes a bunch of "unless blank" to "if present" as well.
This commit is contained in:
Ted Johansson
2023-10-07 19:54:26 +02:00
committed by GitHub
parent 85c6914f80
commit 60e624e768
15 changed files with 42 additions and 77 deletions

View File

@ -165,9 +165,7 @@ module Onebox
end
http.request_head([uri.path, uri.query].join("?")) do |response|
code = response.code.to_i
return nil unless code === 200 || Onebox::Helpers.blank?(response.content_length)
return response.content_length
return response.code.to_i == 200 ? response.content_length.presence : nil
end
end
end
@ -190,27 +188,17 @@ module Onebox
"<div style=\"background:transparent;position:relative;width:#{width}px;height:#{height}px;top:#{height}px;margin-top:-#{height}px;\" onClick=\"style.pointerEvents='none'\"></div>"
end
def self.blank?(value)
if value.nil?
true
elsif String === value
value.empty? || !(/[[:^space:]]/ === value)
else
value.respond_to?(:empty?) ? !!value.empty? : !value
end
end
def self.truncate(string, length = 50)
return string if string.nil?
string.size > length ? string[0...(string.rindex(" ", length) || length)] + "..." : string
end
def self.get(meta, attr)
(meta && !blank?(meta[attr])) ? sanitize(meta[attr]) : nil
(meta && meta[attr].present?) ? sanitize(meta[attr]) : nil
end
def self.sanitize(value, length = 50)
return nil if blank?(value)
return nil if value.blank?
Sanitize.fragment(value).strip
end