Class: BootstrapForm::FormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Includes:
Helpers::Bootstrap
Defined in:
lib/bootstrap_form/form_builder.rb

Constant Summary collapse

FIELD_HELPERS =
%w{color_field date_field datetime_field datetime_local_field
email_field month_field number_field password_field phone_field
range_field search_field telephone_field text_area text_field time_field
url_field week_field}
DATE_SELECT_HELPERS =
%w{date_select time_select datetime_select}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Bootstrap

#alert_message, #error_summary, #errors_on, #input_group_class, #prepend_and_append_input, #primary, #static_class, #static_control, #submit

Constructor Details

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

Returns a new instance of FormBuilder.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bootstrap_form/form_builder.rb', line 18

def initialize(object_name, object, template, options)
  @layout = options[:layout]
  @label_col = options[:label_col] || default_label_col
  @control_col = options[:control_col] || default_control_col
  @label_errors = options[:label_errors] || false
  @inline_errors = if options[:inline_errors].nil?
    @label_errors != true
  else
    options[:inline_errors] != false
  end
  @acts_like_form_tag = options[:acts_like_form_tag]

  super
end

Instance Attribute Details

#acts_like_form_tagObject (readonly)

Returns the value of attribute acts_like_form_tag.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def acts_like_form_tag
  @acts_like_form_tag
end

#control_colObject (readonly)

Returns the value of attribute control_col.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def control_col
  @control_col
end

#has_errorObject (readonly)

Returns the value of attribute has_error.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def has_error
  @has_error
end

#inline_errorsObject (readonly)

Returns the value of attribute inline_errors.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def inline_errors
  @inline_errors
end

#label_colObject (readonly)

Returns the value of attribute label_col.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def label_col
  @label_col
end

#label_errorsObject (readonly)

Returns the value of attribute label_errors.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def label_errors
  @label_errors
end

#layoutObject (readonly)

Returns the value of attribute layout.



7
8
9
# File 'lib/bootstrap_form/form_builder.rb', line 7

def layout
  @layout
end

Instance Method Details

#check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/bootstrap_form/form_builder.rb', line 101

def check_box_with_bootstrap(name, options = {}, checked_value = "1", unchecked_value = "0", &block)
  options = options.symbolize_keys!
  check_box_options = options.except(:label, :label_class, :help, :inline)

  html = check_box_without_bootstrap(name, check_box_options, checked_value, unchecked_value)
  label_content = block_given? ? capture(&block) : options[:label]
  html.concat(" ").concat(label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize)

  label_name = name
  label_name = "#{name}_#{checked_value}" if options[:multiple]

  disabled_class = " disabled" if options[:disabled]
  label_class    = options[:label_class]

  if options[:inline]
    label_class = " #{label_class}" if label_class
    label(label_name, html, class: "checkbox-inline#{disabled_class}#{label_class}")
  else
    (:div, class: "checkbox#{disabled_class}") do
      label(label_name, html, class: label_class)
    end
  end
end

#check_boxes_collection(*args) ⇒ Object



162
163
164
165
# File 'lib/bootstrap_form/form_builder.rb', line 162

def check_boxes_collection(*args)
  warn "'BootstrapForm#check_boxes_collection' is deprecated, use 'BootstrapForm#collection_check_boxes' instead"
  collection_check_boxes(*args)
end

#collection_check_boxes(*args) ⇒ Object



148
149
150
151
152
153
154
# File 'lib/bootstrap_form/form_builder.rb', line 148

def collection_check_boxes(*args)
  html = inputs_collection(*args) do |name, value, options|
    options[:multiple] = true
    check_box(name, options, value, nil)
  end
  hidden_field(args.first,{value: "", multiple: true}).concat(html)
end

#collection_radio_buttons(*args) ⇒ Object



156
157
158
159
160
# File 'lib/bootstrap_form/form_builder.rb', line 156

def collection_radio_buttons(*args)
  inputs_collection(*args) do |name, value, options|
    radio_button(name, value, options)
  end
end

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



77
78
79
80
81
# File 'lib/bootstrap_form/form_builder.rb', line 77

