Module: WhittakerTech::Midas::FormHelper

Defined in:
app/helpers/whittaker_tech/midas/form_helper.rb

Overview

Form helper for rendering currency input fields with bank-style typing behavior.

This helper provides a headless currency input that the parent application can style according to its design system. The helper only provides the behavior (via Stimulus) and basic structure.

Examples:

Basic usage (unstyled)

<%= midas_currency_field f, :price, currency_code: 'USD' %>

With Tailwind styling

<%= midas_currency_field f, :price,
      currency_code: 'USD',
      input_html: { class: 'rounded-lg border-gray-300 text-right' },
      wrapper_html: { class: 'mb-4' },
      label: 'Product Price' %>

With Bootstrap styling

<%= midas_currency_field f, :price,
      currency_code: 'EUR',
      input_html: { class: 'form-control text-end' },
      wrapper_html: { class: 'mb-3' },
      label: 'Price (€)' %>

Instance Method Summary collapse

Instance Method Details

#midas_currency_field(form, attribute, currency_code:, **options) ⇒ String

Renders a currency input field with bank-style typing behavior.

The field consists of:

  • A visible formatted input (displays dollars/euros)
  • A hidden input storing minor units (cents)
  • A hidden input storing the currency code

Examples:

<%= midas_currency_field f, :price,
      currency_code: 'USD',
      decimals: 2,
      label: 'Sale Price',
      wrapper_html: { class: 'form-group' },
      input_html: { class: 'form-control', placeholder: '0.00' } %>

Parameters:

  • form (ActionView::Helpers::FormBuilder)

    The form builder

  • attribute (Symbol)

    The coin attribute name (e.g., :price, :cost)

  • currency_code (String)

    ISO 4217 currency code (e.g., 'USD', 'EUR')

  • options (Hash)

    Additional options

Options Hash (**options):

  • :input_html (Hash)

    HTML attributes for the display input

  • :wrapper_html (Hash)

    HTML attributes for the wrapper div

  • :decimals (Integer)

    Number of decimal places (default: 2)

  • :label (String)

    Label text (optional, no label if nil)

Returns:

  • (String)

    Rendered HTML for the currency field



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/whittaker_tech/midas/form_helper.rb', line 51

def midas_currency_field(form, attribute, currency_code:, **options)
  input_html = options.fetch(:input_html, {})
  wrapper_html = options.fetch(:wrapper_html, {})
  decimals = options.fetch(:decimals, 2)
  label_text = options.fetch(:label, nil)

  # Get current value if coin exists
  resource = form.object
  coin = resource.public_send(attribute) if resource.respond_to?(attribute)
  current_minor = coin&.currency_minor || 0

  render partial: 'whittaker_tech/midas/shared/currency_field',
         locals: {
           form: form,
           attribute: attribute,
           currency_code: currency_code,
           current_minor: current_minor,
           decimals: decimals,
           input_html: input_html,
           wrapper_html: wrapper_html,
           label_text: label_text
         }
end