Skip to content

Instantly share code, notes, and snippets.

@ivalentinee
Last active September 10, 2015 06:50
Show Gist options
  • Save ivalentinee/9b48dcd946232a2fcebb to your computer and use it in GitHub Desktop.
Save ivalentinee/9b48dcd946232a2fcebb to your computer and use it in GitHub Desktop.
MoneyRails user input workaround
class PaymentInput < SimpleForm::Inputs::Base
def input(wrapper_options)
custom_input_options = { value: value, maxlength: 16 }
merged_input_options = merge_wrapper_options(input_html_options, wrapper_options)
merged_input_options = merge_wrapper_options(custom_input_options, merged_input_options)
@builder.text_field(attribute_name, merged_input_options)
end
def value
before_type_cast_value = object.send("#{attribute_name}_money_before_type_cast")
current_value = object.send(attribute_name)
humanized_current_value = template.humanized_money(current_value)
before_type_cast_value || humanized_current_value
end
end
@bertBruynooghe
Copy link

Problem here is that before_type_cast_value is always filled, even if value is correctly set from a float, meaning the output does not humanize the input in this case.

example:

i = Invoice.new(amount: 14.6)
puts humanized_money(i.amount) /* 14,6 for dutch locale */
puts i.send(:amount_money_before_type_cast) /* 14.6 */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment