Module: Liquid::Filters::Helpers

Includes:
ActionView::Helpers::NumberHelper
Defined in:
app/lib/liquid/filters/helpers.rb

Overview

Library code: helpers.

Instance Method Summary collapse

Instance Method Details

#currency(price) ⇒ Object



9
10
11
12
# File 'app/lib/liquid/filters/helpers.rb', line 9

def currency(price)
  unit = @context&.[]('currency_unit') || '$'
  number_to_currency(price, unit: unit)
end

#electricity_cost_per_hour(watts, rate) ⇒ Object

Returns the per-hour electricity operating cost for a given wattage.

Usage in Liquid templates (rate comes from the electricity_rate context variable):
1000 | electricity_cost_per_hour: electricity_rate } → "0.16"
1000 | electricity_cost_per_hour: electricity_rate | currency } → "$0.16"

watts - numeric wattage (the filter input)
rate - $/kWh rate (pass electricity_rate variable from context)



38
39
40
41
42
# File 'app/lib/liquid/filters/helpers.rb', line 38

def electricity_cost_per_hour(watts, rate)
  return 0 if rate.to_f <= 0 || watts.to_f <= 0

  (watts.to_f / 1000.0 * rate.to_f).round(4)
end

#fallback(input, fallback_text) ⇒ Object



14
15
16
# File 'app/lib/liquid/filters/helpers.rb', line 14

def fallback(input, fallback_text)
  input.presence || fallback_text
end

#pluralize(input, singular, plural = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'app/lib/liquid/filters/helpers.rb', line 18

def pluralize(input, singular, plural = nil)
  # Liquid::ParseEnvironment.parse("{{ 2 | pluralize: 'year' : 'years' }}").render
  if input.to_i == 1
    "#{input} #{singular}"
  elsif plural
    "#{input} #{plural}"
  else
    "#{input} #{singular.pluralize}"
  end
end

#with_background(input, color) ⇒ Object

Override the background color on a pre-rendered signature's outer .email-wrapper
so a CRM user can blend the signature into their own colored wrapper:



49
50
51
52
53
54
55
56
57
58
59
# File 'app/lib/liquid/filters/helpers.rb', line 49

def with_background(input, color)
  return input if input.blank?

  safe_color = color.to_s.strip
  return input if safe_color.empty? || safe_color.length > 64
  return input if safe_color.match?(/[;"'<>\\]/)

  input.to_s.sub(/(<div\b[^>]*\bclass="email-wrapper"[^>]*style="[^"]*?)background:\s*[^;"]+/) do
    "#{Regexp.last_match(1)}background: #{safe_color}"
  end
end