class BootstrapForm::FormBuilder

Attributes

acts_like_form_tag[R]
control_col[R]
has_error[R]
inline_errors[R]
label_col[R]
label_errors[R]
layout[R]

Public Class Methods

new(object_name, object, template, options) click to toggle source

rubocop:disable Metrics/AbcSize

Calls superclass method
# File lib/bootstrap_form/form_builder.rb, line 49
def initialize(object_name, object, template, options)
  @layout = options[:layout] || default_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]
  add_default_form_attributes_and_form_inline options
  super
end

Public Instance Methods

add_default_form_attributes_and_form_inline(options) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/bootstrap_form/form_builder.rb, line 66
def add_default_form_attributes_and_form_inline(options)
  options[:html] ||= {}
  options[:html].reverse_merge!(BootstrapForm.config.default_form_attributes)

  return unless options[:layout] == :inline

  options[:html][:class] = [options[:html][:class], "form-inline"].compact.join(" ")
end
fields_for_with_bootstrap(record_name, record_object=nil, fields_options={}, &block) click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 75
def fields_for_with_bootstrap(record_name, record_object=nil, fields_options={}, &block)
  fields_options = fields_for_options(record_object, fields_options)
  record_object.is_a?(Hash) && record_object.extractable_options? &&
    record_object = nil
  fields_for_without_bootstrap(record_name, record_object, fields_options, &block)
end

Private Instance Methods

control_class() click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 122
def control_class
  "form-control"
end
control_specific_class(method) click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 130
def control_specific_class(method)
  "rails-bootstrap-forms-#{method.to_s.tr('_', '-')}"
end
default_control_col() click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 114
def default_control_col
  "col-sm-10"
end
default_label_col() click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 106
def default_label_col
  "col-sm-2"
end
default_layout() click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 101
def default_layout
  # :default, :horizontal or :inline
  :default
end
feedback_class() click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 126
def feedback_class
  "has-feedback"
end
fields_for_options(record_object, fields_options) click to toggle source

the Rails `fields` method passes its options to the builder, so there is no need to write a `bootstrap_form` helper for the `fields` method.

# File lib/bootstrap_form/form_builder.rb, line 90
def fields_for_options(record_object, fields_options)
  field_options = fields_options
  record_object.is_a?(Hash) && record_object.extractable_options? &&
    field_options = record_object
  %i[layout control_col inline_errors label_errors].each do |option|
    field_options[option] ||= options[option]
  end
  field_options[:label_col] = field_options[:label_col].present? ? (field_options[:label_col]).to_s : options[:label_col]
  field_options
end
hide_class() click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 118
def hide_class
  "sr-only" # still accessible for screen readers
end
offset_col(label_col) click to toggle source
# File lib/bootstrap_form/form_builder.rb, line 110
def offset_col(label_col)
  label_col.gsub(/\bcol-(\w+)-(\d)\b/, 'offset-\1-\2')
end