lib/formalizer/formalizer.rb
# Generates an HTML form with fields to fill.
# * +locale+ - relevant with enum/multiple type fields, defaults to default app locale
# * +form_action+ - generated form can have an action. Defaults to ''
# * +tag+ - narrows down thefields list to those under the specified tag. Defaults to all fields
Generates an HTML form with fields to fill.
+locale+ - relevant with enum/multiple type fields, defaults to default app locale
+form_action+ - generated form can have an action. Defaults to ''
+tag+ - narrows down thefields list to those under the specified tag. Defaults to all fields
Generates an HTML form with fields to fill . + locale + - relevant with enum / multiple type fields defaults to default app locale + form_action + - generated form can have an action . Defaults to ' ' + tag + - narrows down thefields list to those under the specified tag . Defaults to all fields
def generate_fields_form locale = I18n.locale, form_action = '', tag = nil
    fields = fields_by_tag(tag)
    action = form_action.empty? ? '' : " action=\"#{form_action}\""
    html = Nokogiri::HTML("<body><form#{action} method='POST'><fieldset></fieldset></form></body>")
    fields.each do |key, field|
      wrapper = Nokogiri::XML::Node.new('div', html)
      label = Nokogiri::XML::Node.new('label', wrapper)
      label['for'] = field.id
      label.content = "#{field.name(locale)}:"
      rendered_field = field.render_html(wrapper, locale)
      label.parent = wrapper
      rendered_field.parent = wrapper
      wrapper.parent = html.at_css('fieldset')
    end
    submit_container = Nokogiri::XML::Node.new('div', html)
    submit_types = %w(HTML PDF)
    %w(reset submit submit).each_with_index do |control_type, index|
      reset_submit = Nokogiri::XML::Node.new('input', submit_container)
      reset_submit['type'] = control_type
      if control_type == 'submit'
        reset_submit['name'] = "export_#{submit_types[index - 1]}"
        reset_submit['value'] = "Export as #{submit_types[index - 1]}"
      end
      reset_submit.parent = submit_container
    end
    submit_container.parent = html.at_css('fieldset')
    html.to_s
  end
def generate_fields_form locale = I18n . locale , form_action = '' , tag = nil fields = fields_by_tag ( tag ) action = form_action . empty? ? '' : " action=\"#{form_action}\"" html = Nokogiri :: HTML ( "<body><form#{action} method='POST'><fieldset></fieldset></form></body>" ) fields . each do | key , field | wrapper = Nokogiri :: XML :: Node . new ( 'div' , html ) label = Nokogiri :: XML :: Node . new ( 'label' , wrapper ) label [ 'for' ] = field . id label . content = "#{field.name(locale)}:" rendered_field = field . render_html ( wrapper , locale ) label . parent = wrapper rendered_field . parent = wrapper wrapper . parent = html . at_css ( 'fieldset' ) end submit_container = Nokogiri :: XML :: Node . new ( 'div' , html ) submit_types = %w( HTML PDF ) %w( reset submit submit ) . each_with_index do | control_type , index | reset_submit = Nokogiri :: XML :: Node . new ( 'input' , submit_container ) reset_submit [ 'type' ] = control_type if control_type == 'submit' reset_submit [ 'name' ] = "export_#{submit_types[index - 1]}" reset_submit [ 'value' ] = "Export as #{submit_types[index - 1]}" end reset_submit . parent = submit_container end submit_container . parent = html . at_css ( 'fieldset' ) html . to_s end
Generates an HTML form with fields to fill.
Generates an HTML form with fields to fill .
locale
<not_specific>
form_action
<not_specific>
tag
<not_specific>