Module: Liquid::Filters::Helpers

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

Instance Method Summary collapse

Instance Method Details

#currency(price) ⇒ Object



6
7
8
9
# File 'app/lib/liquid/filters/helpers.rb', line 6

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)



35
36
37
38
39
# File 'app/lib/liquid/filters/helpers.rb', line 35

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



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

def fallback(input, fallback_text)
	input.blank? ? fallback_text : input
end

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



15
16
17
18
19
20
21
22
23
24
# File 'app/lib/liquid/filters/helpers.rb', line 15

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