Sha256: 46516f3d23313459d940acbeb49f8cea6bcb78caadc31a096b7afd3c3e8323c3

Contents?: true

Size: 1.26 KB

Versions: 20

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Katalyst
  module Tables
    module Cells
      # Formats the value as a number
      #
      # Adds a class to the cell to allow for custom styling
      class NumberComponent < CellComponent
        include ActiveSupport::NumberHelper

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

          @format = format
          @options = options
        end

        def format(value)
          case @format
          when :phone
            number_to_phone(value, @options)
          when :currency
            number_to_currency(value, @options)
          when :percentage
            number_to_percentage(value, @options)
          when :delimited
            number_to_delimited(value, @options)
          when :rounded
            number_to_rounded(value, @options)
          when :human_size
            number_to_human_size(value, @options)
          when :human
            number_to_human(value, @options)
          else
            raise ArgumentError, "Unsupported format #{@format}"
          end
        end

        def rendered_value
          value.present? ? format(value) : ""
        end

        private

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

Version data entries

20 entries across 20 versions & 1 rubygems

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