Sha256: 74166c286da655fc8a3c1d699ee6950e9649c96862c81aaf3988d6e734bd3763

Contents?: true

Size: 1.26 KB

Versions: 33

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module EacRailsUtils
  module CommonFormHelper
    class FormBuilder
      module CurrencyField
        def currency_field(field_name, options = {})
          hidden_id = SecureRandom.hex(5)
          visible_id = SecureRandom.hex(5)
          cf = field(field_name, options) do
            form.hidden_field(field_name, id: hidden_id) <<
              helper.text_field_tag("#{field_name}_visible", '',
                                    id: visible_id, class: 'form-control')
          end
          cf << javascript_currency_mask(hidden_id, visible_id)
        end

        private

        def javascript_currency_mask(hidden_id, visible_id)
          helper.content_tag :script do
            helper.raw("new CurrencyField('#{hidden_id}', '#{visible_id}', " \
                "#{mask_money_options});")
          end
        end

        def mask_money_options
          <<~JSON_CODE
            {
              prefix: '#{I18n.t('number.currency.format.unit')} ',
              allowNegative: true,
              thousands: '#{I18n.t('number.currency.format.delimiter')}',
              decimal: '#{I18n.t('number.currency.format.separator')}',
              affixesStay: false
            }
          JSON_CODE
        end
      end
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
eac_rails_utils-0.22.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.22.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.22.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.21.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.20.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.19.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.18.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.17.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.17.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.17.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.16.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.15.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.15.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.15.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.14.1 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.14.0 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.13.5 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.13.4 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.13.3 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb
eac_rails_utils-0.13.2 app/helpers/eac_rails_utils/common_form_helper/form_builder/currency_field.rb