Module: Formize::Helpers::FormHelper

Defined in:
lib/formize/helpers/form_helper.rb

Constant Summary collapse

FIELD_ERROR_PROC =

Code picked from Justin French’s formtastic gem

proc do |html_tag, instance_tag| # :nodoc:
  html_tag
end

Instance Method Summary collapse

Instance Method Details

#date_field(object_name, method, options = {}) ⇒ Object

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

Examples:

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"/>

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :format (Symbol)

    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`

  • :size (Integer)

    Set the size of the visible text field

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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 = {}) ⇒ Object

Returns a text field with a default placeholder for timestamp



104
105
106
107
108
109
# 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 = {}) ⇒ Object

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.



46
47
48
49
50
51
52
53
# 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 << (: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 = {}) ⇒ Object

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…



57
58
59
60
61
62
63
64
65
66
67
68
69
# 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 (:span, html.html_safe, html_options)
end

#with_custom_field_error_proc(&block) ⇒ Object

:nodoc:



10
11
12
13
14
15
16
# File 'lib/formize/helpers/form_helper.rb', line 10

def with_custom_field_error_proc(&block) # :nodoc:
  default_field_error_proc = ::ActionView::Base.field_error_proc
  ::ActionView::Base.field_error_proc = FIELD_ERROR_PROC
  yield
ensure
  ::ActionView::Base.field_error_proc = default_field_error_proc
end