Class: Formant::FormObject
- Inherits:
-
Object
- Object
- Formant::FormObject
- Includes:
- ActiveModel::Model, ActiveModel::Validations::Callbacks
- Defined in:
- lib/formant.rb
Overview
Base class for Form objects.
See www.reinteractive.net/posts/158-form-objects-in-rails (among others) for more info on form objects for more details.
This is a simplified implementation of form objects that focuses on collecting input in a PORO, specifying any special parsing and transformation on fields upon input (i.e., before validation), and special formatting/transformation on fields for display/output and redisplay of forms.
Class Attribute Summary collapse
-
.format_fields ⇒ Object
Returns the value of attribute format_fields.
-
.parse_fields ⇒ Object
Returns the value of attribute parse_fields.
Class Method Summary collapse
-
.parse(field_name, options = {}) ⇒ Object
Directive to add a parse rule for the specified attribute.
-
.reformat(field_name, options = {}) ⇒ Object
Directive to add a reformat rule for the specified attribute.
Instance Method Summary collapse
-
#reformatted! ⇒ Object
Triger any formatting rules specified with the reformat directive.
-
#to_params ⇒ Object
Return all the attributes as a params hash.
Class Attribute Details
.format_fields ⇒ Object
Returns the value of attribute format_fields.
32 33 34 |
# File 'lib/formant.rb', line 32 def format_fields @format_fields end |
.parse_fields ⇒ Object
Returns the value of attribute parse_fields.
32 33 34 |
# File 'lib/formant.rb', line 32 def parse_fields @parse_fields end |
Class Method Details
.parse(field_name, options = {}) ⇒ Object
Directive to add a parse rule for the specified attribute. Parse rules let you apply any special parsing/transformation logic on the form’s attributes upon input. The parsing rules are applied automatically prior to validation.
Usage example:
class MyForm < FormObject
...
parse :appointment_time, as: :datetime
parse :phone, as: :phone_number
parse :price, as: :currency
parse :email, to: :strip_whitespace
...
end
The above example specifies that the appointment_time attribute should be parsed with the parse_datetime method (which converts the datetime string into an ActiveSupport::TimeWithZone object), and that the # phone attribute should be parsed with the parse_phone_number method (which normalizes the phone number into a standard format).
59 60 61 62 63 64 |
# File 'lib/formant.rb', line 59 def self.parse(field_name, ={}) self.parse_fields ||= [] parse_type = [:as] || [:to] raise "no parse type provided" if parse_type.blank? self.parse_fields << [ field_name, "parse_#{parse_type}", ] end |
.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, ={}) self.format_fields ||= [] self.format_fields << [ field_name, "format_#{options[:as]}", ] end |
Instance Method Details
#reformatted! ⇒ Object
Triger any formatting rules specified with the reformat directive. The attributes are reformatted and mutated in place.
Returns an instance of the form object.
101 102 103 104 105 106 107 |
# File 'lib/formant.rb', line 101 def reformatted! self.class.format_fields.each do |field_name, format_method, | formatted_value = send(format_method, get_field(field_name), ) set_field(field_name, formatted_value) end self end |
#to_params ⇒ Object
Return all the attributes as a params hash.
112 113 114 115 116 117 118 119 |
# File 'lib/formant.rb', line 112 def to_params attrs = Hash.new instance_variables.each do |ivar| name = ivar[1..-1] attrs[name.to_sym] = instance_variable_get(ivar) if respond_to? "#{name}=" end attrs end |