Class: Bootstrap4FormBuilder::FormBuilder::BootstrapBuilder
- Inherits:
-
ActionView::Helpers::FormBuilder
- Object
- ActionView::Helpers::FormBuilder
- Bootstrap4FormBuilder::FormBuilder::BootstrapBuilder
- Defined in:
- lib/bootstrap4_form_builder/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}
Instance Attribute Summary collapse
-
#control_col ⇒ Object
readonly
Returns the value of attribute control_col.
-
#label_col ⇒ Object
readonly
Returns the value of attribute label_col.
-
#layout ⇒ Object
readonly
Returns the value of attribute layout.
Instance Method Summary collapse
- #alert_message(title, options = {}) ⇒ Object
- #check_box(name, options = {}, checked_value = "1", unchecked_value = "0", &block) ⇒ Object
- #error_summary ⇒ Object
- #form_group(*args, &block) ⇒ Object
-
#initialize(object_name, object, template, options) ⇒ BootstrapBuilder
constructor
A new instance of BootstrapBuilder.
- #primary(name = nil, options = {}) ⇒ Object
- #radio_button(name, value, *args) ⇒ Object
- #standard_html(type, method, options = {}) ⇒ Object
- #submit(name = nil, options = {}) ⇒ Object
Constructor Details
#initialize(object_name, object, template, options) ⇒ BootstrapBuilder
Returns a new instance of BootstrapBuilder.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 14 def initialize(object_name, object, template, ) @layout = [:layout] @label_col = [:label_col] @control_col = [:control_col] @inline_errors = [:inline_errors].nil? ? true : [:inline_errors] @inline_error_class = [:inline_error_class] #puts "Inline Errors: #{@inline_errors}" #Could be done in helper. if inline_layout? .reverse_merge! html: {} [:html][:class] = [[:html][:class], inline_class].compact.join(" ") end super end |
Instance Attribute Details
#control_col ⇒ Object (readonly)
Returns the value of attribute control_col.
7 8 9 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 7 def control_col @control_col end |
#label_col ⇒ Object (readonly)
Returns the value of attribute label_col.
7 8 9 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 7 def label_col @label_col end |
#layout ⇒ Object (readonly)
Returns the value of attribute layout.
7 8 9 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 7 def layout @layout end |
Instance Method Details
#alert_message(title, options = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 134 def (title, = {}) alert_class = [:class] || 'alert alert-danger' display_errors = .delete(:error_summary) if object.respond_to?(:errors) && object.errors..any? content_tag :div, class: alert_class do content = [content_tag(:p, title)] content << error_summary if display_errors content.join.html_safe end end end |
#check_box(name, options = {}, checked_value = "1", unchecked_value = "0", &block) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 58 def check_box(name, = {}, checked_value = "1", unchecked_value = "0", &block) = .symbolize_keys! = .except(:label, :label_class, :inline) label_class = [[:label_class]] temporarily_disable_field_error_proc do html = super(name, , checked_value, unchecked_value) label_content = block_given? ? capture(&block) : [:label] html.concat(" ").concat(label_content || (object && object.class.human_attribute_name(name)) || name.to_s.humanize) label_name = name label_class = [:label_class] if [:inline] label_class = ["checkbox-inline", label_class].compact.join(' ') label(label_name, html, class: label_class) else content_tag(:div, class: "checkbox") do label(label_name, html, class: label_class) end end end end |
#error_summary ⇒ Object
147 148 149 150 151 152 153 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 147 def error_summary content_tag :ul, class: 'bootstrap-form-error-summary' do object.errors..each do |error| concat content_tag(:li, error) end end end |
#form_group(*args, &block) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 102 def form_group(*args, &block) = args. name = args.first [:class] = ["form-group", [:class]] [:class] << label_error_class if has_errors?(name) [:class] = [:class].compact.join(" ") content_tag(:div, .except(:label, :label_class)) do label_class = [@label_col, [:label_class]].compact.join(' ') label = @template.label_tag(nil, [:label], class: label_class) if [:label] controls = capture(&block).to_s error_help = generate_error_help(name) controls = content_tag(:div, class: @control_col) { concat(controls) } if gridded_form? concat(label).concat(controls).concat(error_help) end end |
#primary(name = nil, options = {}) ⇒ Object
129 130 131 132 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 129 def primary(name = nil, = {}) [:class] = ["btn btn-primary", [:class]].compact.join(" ") method(:submit).super_method.call(name, ) end |
#radio_button(name, value, *args) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 83 def (name, value, *args) = args..symbolize_keys! args << .except(:label, :label_class, :inline) label_class = [[:label_class]] temporarily_disable_field_error_proc do html = super(name, value, *args) + " " + value.humanize if [:inline] label_class = ["radio-inline", label_class].compact.join(" ") label(value, html, value: value, class: label_class) else content_tag(:div, class: "radio") do label(value, html, value: value, class: label_class) end end end end |
#standard_html(type, method, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 37 def standard_html(type, method, = {}) form_group_builder(method, ) do content = [] [:class] = [control_class, [:class]].compact.join(" ") temporarily_disable_field_error_proc do content << generate_label(method, ) if gridded_form? content << content_tag(:div, class: @control_col) do @template.send(type, object_name, method, ) end else content << @template.send(type, object_name, method, ) end end content << generate_error_help(method, ) content.compact.join.html_safe end end |
#submit(name = nil, options = {}) ⇒ Object
124 125 126 127 |
# File 'lib/bootstrap4_form_builder/form_builder.rb', line 124 def submit(name = nil, = {}) [:class] = ["btn btn-default", [:class]].compact.join(" ") super(name, ) end |