Class: Katalyst::Tables::Cells::CurrencyComponent

Inherits:
Katalyst::Tables::CellComponent show all
Defined in:
app/components/katalyst/tables/cells/currency_component.rb

Overview

Formats the value as a money value

The value is assumed to be cents if integer, or dollars if float or decimal. Also supports RubyMoney type if defined.

Adds a class to the cell to allow for custom styling

Instance Attribute Summary

Attributes inherited from Katalyst::Tables::CellComponent

#collection, #column, #record, #row

Instance Method Summary collapse

Methods inherited from Katalyst::Tables::CellComponent

#call, #heading?, #inspect, #label, #to_s, #with_content_wrapper

Constructor Details

#initialize(options:) ⇒ CurrencyComponent

Returns a new instance of CurrencyComponent.



15
16
17
18
19
# File 'app/components/katalyst/tables/cells/currency_component.rb', line 15

def initialize(options:, **)
  super(**)

  @options = options
end

Instance Method Details

#format(value) ⇒ Object



25
26
27
# File 'app/components/katalyst/tables/cells/currency_component.rb', line 25

def format(value)
  value.present? ? number_to_currency(value, @options) : ""
end

#rendered_valueObject



21
22
23
# File 'app/components/katalyst/tables/cells/currency_component.rb', line 21

def rendered_value
  format(value)
end

#valueObject



29
30
31
32
33
34
35
36
37
38
# File 'app/components/katalyst/tables/cells/currency_component.rb', line 29

def value
  case (v = super)
  when nil
    nil
  when Integer
    (super.to_d / BigDecimal(100))
  else
    (v.to_d rescue nil) # rubocop:disable Style/RescueModifier
  end
end