Sha256: 4afb49367d4406233044fa4be1dc86ff3ba2ae60113d8c68cf95d818b12d86f6

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require "bigdecimal/util"

module Katalyst
  module Tables
    module Cells
      # 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
      class CurrencyComponent < CellComponent
        def initialize(options:, **)
          super(**)

          @options = options
        end

        def rendered_value
          format(value)
        end

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

        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

        private

        def default_html_attributes
          { class: "type-currency" }
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
katalyst-tables-3.6.0 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.5.5 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.5.4 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.5.3 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.5.2 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.5.1 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.5.0 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.6 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.5 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.4 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.3 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.2 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.1 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.4.0 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.3.4 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.3.3 app/components/katalyst/tables/cells/currency_component.rb
katalyst-tables-3.3.2 app/components/katalyst/tables/cells/currency_component.rb