Class: Binco::BootstrapFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/helpers/binco/bootstrap_form_builder.rb

Constant Summary collapse

CHECK_BOX_GROUP_CLASS =
'form-check'
CHECK_BOX_LABEL_CLASS =
'form-check-label'
FORM_ELEMENT_CLASS =
'form-control'
CHECK_BOX_INPUT_CLASS =
'form-check-input'
ERROR_PROC =
Proc.new do |html_tag, instance|
  if instance.respond_to?(:error_message) && instance.class.to_s != 'ActionView::Helpers::Tags::Label'
    error_messages = instance.error_message.collect{ |error| "<div class=\"invalid-feedback\">#{error}</div>" }.join

    "#{html_tag.gsub(/class="/, 'class="is-invalid ')} #{error_messages}".html_safe
  else
    html_tag
  end
end

Instance Method Summary collapse

Constructor Details

#initialize(object_name, object, template, options) ⇒ BootstrapFormBuilder

Returns a new instance of BootstrapFormBuilder.



23
24
25
26
27
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 23

def initialize(object_name, object, template, options)
  ActionView::Base::field_error_proc = ERROR_PROC

  super(object_name, object, template, options)
end

Instance Method Details

#addon(icon, options = {}) ⇒ Object



163
164
165
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 163

def addon(icon, options = {})
  input_group_append(icon, options)
end

#check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



110
111
112
113
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 110

def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  options = add_class_to_options(CHECK_BOX_INPUT_CLASS, options)
  super method, options, checked_value, unchecked_value
end

#check_box_originalObject



4
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 4

alias_method :check_box_original, :check_box

#check_label(method, options = {}, &block) ⇒ Object Also known as: check_box_label



152
153
154
155
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 152

def check_label(method, options = {}, &block)
  options = add_class_to_options(CHECK_BOX_LABEL_CLASS, options)
  @template.label(@object_name, method, nil, objectify_options(options), &block)
end

#collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 50

def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  if block_given?
    super(method, collection, value_method, text_method, options, html_options, &block)
  else
    super method, collection, value_method, text_method, options, html_options do |b|
      group_tag class: CHECK_BOX_GROUP_CLASS do
        b.label class: CHECK_BOX_LABEL_CLASS do
          b.check_box(class: CHECK_BOX_INPUT_CLASS) + " " + b.text
        end
      end
    end
  end
end

#collection_check_boxes2(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object

Since select2 support multiple choices (checkboxes)



65
66
67
68
69
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 65

def collection_check_boxes2(method, collection, value_method, text_method, options = {}, html_options = {})
  html_options ||= {}
  html_options[:multiple] = 'multiple'
  collection_select2 method, collection, value_method, text_method, options, html_options
end

#collection_select(method, collection, value_method, text_method, options = {}, html_options = {}) ⇒ Object



71
72
73
74
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 71

def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
  html_options = add_class_to_options('custom-select', html_options)
  super method, collection, value_method, text_method, options, html_options
end

#collection_select2(method, collection, value_method, text_method, options = {}, html_options = {}, &block) ⇒ Object



76
77
78
79
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 76

