Class: BootstrapForm::BootstrapOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrap_form/bootstrap_options.rb

Overview

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

bootstrap_form_with scope: :login, url: "/login", bootstrap: {layout: :inline} do |f|
   f.text_field :username, bootstrap: {label: {text: "Your username"}}
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BootstrapOptions

Returns a new instance of BootstrapOptions.



67
68
69
70
# File 'lib/bootstrap_form/bootstrap_options.rb', line 67

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

Instance Attribute Details

#appendObject (readonly)

Returns the value of attribute append.



47
48
49
# File 'lib/bootstrap_form/bootstrap_options.rb', line 47

def append
  @append
end

#check_inlineObject (readonly)

Options to render checkboxes and radio buttons inline. Default is false. Example:

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


59
60
61
# File 'lib/bootstrap_form/bootstrap_options.rb', line 59

def check_inline
  @check_inline
end

#control_col_classObject (readonly)

CSS class for control column when using horizontal form. Default: “col-sm-10”



19
20
21
# File 'lib/bootstrap_form/bootstrap_options.rb', line 19

def control_col_class
  @control_col_class
end

#custom_controlObject (readonly)

Enables special input styling for file_field, radio and checkboxes. Example:

form.file_file :photo, bootstrap: {custom_control: true}


65
66
67
# File 'lib/bootstrap_form/bootstrap_options.rb', line 65

def custom_control
  @custom_control
end

#helpObject (readonly)

Help text that goes under the form field. Example usage:

form.password_field :password, bootstrap: {help: "Password should be more than 8 characters in length"}


53
54
55
# File 'lib/bootstrap_form/bootstrap_options.rb', line 53

def help
  @help
end

#inline_margin_classObject (readonly)

CSS class used to space out form groups for inline forms. Default: “mr-sm-2”



25
26
27
# File 'lib/bootstrap_form/bootstrap_options.rb', line 25

def inline_margin_class
  @inline_margin_class
end

#labelObject (readonly)

Label specific options. Default is and empty hash. Options are as follows:

text:   "Label Text"  - override automatically generated label text
hide:   true          - label only visible to screen readers
class:  "custom"      - append custom CSS class

Example:

form.label :username, bootstrap: {label: {text: "Name", class: "important"}}


35
36
37
# File 'lib/bootstrap_form/bootstrap_options.rb', line 35

def label
  @label
end

#label_align_classObject (readonly)

CSS class for label alignment in horizontal form. Default: “text-sm-right”



22
23
24
# File 'lib/bootstrap_form/bootstrap_options.rb', line 22

def label_align_class
  @label_align_class
end

#label_col_classObject (readonly)

CSS class for label column when using horizontal form. Default: “col-sm-2”



16
17
18
# File 'lib/bootstrap_form/bootstrap_options.rb', line 16

def label_col_class
  @label_col_class
end

#layoutObject (readonly)

Controls form layout. Can be: “vertical” (default), “horizontal” or “inline”



13
14
15
# File 'lib/bootstrap_form/bootstrap_options.rb', line 13

def layout
  @layout
end

#prependObject (readonly)

Input groups allow prepending and appending arbitrary html. By default these are nil. Example usage:

form.text_field :dollars, bootstrap: {prepend: "$", append: ".00"}

For non-text values, use hash like so:

form.text_field :search, bootstrap: {append: {html: "<button>Go</button>".html_safe}}


46
47
48
# File 'lib/bootstrap_form/bootstrap_options.rb', line 46

def prepend
  @prepend
end

Instance Method Details

#horizontal?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/bootstrap_form/bootstrap_options.rb', line 72

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

#inline?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/bootstrap_form/bootstrap_options.rb', line 76

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

#offset_col_classObject



80
81
82
# File 'lib/bootstrap_form/bootstrap_options.rb', line 80

def offset_col_class
  label_col_class.sub(%r{\Acol-(\w+)-(\d+)\z}, 'offset-\1-\2')
end

#scoped(options = {}) ⇒ Object

This will return a copy of BootstrapOptions 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 scope: :login do |f|
  f.text_field :email, bootstrap: {label_col_class: "col-md-6", control_col_class: "col-md-6"}
  f.password_field :password
end


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

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

#set_options(options = {}) ⇒ Object



99
100
101
102
103
# File 'lib/bootstrap_form/bootstrap_options.rb', line 99

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