Method: Formant::FormObject.reformat

Defined in:
lib/formant.rb

.reformat(field_name, options = {}) ⇒ Object

Directive to add a reformat rule for the specified attribute. These let you apply any special formatting/normalization logic on the form’s attributes upon output. Typically you want to do this when you have to redisplay a form.

The format rules are triggered when the FormObject#reformatted! method is invoked, which modifies the attribute in place.

Usage example:

class MyForm < FormObject

...
reformat :appointment_time, as: :datetime, format: :day_date_time
reformat :phone, as: :phone_number, country_code: 'US'
...

end

The above example specifies that the appointment_time attribute should be reformatted with the format_datetime method (using the format specified in the locale file with the :day_date_time key), and that the phone attribute should be parsed with the parse_phone_number method, and a fixed country_code of ‘US’



90
91
92
93
# File 'lib/formant.rb', line 90

def self.reformat(field_name, options={})
  self.format_fields ||= []
  self.format_fields << [ field_name, "format_#{options[:as]}", options ]
end