def collection_select2(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
  html_options = add_class_to_options('select2-rails', html_options)
  collection_select_original(method, collection, value_method, text_method, options, html_options)
end

#collection_select_originalObject



3
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 3

alias_method :collection_select_original, :collection_select

#custom_check_box(method, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 115

def custom_check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
  options = add_class_to_options('custom-control-input', options)
  base_proc = ActionView::Base::field_error_proc
  ActionView::Base::field_error_proc = Proc.new { |html_tag, instance| html_tag }
  html = check_box_original(method, options, checked_value, unchecked_value)
  ActionView::Base::field_error_proc = base_proc

  return html
end

#datepicker(method, options = {}) ⇒ Object



96
97
98
99
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 96

def datepicker(method, options = {})
  options = add_data_to_options({ provide: 'datepicker' }, options)
  text_field(method, options)
end

#email_field(name, options = {}) ⇒ Object



81
82
83
84
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 81

def email_field(name, options = {})
  options = add_class_to_options(FORM_ELEMENT_CLASS, options)
  super name, options
end

#file_field(method, options = {}) ⇒ Object



125
126
127
128
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 125

def file_field(method, options = {})
  options = add_class_to_options('form-control-file', options)
  super method, options
end

#form_check(options = {}, &block) ⇒ Object Also known as: check_box_group



146
147
148
149
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 146

def form_check(options = {}, &block)
  options = add_class_to_options(CHECK_BOX_GROUP_CLASS, options)
  group_tag options, &block
end

#form_group(options = {}, &block) ⇒ Object



136
137
138
139
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 136

def form_group(options = {}, &block)
  options = add_class_to_options('form-group', options)
  group_tag options, &block
end

#input_group(options = {}, &block) ⇒ Object



158
159
160
161
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 158

def input_group(options = {}, &block)
  options = add_class_to_options('input-group', options)
  group_tag options, &block
end

#input_group_append(content, options = {}) ⇒ Object



172
173
174
175
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 172

def input_group_append(content, options = {})
  options = add_class_to_options('input-group-append', options)
  @template.(:div, @template.(:span, content, { class: 'input-group-text' }), options)
end

#input_group_prepend(content, options = {}) ⇒ Object



167
168
169
170
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 167

def input_group_prepend(content, options = {})
  options = add_class_to_options('input-group-prepend', options)
  @template.(:div, @template.(:span, content, { class: 'input-group-text' }), options)
end

#number_field(name, options = {}) ⇒ Object



86
87
88
89
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 86

def number_field(name, options = {})
  options = add_class_to_options(FORM_ELEMENT_CLASS, options)
  super name, options
end

#password_field(name, options = {}) ⇒ Object



91
92
93
94
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 91

def password_field(name, options = {})
  options = add_class_to_options(FORM_ELEMENT_CLASS, options)
  super name, options
end

#radio_button(method, tag_value, options = {}) ⇒ Object



106
107
108
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 106

def radio_button(method, tag_value, options = {})
  super method, tag_value, options
end

#radio_group(options = {}, &block) ⇒ Object



141
142
143
144
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 141

def radio_group(options = {}, &block)
  options = add_class_to_options('radio', options)
  group_tag options, &block
end

#select(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



40
41
42
43
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 40

def select(method, choices = nil, options = {}, html_options = {}, &block)
  html_options = add_class_to_options('custom-select', html_options)
  super method, choices, options, html_options, &block
end

#select2(method, choices = nil, options = {}, html_options = {}, &block) ⇒ Object



45
46
47
48
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 45

def select2(method, choices = nil, options = {}, html_options = {}, &block)
  html_options = add_class_to_options('select2-rails', html_options)
  select_original method, choices, options, html_options, &block
end

#select_originalObject



5
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 5

alias_method :select_original, :select

#submit(value = nil, options = {}) ⇒ Object



130
131
132
133
134
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 130

def submit(value = nil, options = {})
  options = add_class_to_options('btn btn-success', options)
  add_data_to_options({ disable_with: 'Enviando' }, options)
  super value, options
end

#submit_originalObject



6
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 6

alias_method :submit_original, :submit

#telephone_field(name, options = {}) ⇒ Object Also known as: phone_field



34
35
36
37
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 34

def telephone_field(name, options = {})
  options = add_class_to_options(FORM_ELEMENT_CLASS, options)
  super name, options
end

#text_area(method, options = {}) ⇒ Object



101
102
103
104
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 101

def text_area(method, options = {})
  options = add_class_to_options(FORM_ELEMENT_CLASS, options)
  super(method, options)
end

#text_field(name, options = {}) ⇒ Object



29
30
31
32
# File 'app/helpers/binco/bootstrap_form_builder.rb', line 29

def text_field(name, options = {})
  options = add_class_to_options(FORM_ELEMENT_CLASS, options)
  super name, options
end