def collection_select_with_bootstrap(method, collection, value_method, text_method, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    collection_select_without_bootstrap(method, collection, value_method, text_method, options, html_options)
  end
end

#fields_for_with_bootstrap(record_name, record_object = nil, fields_options = {}, &block) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/bootstrap_form/form_builder.rb', line 199

def fields_for_with_bootstrap(record_name, record_object = nil, fields_options = {}, &block)
  fields_options, record_object = record_object, nil if record_object.is_a?(Hash) && record_object.extractable_options?
  fields_options[:layout] ||= options[:layout]
  fields_options[:label_col] = fields_options[:label_col].present? ? "#{fields_options[:label_col]} #{label_class}" : options[:label_col]
  fields_options[:control_col] ||= options[:control_col]
  fields_options[:inline_errors] ||= options[:inline_errors]
  fields_options[:label_errors] ||= options[:label_errors]
  fields_for_without_bootstrap(record_name, record_object, fields_options, &block)
end

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



61
62
63
64
65
# File 'lib/bootstrap_form/form_builder.rb', line 61

def file_field_with_bootstrap(name, options = {})
  form_group_builder(name, options.reverse_merge(control_class: nil)) do
    file_field_without_bootstrap(name, options)
  end
end

#form_group(*args, &block) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/bootstrap_form/form_builder.rb', line 172

def form_group(*args, &block)
  options = args.extract_options!
  name = args.first

  options[:class] = ["form-group", options[:class]].compact.join(' ')
  options[:class] << " #{error_class}" if has_error?(name)
  options[:class] << " #{feedback_class}" if options[:icon]

  (:div, options.except(:id, :label, :help, :icon, :label_col, :control_col, :layout)) do
    label = generate_label(options[:id], name, options[:label], options[:label_col], options[:layout]) if options[:label]
    control = capture(&block).to_s
    control.concat(generate_help(name, options[:help]).to_s)
    control.concat(generate_icon(options[:icon])) if options[:icon]

    if get_group_layout(options[:layout]) == :horizontal
      control_class = (options[:control_col] || control_col.clone)
      unless options[:label]
        control_offset = offset_col(/([0-9]+)$/.match(options[:label_col] || @label_col))
        control_class.concat(" #{control_offset}")
      end
      control = (:div, control, class: control_class)
    end

    concat(label).concat(control)
  end
end

#grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {}) ⇒ Object



85
86
87
88
89
# File 'lib/bootstrap_form/form_builder.rb', line 85

def grouped_collection_select_with_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    grouped_collection_select_without_bootstrap(method, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
  end
end

#radio_button_with_bootstrap(name, value, *args) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/bootstrap_form/form_builder.rb', line 127

def radio_button_with_bootstrap(name, value, *args)
  options = args.extract_options!.symbolize_keys!
  args << options.except(:label, :label_class, :help, :inline)

  html = radio_button_without_bootstrap(name, value, *args) + " " + options[:label]

  disabled_class = " disabled" if options[:disabled]
  label_class    = options[:label_class]

  if options[:inline]
    label_class = " #{label_class}" if label_class
    label(name, html, class: "radio-inline#{disabled_class}#{label_class}", value: value)
  else
    (:div, class: "radio#{disabled_class}") do
      label(name, html, value: value, class: label_class)
    end
  end
end

#radio_buttons_collection(*args) ⇒ Object



167
168
169
170
# File 'lib/bootstrap_form/form_builder.rb', line 167

def radio_buttons_collection(*args)
  warn "'BootstrapForm#radio_buttons_collection' is deprecated, use 'BootstrapForm#collection_radio_buttons' instead"
  collection_radio_buttons(*args)
end

#select_with_bootstrap(method, choices, options = {}, html_options = {}) ⇒ Object



69
70
71
72
73
# File 'lib/bootstrap_form/form_builder.rb', line 69

def select_with_bootstrap(method, choices, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    select_without_bootstrap(method, choices, options, html_options)
  end
end

#time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {}) ⇒ Object



93
94
95
96
97
# File 'lib/bootstrap_form/form_builder.rb', line 93

def time_zone_select_with_bootstrap(method, priority_zones = nil, options = {}, html_options = {})
  form_group_builder(method, options, html_options) do
    time_zone_select_without_bootstrap(method, priority_zones, options, html_options)
  end
end