Module: MoneyAttribute::FormBuilderExtension
- Defined in:
- lib/money_attribute/form_builder_extension.rb
Overview
Form builder methods for money attributes.
Included into ActionView::Helpers::FormBuilder by the railtie,
providing two helper methods that mirror Rails' text_field and
number_field but work with Mint::Money attribute values.
Both helpers render unbound <input> tags (not scoped to
the form builder's object name), so the submitted value is accessible
via params directly rather than through params[object_name].
Instance Method Summary collapse
-
#money_amount_field(method, options = {}) ⇒ String
Renders a number input for a single-column (fixed-currency) money attribute.
-
#money_field(method, options = {}) ⇒ String
Renders a text input for a composed (amount + currency) money attribute.
Instance Method Details
#money_amount_field(method, options = {}) ⇒ String
Renders a number input for a single-column (fixed-currency) money attribute.
Displays the raw decimal value (e.g. "1234.56") via
Mint::Money#to_d. This is suitable for attributes backed by a single
column where the currency is fixed per application config.
61 62 63 64 65 66 67 |
# File 'lib/money_attribute/form_builder_extension.rb', line 61 def money_amount_field(method, = {}) money_from_column = object.public_send(method) value = money_from_column&.to_d @template.number_field_tag(field_name(method), value, { id: field_id(method) }.merge()) end |
#money_field(method, options = {}) ⇒ String
Renders a text input for a composed (amount + currency) money attribute.
Displays the formatted money string (e.g. "R$ 1.234,56") via
Mint::Money#to_fs. The raw value is submitted as a string; the
application should parse it on the receiving end, typically using
Converter#parse.
37 38 39 40 41 42 43 |
# File 'lib/money_attribute/form_builder_extension.rb', line 37 def money_field(method, = {}) money = object.public_send(method) value = money&.to_fs @template.text_field_tag(field_name(method), value, { id: field_id(method) }.merge()) end |