Sha256: 04fab5af1544410822bedbeca848226b1408ddaf41b575f94dffeeeac6d0a86d

Contents?: true

Size: 1001 Bytes

Versions: 1

Compression:

Stored size: 1001 Bytes

Contents

# frozen_string_literal: true

require 'rails'
require 'money'
require 'administrate/field/text'
require 'administrate/engine'

Money.default_currency = 'USD'
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN

module Administrate
  module Field
    class Money < Administrate::Field::Text
      delegate :currency, to: :money

      class Engine < ::Rails::Engine
        Administrate::Engine.add_javascript 'administrate-field-money/application'
      end

      def to_s
        money.format(symbol: symbol, separator: separator, delimiter: delimiter)
      end

      def money
        @money ||= ::Money.new(data, code)
      end

      def code
        options.fetch(:code, ::Money.default_currency.iso_code)
      end

      def symbol
        options.fetch(:symbol, currency.symbol)
      end

      def delimiter
        options.fetch(:delimiter, currency.thousands_separator)
      end

      def separator
        options.fetch(:separator, currency.decimal_mark)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
administrate-field-money-0.3.0 lib/administrate/field/money.rb