module ExpressTemplates
module Components
#
# Create an html form for a model object.
#
# Example:
#
# ````
# form_for(:people) do |f|
# f.text_field :name
# f.email_field :email
# f.phone_field :phone, wrapper_class: 'field phone'
# f.submit 'Save'
# end
# ````
#
# This assumes that @people variable will exist in the
# view and that it will be a collection whose members respond to :name, :email etc.
#
# This will result in markup like the following:
#
# ````
#
# ````
#
# As seen above, each field is accompanied by a label and is wrapped by a div.
# The div can be further styled by adding css classes to it via the `wrapper_class` option.
#
# Example:
#
# ````
# form_for(:posts) do
# f.text_field :title, wrapped_class: 'string optional'
# end
#
# Will result to generating HTML like so:
#
# ````
# ...
#
#
#
#
# ...
# ````
#
# In addition to this, label text can also be customized using the `label` option:
#
# ````
# f.email_field :email_address, label: 'Your Email'
# ````
#
# Compiles to;
# ````
#
#
# ````
class FormFor < Base
include Capabilities::Configurable
include Capabilities::Building
def initialize(*args)
# TODO: need a better way to select the form options
if @form_options = args.find { |x| x.is_a?(Hash) }
@_method = @form_options.delete :method
end
super(*args)
_process_args!(args) # from Configurable
yield(self) if block_given?
end
attr :fields
# Express Templates uses Rails' form helpers to generate the fields with labels
#
# Example:
#
# ````
# form_for(:people) do |f|
# f.phone_field :phone
# end
# ````
#
# This will precompile as
#
# ````
# ...
# phone_field_tag :phone, @people.phone, {}
# ...
# ````
#
# You can also add html options to the form (add classes, id, etc)
#
# ````
# form_for(:people, html_options: {class: 'edit_form', id: 'people_form'}) do |f|
# f.phone_field :phone
# end
# ````
#
# #