module Formize::Helpers::FormHelper

Public Instance Methods

date_field(object_name, method, options = {}) click to toggle source

Returns a text field for selecting a Date with a hidden field containing the well formatted date

@example Set a date field

<%= date_field :post, :published_on -%>
# => <input type="hidden" id="post_published_on" name="post[published_on]" value="#{@post.published_on}"/>
#    <input type="text" data-datepicker="post_published_on" data-date_format="<jQuery_format>" value="<formatted_date>" data-locale="#{I18n.locale}" size="10"/>

@option options [Symbol] :format

Select date format used in visible text field. The format must be defined in locale files. To maintain compatibility with jQuery only few tokens can be used to compose the format: `%d`, `%j`, `%a`, `%A`, `%m`, `%b`, `%B`, `%y` and `%Y`

@option options [Integer] :size

Set the size of the visible text field
# File lib/formize/helpers/form_helper.rb, line 85
def date_field(object_name, method, options = {})
  object = instance_variable_get("@#{object_name}")
  format = options[:format]||:default
  raise ArgumentError.new("Option :format must be a Symbol referencing a translation 'date.formats.<format>'")unless format.is_a?(Symbol)
  if localized_value = object.send(method)
    localized_value = I18n.localize(localized_value, :format => format)
  end
  format = I18n.translate('date.formats.'+format.to_s) 
  Formize::DATE_FORMAT_TOKENS.each{|js, rb| format.gsub!(rb, js)}
  key_options = {}
  options.each{|k,v| key_options[k] = v if k.to_s.match(/^data\-/)}
  html  = ""
  html << hidden_field(object_name, method, key_options)
  html << tag(:input, :type => :text, "data-datepicker" => "#{object_name}_#{method}", "data-date-format" => format, :value => localized_value, "data-locale" => ::I18n.locale, :size => options.delete(:size)||10)
  return html.html_safe
end
datetime_field(object_name, method, options = {}) click to toggle source

Returns a text field with a default placeholder for timestamp

# File lib/formize/helpers/form_helper.rb, line 104
def datetime_field(object_name, method, options = {})
  default = {}
  default[:placeholder] = I18n.translate!('time.placeholder') rescue nil
  default[:size] ||= 24
  return text_field(object_name, method, default.merge(options))
end
radio_buttons(object_name, method, choices, options = {}, html_options = {}) click to toggle source

Returns a list of radio buttons for specified attribute (identified by method) on an object assigned to the template (identified by object_name). It works like select.

# File lib/formize/helpers/form_helper.rb, line 46
def radio_buttons(object_name, method, choices, options = {}, html_options = {})
  html = "".html_safe
  html_options[:class] ||= :rad
  for choice in choices
    html << content_tag(:span, radio_button(object_name, method, choice[1]) + '&nbsp;'.html_safe + label(object_name, method, choice[0], :value => choice[1]), html_options)
  end
  return html
end
unroll(object_name, method, choices, options = {}, input_options={}, html_options = {}) click to toggle source

Returns a text field which has the same behavior of select but with a search action which permits to find easily in very long lists…

# File lib/formize/helpers/form_helper.rb, line 57
def unroll(object_name, method, choices, options = {}, input_options={}, html_options = {})
  object = instance_variable_get("@#{object_name}")
  label = options[:label]
  if label.is_a?(String) or label.is_a?(Symbol)
    label = Proc.new{|x| x.send(label)}
  elsif !label.is_a?(Proc)
    label = Proc.new{|x| x.inspect}
  end
  html  = ""
  html << hidden_field(object_name, method, input_options)
  html << tag(:input, :type => :text, "data-unroll" => url_for(choices.merge(:format => :json)), "data-value-container" => "#{object_name}_#{method}", :value => label.call(object.send(method.to_s.gsub(/_id$/, ''))), :size => html_options.delete(:size)||32)
  return content_tag(:span, html.html_safe, html_options)
end