Class: RailsBootstrapForm::BootstrapFormOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_bootstrap_form/bootstrap_form_options.rb

Overview

Container for bootstrap specific form builder options. It controls options that define form layout, grid sizing, and few other configurable options. They are passed-in into form builder helper and field helpers via ‘:bootstrap` option.

For example:

bootstrap_form_with model: @user, bootstrap: {layout: :inline} do |f|
   f.text_field :email, bootstrap: {label: {text: "Your email"}}
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BootstrapFormOptions

Returns a new instance of BootstrapFormOptions.



130
131
132
133
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 130

def initialize(options = {})
  set_defaults
  set_bootstrap_form_options(options)
end

Instance Attribute Details

#additional_field_classObject

An additional CSS class that will be added along with the existing css classes of field helpers. The default value is nil.



33
34
35
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 33

def additional_field_class
  @additional_field_class
end

#additional_input_group_classObject

An additional CSS class that will be added to existing input group wrapper css classes. The default value is ‘nil`.



84
85
86
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 84

def additional_input_group_class
  @additional_input_group_class
end

#additional_label_classObject

An additional CSS class that will be added along with the existing label css classes. The default value is ‘nil`.



64
65
66
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 64

def additional_label_class
  @additional_label_class
end

#appendObject

Raw or HTML content to be appended to the field. The default value is ‘nil`.



80
81
82
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 80

def append
  @append
end

#disabledObject

Controls whether to render default Rails form builder element. The default value is ‘false`.



21
22
23
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 21

def disabled
  @disabled
end

#field_classObject

A CSS class that will be applied to all form fields. The default value is ‘nil`.



29
30
31
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 29

def field_class
  @field_class
end

#field_col_wrapper_classObject

A CSS class for field column when layout is horizontal. The default value is ‘col-sm-10`.



123
124
125
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 123

def field_col_wrapper_class
  @field_col_wrapper_class
end

#floatingObject

An option to control whether the field should have a floating label. The default value is false.



88
89
90
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 88

def floating
  @floating
end

#help_textObject

Describes help text for the HTML field. Help text is automatically read from translation file. If you want to customize it, you can pass string. You can also set it false if you do not want help text displayed. The default value is nil.



39
40
41
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 39

def help_text
  @help_text
end

#hide_classObject

The CSS class that will be used when the label is only accessible to a screen readers. The default value is ‘visually-hidden`.



55
56
57
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 55

def hide_class
  @hide_class
end

#hide_labelObject

An option to control whether the label is only visible to a screen readers. The default value is ‘false`.



51
52
53
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 51

def hide_label
  @hide_label
end

#inlineObject

An option to group checkboxes and radio buttons on the same horizontal row. The default value is ‘false`.

Example:

form.collection_radio_buttons :choices, ["yes", "no"], :to_s, :to_s, bootstrap: {inline: true}


111
112
113
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 111

def inline
  @inline
end

#label_classObject

A CSS class that will be applied to all label tags when layout is vertical. The default value is ‘form-label`.



60
61
62
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 60

def label_class
  @label_class
end

#label_col_classObject

A CSS class that will be applied to all labels when layout is horizontal. The default value is ‘col-form-label`.



115
116
117
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 115

def label_col_class
  @label_col_class
end

#label_col_wrapper_classObject

A CSS class for label column when layout is horizontal. The default value is ‘col-sm-2`.



119
120
121
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 119

def label_col_wrapper_class
  @label_col_wrapper_class
end

#label_textObject

An option to customize automatically generated label text. The default value is ‘nil`.



43
44
45
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 43

def label_text
  @label_text
end

#layoutObject

Controls layout of form and field helpers. It can be “vertical, ”horizontal“, or ”inline“. The default value is ‘vertical`.



25
26
27
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 25

def layout
  @layout
end

#prependObject

Input group specific options. Input groups allow prepending and appending arbitrary html or text to the field.

Example:

form.text_field :dollars, bootstrap: {input_group: {prepend: "$", append: ".00"}}
form.text_field :search, bootstrap: {input_group: {append: button_tag("Go", type: :submit, class: "btn btn-secondary")}}

Raw or HTML content to be prepended to the field. The default value is ‘nil`.



76
77
78
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 76

def prepend
  @prepend
end

#render_as_buttonObject

An option to render submit button using ‘<button type=“submit”>` instead of `<input type=“submit”>`. The default value is `false`.



128
129
130
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 128

def render_as_button
  @render_as_button
end

#sizeObject

An option to control the size of input groups, buttons, labels, and fields. The valid values are ‘sm` and `lg`. The default value is `nil`.



104
105
106
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 104

def size
  @size
end

#skip_labelObject

An option to control whether the label is to be displayed or not. The default value is ‘false`.



47
48
49
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 47

def skip_label
  @skip_label
end

#static_field_classObject

A CSS class that will be applied to all static fields. The default value is ‘form-control-plaintext`.



92
93
94
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 92

def static_field_class
  @static_field_class
end

#switchObject

An option to control whether the check box should look like Bootstrap switches. The default value is ‘false`.



96
97
98
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 96

def switch
  @switch
end

#wrapperObject

An option to control the HTML attributes and options that will be added to the field wrapper. The default value is ‘{}`.



100
101
102
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 100

def wrapper
  @wrapper
end

Instance Method Details

#disable_floating_labels!Object



172
173
174
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 172

def disable_floating_labels!
  self.floating = false
end

#layout_horizontal?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 135

def layout_horizontal?
  @layout.to_s == "horizontal"
end

#layout_inline?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 139

def layout_inline?
  @layout.to_s == "inline"
end

#layout_vertical?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 143

def layout_vertical?
  @layout.to_s == "vertical"
end

#scoped(options = {}) ⇒ Object

This will return a copy of ‘BootstrapFormOptions` object with new options set that don’t affect original object. This way we can have options specific to a given form field. For example, we can change grid just for one field:

bootstrap_form_with model: @user do |f|
  f.text_field :email, bootstrap: {label_col_wrapper_class: "col-md-6", field_col_wrapper_class: "col-md-6"}
  f.password_field :password
end


156
157
158
159
160
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 156

def scoped(options = {})
  scope = clone
  scope.set_bootstrap_form_options(options)
  scope
end

#set_bootstrap_form_options(options) ⇒ Object



162
163
164
165
166
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 162

def set_bootstrap_form_options(options)
  options.is_a?(Hash) && options.each do |key, value|
    public_send("#{key}=", value)
  end
end

#set_field_class!(css_class) ⇒ Object



176
177
178
# File 'lib/rails_bootstrap_form/bootstrap_form_options.rb', line 176

def set_field_class!(css_class)
  self.field_class ||= css_class
end