Class: Manageable::Helpers::FormBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Manageable::Helpers::FormBuilder
- Defined in:
- lib/manageable/helpers/form_builder.rb
Overview
This custom FormBuilder automatically adds labels to form fields.
Constant Summary collapse
- HTML_CLASSES =
{ :text_field => "text_field", :password_field => "text_field", :telephone_field => "text_field", :url_field => "text_field", :email_field => "text_field", :number_field => "text_field", :range_field => "text_field", :file_field => "text_field", :text_area => "text_area" }
- @@field_with_errors_proc =
Proc.new do |method, label_tag, object, template| if object.respond_to?(:errors) && object.errors.respond_to?(:[]) && object.errors[method].present? template.content_tag(:div, :class => "fieldWithErrors") do label_tag + " ".html_safe + template.content_tag(:span, object.errors[method].first, :class => "error") end else label_tag end end
Instance Method Summary collapse
-
#button(value = nil, options = {}) ⇒ Object
Generates an activo compliant submit button.
- #check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
- #field_label(method, text, options = {}) ⇒ Object
- #group(options = {}) ⇒ Object
- #radio_button(method, tag_value, options = {}) ⇒ Object
- #select(method, choices, options = {}, html_options = {}) ⇒ Object
Instance Method Details
#button(value = nil, options = {}) ⇒ Object
Generates an activo compliant submit button
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/manageable/helpers/form_builder.rb', line 18 def (value=nil, ={}) value, = nil, value if value.is_a?(Hash) value ||= I18n.t("manageable.save") [:class] = [[:class], "button"].compact.join(" ") image = @template.image_tag(.delete(:image) || "/assets/manageable/icons/tick.png", :alt => value) @template.() do [image, value].compact.join(" ").html_safe end end |
#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/manageable/helpers/form_builder.rb', line 85 def check_box(method, = {}, checked_value = "1", unchecked_value = "0") [:class] = [[:class], "checkbox"].compact.join(" ") label_tag = label(method, .delete(:label), :class => "checkbox") label_tag = @@field_with_errors_proc.call(method, label_tag, @object, @template) @template.content_tag(:div) do (super(method, , checked_value, unchecked_value) + label_tag).html_safe end end |
#field_label(method, text, options = {}) ⇒ Object
114 115 116 117 118 |
# File 'lib/manageable/helpers/form_builder.rb', line 114 def field_label(method, text, = {}) text << t("active_model.required") if [:required] && text.present? css_class = ["label", .delete(:class)] label(method, text, :class => css_class.compact.join(" ")) end |
#group(options = {}) ⇒ Object
105 106 107 108 109 110 111 112 |
# File 'lib/manageable/helpers/form_builder.rb', line 105 def group( = {}) css_class = ["group", .delete(:class)] css_class << "required" if [:required] @template.content_tag(:div, :class => css_class.compact.join(" ")) do yield end end |
#radio_button(method, tag_value, options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/manageable/helpers/form_builder.rb', line 95 def (method, tag_value, = {}) [:class] = [[:class], "radio"].compact.join(" ") label_tag = label(method, .delete(:label) || tag_value, :class => "radio", :value => tag_value) label_tag = @@field_with_errors_proc.call(method, label_tag, @object, @template) @template.content_tag(:div) do (super(method, tag_value, ) + label_tag).html_safe end end |
#select(method, choices, options = {}, html_options = {}) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/manageable/helpers/form_builder.rb', line 68 def select(method, choices, = {}, = {}) unless [:label] == false description_tag = @template.content_tag(:span, .delete(:description), :class => "description") if [:description].present? label_tag = field_label(method, .delete(:label), (:label_class, )) field_tag = super(method, choices, , ) # Applies fieldWithErrors label_tag = @@field_with_errors_proc.call(method, label_tag, @object, @template) group((:group_class, )) do (label_tag + field_tag + description_tag).html_safe end else super(method, choices, , ) end end |