module ExpressTemplates module Components # # Create an html form for a model object. # # Example: # # ````ruby # 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: # # ````ruby # 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: # # ````ruby # 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 @form_options = args.select { |x| x.is_a?(Hash) } 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: # # ````ruby # form_for(:people) do |f| # f.phone_field :phone # end # ```` # # This will precompile as # # ````ruby # ... # phone_field_tag :phone, @people.phone, {} # ... # ```` # # Fields can also have custom classes via the `class` option: # # Example: # # ````ruby # f.url_field :website_url, class: 'url-string' # ```` # # Compiles to: # # ```` # # ```` # # You can also add in the basic options supported by the # phone_field_tag [here](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-phone_field_tag) # # This applies to all the other '_field_tags' listed # [here](http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html) # # # = Fields # ````ruby # f.text_field :name # #
# # # # # #
# # f.text_field :name, label: 'post title' # #
# # # # # #
# # f.text_field :name, class: 'string' # #
# # # # # #
# # f.text_field :name, wrapper_class: 'field input' # #
# # # # # #
# # ```` %w(email phone text password color date datetime datetime_local hidden number range search telephone time url week).each do |type| define_method("#{type}_field") do |name, options={}| @fields ||= [] @fields << Field.new(name, options, type.to_sym) end end # ==== Examples # f.select :gender, ['Male', 'Female'], selected: 'Male' # #
# # # # # #
# f.select :name, options_from_collection_for_select(@people, "id", "name") # #
# # # # # #
# def select(name, select_options, options = {}) @fields ||= [] @fields << Select.new(name, options.merge!(select_options: select_options)) end # ==== Examples # f.radio :card, [[1, 'One'], [2, 'Two']], :first, :last # #
# # # # # #
# def radio(name, collection, value_method, text_method, options = {}) @fields ||= [] @fields << Radio.new(name, options.merge!(collection: collection, value_method: value_method, text_method: text_method)) end # ==== Examples # f.checkbox :age, [[1, 'One'], [2, 'Two']], :first, :last # #
# # # # # #
# def checkbox(name, collection, value_method, text_method, options = {}) @fields ||= [] @fields << Checkbox.new(name, options.merge!(collection: collection, value_method: value_method, text_method: text_method)) end # ==== Examples # f.text_area :name # #
# # # #