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
-
#date_field(object_name, method, options = {}) ⇒ Object
Returns a text field for selecting a Date with a hidden field containing the well formatted date.
-
#datetime_field(object_name, method, options = {}) ⇒ Object
Returns a text field with a default placeholder for timestamp.
-
#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 byobject_name
). -
#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… -
#with_custom_field_error_proc(&block) ⇒ Object
:nodoc:.
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
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, = {}) object = instance_variable_get("@#{object_name}") format = [: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)} = {} .each{|k,v| [k] = v if k.to_s.match(/^data\-/)} html = "" html << hidden_field(object_name, method, ) html << tag(:input, :type => :text, "data-datepicker" => "#{object_name}_#{method}", "data-date-format" => format, :value => localized_value, "data-locale" => ::I18n.locale, :size => .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, = {}) default = {} default[:placeholder] = I18n.translate!('time.placeholder') rescue nil default[:size] ||= 24 return text_field(object_name, method, default.merge()) 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 (object_name, method, choices, = {}, = {}) html = "".html_safe [:class] ||= :rad for choice in choices html << content_tag(:span, (object_name, method, choice[1]) + ' '.html_safe + label(object_name, method, choice[0], :value => choice[1]), ) 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, = {}, ={}, = {}) object = instance_variable_get("@#{object_name}") label = [: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, ) 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 => .delete(:size)||32) return content_tag(:span, html.html_safe, ) 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 |