Class: Manageable::Helpers::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
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.(:div, :class => "fieldWithErrors") do
      label_tag + "&nbsp".html_safe + template.(:span, object.errors[method].first, :class => "error")
    end
  else
    label_tag
  end
end

Instance Method Summary collapse

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 button(value=nil, options={})
  value, options = nil, value if value.is_a?(Hash)

  value         ||= I18n.t("manageable.save")
  options[:class] = [options[:class], "button"].compact.join(" ")
  image           = @template.image_tag(options.delete(:image) || "/assets/manageable/icons/tick.png", :alt => value)

  @template.button_tag(options) 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, options = {}, checked_value = "1", unchecked_value = "0")
  options[:class] = [options[:class], "checkbox"].compact.join(" ")
  label_tag = label(method, options.delete(:label), :class => "checkbox")
  label_tag = @@field_with_errors_proc.call(method, label_tag, @object, @template)

  @template.(:div) do
    (super(method, options, 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, options = {})
  text << t("active_model.required") if options[:required] && text.present?
  css_class = ["label", options.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(options = {})
  css_class = ["group", options.delete(:class)]
  css_class << "required" if options[:required]

  @template.(: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 radio_button(method, tag_value, options = {})
  options[:class] = [options[:class], "radio"].compact.join(" ")
  label_tag = label(method, options.delete(:label) || tag_value, :class => "radio", :value => tag_value)
  label_tag = @@field_with_errors_proc.call(method, label_tag, @object, @template)

  @template.(:div) do
    (super(method, tag_value, options) + 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, options = {}, html_options = {})
  unless options[:label] == false
    description_tag = @template.(:span, options.delete(:description), :class => "description") if options[:description].present?
    label_tag       = field_label(method, options.delete(:label), extract_options(:label_class, options))
    field_tag       = super(method, choices, options, html_options)

    # Applies fieldWithErrors
    label_tag = @@field_with_errors_proc.call(method, label_tag, @object, @template)

    group(extract_options(:group_class, options)) do
      (label_tag + field_tag + description_tag).html_safe
    end
  else
    super(method, choices, options, html_options)
  end
end