rails_bootstrap_form 0.9.7 → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3ffc0cdcd818f57b7e0b683ec59b2ac3a59b80edab6e40f9eebe1da8d32cd376
4
- data.tar.gz: 676be2383b1d1421b70a50ad5dc044eed15dfe4707b26ca345077644de9abebc
3
+ metadata.gz: 4d162d5f0c75c1cb4197b2a39c2a97507701b9551a36ed8dec2d0c854d1f1765
4
+ data.tar.gz: ed59ebe1781a986acaa02e21d58a0de124bd28db51416ab95651394bd77c3c60
5
5
  SHA512:
6
- metadata.gz: cdf31544bc3fd0032dff7b12bb95b1af33466d8e5a199ea0ecd263e1a8cdfdc2d00b02cc95425a1e712c6f2edd8ca626cf5e9ca6523fdf7eecf66a5a4776b17e
7
- data.tar.gz: 60e72524a66706827e25fd018ffbe4dc75e823d346a86627c525e0e9f095af6a490846dc04526127708f4c30811f59c30c94b32936543055dcc6bf2fd6a3af4f
6
+ metadata.gz: ce0eb94537c026a961c5323567e150c4c3c962252d6e670317a08176d6dc70c65aca0c70ea66dbf1ab90b874f25363218fc6fe084d819282b9310618e9853a2f
7
+ data.tar.gz: 5b7c554cc34e2e9d603f623a263bef1ad019fe2e9ff0c27a276e2c2196a92502d28d1a123e0c9ec93d38115e4c78c0174b8ef6500d3278e86908c25e094dfba6
data/.codeclimate.yml ADDED
@@ -0,0 +1,47 @@
1
+ version: "2"
2
+ checks:
3
+ argument-count:
4
+ enabled: true
5
+ config:
6
+ threshold: 4
7
+ complex-logic:
8
+ enabled: true
9
+ config:
10
+ threshold: 4
11
+ file-lines:
12
+ enabled: true
13
+ config:
14
+ threshold: 250
15
+ method-complexity:
16
+ enabled: true
17
+ config:
18
+ threshold: 5
19
+ method-count:
20
+ enabled: true
21
+ config:
22
+ threshold: 20
23
+ method-lines:
24
+ enabled: true
25
+ config:
26
+ threshold: 25
27
+ nested-control-flow:
28
+ enabled: true
29
+ config:
30
+ threshold: 4
31
+ return-statements:
32
+ enabled: true
33
+ config:
34
+ threshold: 4
35
+ exclude_patterns:
36
+ - "config/"
37
+ - "db/"
38
+ - "dist/"
39
+ - "features/"
40
+ - "demo/"
41
+ - "**/node_modules/"
42
+ - "script/"
43
+ - "**/spec/"
44
+ - "**/test/"
45
+ - "**/tests/"
46
+ - "**/vendor/"
47
+ - "**/*.d.ts"
data/CHANGELOG.md CHANGED
@@ -4,6 +4,16 @@ You can find recent releases with docs in GitHub:
4
4
 
5
5
  https://github.com/shivam091/rails_bootstrap_form/releases
6
6
 
7
+ ## [0.9.9](https://github.com/shivam091/rails_bootstrap_form/compare/v0.9.8...v0.9.9) - 2023-07-02
8
+
9
+ - Code smell fixes and removed `role="form"` from demo application and readme.
10
+
11
+ ## [0.9.8](https://github.com/shivam091/rails_bootstrap_form/compare/v0.9.7...v0.9.8) - 2023-07-01
12
+
13
+ ### What's fixed
14
+
15
+ - Fixed bug causing button helpers not to work with block and options ([#50](https://github.com/shivam091/rails_bootstrap_form/issues/50))
16
+
7
17
  ## [0.9.7](https://github.com/shivam091/rails_bootstrap_form/compare/v0.9.6...v0.9.7) - 2023-06-26
8
18
 
9
19
  ### What's new
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.9.7)
4
+ rails_bootstrap_form (0.9.9)
5
5
  actionpack (~> 7.0)
6
6
  activemodel (~> 7.0)
7
7
 
data/README.md CHANGED
@@ -2,6 +2,9 @@
2
2
 
3
3
  [![Ruby](https://github.com/shivam091/rails_bootstrap_form/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/shivam091/rails_bootstrap_form/actions/workflows/main.yml)
4
4
  [![Gem Version](https://badge.fury.io/rb/rails_bootstrap_form.svg)](https://badge.fury.io/rb/rails_bootstrap_form)
5
+ [![Gem Downloads](https://img.shields.io/gem/dt/rails_bootstrap_form.svg)](http://rubygems.org/gems/rails_bootstrap_form)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/8038de955c10394ae35d/test_coverage)](https://codeclimate.com/github/shivam091/rails_bootstrap_form/test_coverage)
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/8038de955c10394ae35d/maintainability)](https://codeclimate.com/github/shivam091/rails_bootstrap_form/maintainability)
5
8
 
6
9
  **rails_bootstrap_form** is a Rails form builder that makes it super easy to integrate [Bootstrap 5](https://getbootstrap.com/) forms into your Rails application.
7
10
  `rails_bootstrap_form`'s form helpers generate the form field and its label along with all the Bootstrap mark-up required for proper Bootstrap display.
@@ -28,7 +31,7 @@ for setting up `application.scss` and `application.js`.
28
31
  Add the `rails_bootstrap_form` gem to your `Gemfile`:
29
32
 
30
33
  ```ruby
31
- gem "rails_bootstrap_form", "~> 0.9.6"
34
+ gem "rails_bootstrap_form", "~> 0.9.8"
32
35
  ```
33
36
 
34
37
  Then:
@@ -94,7 +97,7 @@ To get started, use the `bootstrap_form_for` helper in place of the Rails [`form
94
97
  This generates the following HTML:
95
98
 
96
99
  ```html
97
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
100
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
98
101
  <div class="mb-3">
99
102
  <label class="form-label required" for="user_email">Email address</label>
100
103
  <input class="form-control" aria-required="true" required="required" type="email" name="user[email]" id="user_email">
@@ -132,7 +135,7 @@ To get started, use the `bootstrap_form_with` helper in place of the Rails [`for
132
135
  This generates the following HTML:
133
136
 
134
137
  ```html
135
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
138
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
136
139
  <div class="mb-3">
137
140
  <label class="form-label required" for="user_email">Email address</label>
138
141
  <input class="form-control" aria-required="true" required="required" type="email" name="user[email]" id="user_email">
@@ -200,7 +203,7 @@ Here's an example of a form where one field uses different layout:
200
203
  This generates the following HTML:
201
204
 
202
205
  ```html
203
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
206
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
204
207
  <div class="mb-3">
205
208
  <label class="form-label required" for="user_name">Name</label>
206
209
  <input class="form-control" aria-required="true" required="required" type="text" name="user[name]" id="user_name">
@@ -343,7 +346,7 @@ Here's an example of how it looks like:
343
346
  This generates the following HTML:
344
347
 
345
348
  ```html
346
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
349
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
347
350
  <div class="mb-3">
348
351
  <label class="form-label required" for="user_email">Email address</label>
349
352
  <input class="form-control" aria-required="true" required="required" type="email" name="user[email]" id="user_email">
@@ -376,7 +379,7 @@ Here's an example of how it looks like by default:
376
379
  This generates the following HTML:
377
380
 
378
381
  ```html
379
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
382
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
380
383
  <div class="row mb-3">
381
384
  <label class="col-form-label col-sm-2 required" for="user_email">Email address</label>
382
385
  <div class="col-sm-10">
@@ -413,7 +416,7 @@ The `label_col_wrapper_class` and `field_col_wrapper_class` css classes can also
413
416
  This generates the following HTML:
414
417
 
415
418
  ```html
416
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
419
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
417
420
  <div class="row mb-3">
418
421
  <label class="col-form-label col-sm-2 required" for="user_name">Name</label>
419
422
  <div class="col-sm-10">
@@ -467,7 +470,7 @@ Here's an example of how it looks like:
467
470
  This generates the following HTML:
468
471
 
469
472
  ```html
470
- <form role="form" novalidate="novalidate" class="new_user row row-cols-lg-auto g-3 align-items-center" id="new_user" action="/users" accept-charset="UTF-8" method="post">
473
+ <form novalidate="novalidate" class="new_user row row-cols-lg-auto g-3 align-items-center" id="new_user" action="/users" accept-charset="UTF-8" method="post">
471
474
  <div class="col-12">
472
475
  <label class="form-label visually-hidden required" for="user_email">Email address</label>
473
476
  <input class="form-control" aria-required="true" required="required" placeholder="Email address" type="email" name="user[email]" id="user_email">
@@ -918,7 +921,7 @@ Our `fields_for` helper accepts the same arguments as the [default Rails helper]
918
921
  This generates the following HTML:
919
922
 
920
923
  ```html
921
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
924
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
922
925
  <div class="mb-3">
923
926
  <label class="form-label required" for="user_email">Email address</label>
924
927
  <input autocomplete="new-email" class="form-control" aria-required="true" required="required" type="email" name="user[email]" id="user_email">
@@ -981,7 +984,7 @@ By setting `bootstrap` option on `bootstrap_form_for` or `bootstrap_form_with`,
981
984
  This generates the following HTML:
982
985
 
983
986
  ```html
984
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
987
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
985
988
  <div class="row mb-3">
986
989
  <label class="col-form-label col-sm-2 required" for="user_email">Email address</label>
987
990
  <div class="col-sm-10">
@@ -1927,7 +1930,7 @@ The `floating` option can be used to enable Bootstrap floating labels. This opti
1927
1930
  This generates the following HTML:
1928
1931
 
1929
1932
  ```html
1930
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
1933
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
1931
1934
  <div class="mb-3">
1932
1935
  <div class="form-floating">
1933
1936
  <input class="form-control" aria-required="true" required="required" placeholder="Name" type="text" name="user[name]" id="user_name">
@@ -1991,7 +1994,7 @@ By default, fields that have validation errors will be outlined in red and the e
1991
1994
  This generates the following HTML:
1992
1995
 
1993
1996
  ```html
1994
- <form role="form" novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
1997
+ <form novalidate="novalidate" class="new_user" id="new_user" action="/users" accept-charset="UTF-8" method="post">
1995
1998
  <div class="mb-3">
1996
1999
  <label class="form-label required is-invalid" for="user_email">Email address</label>
1997
2000
  <input class="form-control is-invalid" aria-required="true" required="required" type="email" name="user[email]" id="user_email">
@@ -6,5 +6,5 @@
6
6
 
7
7
  RailsBootstrapForm.configure do |config|
8
8
  # to make forms non-compliant with W3C.
9
- config.default_form_attributes = {role: "form", novalidate: true}
9
+ config.default_form_attributes = {novalidate: true}
10
10
  end
@@ -36,6 +36,8 @@ module RailsBootstrapForm
36
36
  end
37
37
  end
38
38
 
39
+ private
40
+
39
41
  def supress_form_field_errors
40
42
  original_proc = ActionView::Base.field_error_proc
41
43
  ActionView::Base.field_error_proc = RailsBootstrapForm.field_error_proc
@@ -43,8 +45,6 @@ module RailsBootstrapForm
43
45
  ensure
44
46
  ActionView::Base.field_error_proc = original_proc
45
47
  end
46
-
47
- private :supress_form_field_errors
48
48
  end
49
49
  end
50
50
  end
@@ -13,7 +13,7 @@ module RailsBootstrapForm
13
13
  include RailsBootstrapForm::InputGroupBuilder
14
14
  include RailsBootstrapForm::Inputs
15
15
 
16
- delegate :capture, :concat, :tag, to: :@template
16
+ delegate :capture, :concat, :tag, :button_tag, to: :@template
17
17
 
18
18
  attr_accessor :bootstrap_form_options
19
19
 
@@ -30,6 +30,8 @@ module RailsBootstrapForm
30
30
  super(record_name, record_object, fields_options, &block)
31
31
  end
32
32
 
33
+ private
34
+
33
35
  def apply_default_form_options(options)
34
36
  options[:html] ||= {}
35
37
  options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
@@ -49,7 +51,5 @@ module RailsBootstrapForm
49
51
  fields_options = bootstrap.deep_merge!(fields_options)
50
52
  fields_options
51
53
  end
52
-
53
- private :apply_default_form_options, :fields_for_options, :apply_default_form_classes
54
54
  end
55
55
  end
@@ -177,6 +177,8 @@ module RailsBootstrapForm
177
177
  self.field_class ||= css_class
178
178
  end
179
179
 
180
+ private
181
+
180
182
  def set_defaults
181
183
  @disabled = false
182
184
 
@@ -216,7 +218,5 @@ module RailsBootstrapForm
216
218
 
217
219
  @render_as_button = false
218
220
  end
219
-
220
- private :set_defaults
221
221
  end
222
222
  end
@@ -4,6 +4,9 @@
4
4
 
5
5
  module RailsBootstrapForm
6
6
  module FieldWrapperBuilder
7
+
8
+ private
9
+
7
10
  def field_wrapper_builder(attribute, bootstrap, options, html_options = nil, &block)
8
11
  field_options = field_css_options(attribute, bootstrap, options, html_options.try(:symbolize_keys!))
9
12
 
@@ -15,37 +18,17 @@ module RailsBootstrapForm
15
18
  help_text = help_text(attribute, bootstrap)
16
19
  wrapper_content = ActiveSupport::SafeBuffer.new
17
20
 
18
- if bootstrap.layout_horizontal?
19
- wrapper_content << label
20
- wrapper_content << tag.div(class: bootstrap.field_col_wrapper_class) do
21
- input_group_wrapper(attribute, bootstrap) do
22
- capture(&block)
23
- end + help_text
24
- end
21
+ wrapper_content = if bootstrap.layout_horizontal?
22
+ build_horizontal_layout_content(attribute, bootstrap, label, help_text, &block)
25
23
  else
26
24
  if bootstrap.floating?
27
- wrapper_content << input_group_wrapper(attribute, bootstrap) do
28
- tag.div(class: floating_label_classes(attribute)) do
29
- capture(&block) + label
30
- end
31
- end
32
- wrapper_content << help_text
25
+ build_floating_layout_content(attribute, bootstrap, label, help_text, &block)
33
26
  else
34
- wrapper_content << label
35
- wrapper_content << input_group_wrapper(attribute, bootstrap) do
36
- capture(&block)
37
- end
38
- wrapper_content << help_text
27
+ build_default_layout_content(attribute, bootstrap, label, help_text, &block)
39
28
  end
40
29
  end
41
30
 
42
- if bootstrap.wrapper
43
- tag.div(**field_wrapper_options(bootstrap)) do
44
- wrapper_content
45
- end
46
- else
47
- wrapper_content
48
- end
31
+ build_wrapper_element(bootstrap, wrapper_content)
49
32
  end
50
33
 
51
34
  def field_wrapper_options(bootstrap)
@@ -72,18 +55,11 @@ module RailsBootstrapForm
72
55
  def field_css_options(attribute, bootstrap, options, html_options)
73
56
  css_options = (html_options || options)
74
57
 
75
- field_classes = Array(bootstrap.field_class) << [bootstrap.additional_field_class || css_options[:class]]
76
- field_classes << "is-invalid" if is_invalid?(attribute)
77
- if is_size_valid?(bootstrap)
78
- field_classes << "#{bootstrap.field_class}-#{bootstrap.size}"
79
- end
58
+ field_classes = build_field_classes(attribute, bootstrap, css_options)
80
59
 
81
60
  css_options[:class] = field_classes.flatten.compact
82
61
  css_options.merge!(required_field_options(attribute, options))
83
-
84
- if placeholder_required?(bootstrap)
85
- css_options[:placeholder] ||= label_text(attribute, bootstrap)
86
- end
62
+ add_placeholder_if_required!(css_options, attribute, bootstrap)
87
63
 
88
64
  css_options
89
65
  end
@@ -96,11 +72,63 @@ module RailsBootstrapForm
96
72
  classes
97
73
  end
98
74
 
75
+ def build_field_classes(attribute, bootstrap, css_options)
76
+ field_classes = Array(bootstrap.field_class) <<
77
+ field_classes << [bootstrap.additional_field_class || css_options[:class]]
78
+ field_classes << "is-invalid" if is_invalid?(attribute)
79
+ field_classes << "#{bootstrap.field_class}-#{bootstrap.size}" if is_size_valid?(bootstrap)
80
+ field_classes
81
+ end
82
+
99
83
  def placeholder_required?(bootstrap)
100
84
  (bootstrap.floating? && !bootstrap.layout_horizontal?) || bootstrap.layout_inline?
101
85
  end
102
86
 
103
- private :field_wrapper, :field_wrapper_classes, :field_wrapper_default_class,
104
- :field_css_options, :floating_label_classes
87
+ def add_placeholder_if_required!(css_options, attribute, bootstrap)
88
+ css_options[:placeholder] ||= label_text(attribute, bootstrap) if placeholder_required?(bootstrap)
89
+ css_options
90
+ end
91
+
92
+ def build_horizontal_layout_content(attribute, bootstrap, label, help_text, &block)
93
+ wrapper_content = ActiveSupport::SafeBuffer.new
94
+ wrapper_content << label
95
+ wrapper_content << tag.div(class: bootstrap.field_col_wrapper_class) do
96
+ input_group_wrapper(attribute, bootstrap) do
97
+ capture(&block)
98
+ end + help_text
99
+ end
100
+ wrapper_content
101
+ end
102
+
103
+ def build_floating_layout_content(attribute, bootstrap, label, help_text, &block)
104
+ wrapper_content = ActiveSupport::SafeBuffer.new
105
+ wrapper_content << input_group_wrapper(attribute, bootstrap) do
106
+ tag.div(class: floating_label_classes(attribute)) do
107
+ capture(&block) + label
108
+ end
109
+ end
110
+ wrapper_content << help_text
111
+ wrapper_content
112
+ end
113
+
114
+ def build_default_layout_content(attribute, bootstrap, label, help_text, &block)
115
+ wrapper_content = ActiveSupport::SafeBuffer.new
116
+ wrapper_content << label
117
+ wrapper_content << input_group_wrapper(attribute, bootstrap) do
118
+ capture(&block)
119
+ end
120
+ wrapper_content << help_text
121
+ wrapper_content
122
+ end
123
+
124
+ def build_wrapper_element(bootstrap, wrapper_content)
125
+ if bootstrap.wrapper
126
+ tag.div(**field_wrapper_options(bootstrap)) do
127
+ wrapper_content
128
+ end
129
+ else
130
+ wrapper_content
131
+ end
132
+ end
105
133
  end
106
134
  end
@@ -7,37 +7,67 @@ module RailsBootstrapForm
7
7
  module Buttons
8
8
  extend ActiveSupport::Concern
9
9
 
10
- def self.included(base_class)
11
- def render_button(value = nil, options = {}, &block)
12
- value, options = nil, value if value.is_a?(Hash)
10
+ included do
11
+ def button(value, options, &block)
13
12
  bootstrap = bootstrap_form_options.scoped(options.delete(:bootstrap))
13
+ value, options = extract_button_value_and_options(value, options)
14
14
 
15
- button_html = if (bootstrap.render_as_button? || block)
16
- button(value, options, &block)
17
- else
18
- submit(value, options)
19
- end
20
-
21
- if bootstrap.layout_inline?
22
- tag.div(class: "col-12") { button_html }
23
- else
24
- button_html
25
- end
15
+ add_button_css_classes!(options, bootstrap)
16
+
17
+ button_html = render_button_html(value, options, bootstrap, &block)
18
+ button_html = wrap_button_html(button_html, bootstrap)
19
+
20
+ button_html
26
21
  end
27
22
 
28
23
  def secondary(value = nil, options = {}, &block)
29
- add_css_class!(options, "btn btn-secondary")
30
- render_button(value, options, &block)
24
+ button(value, options.merge!(variant: "secondary"), &block)
31
25
  end
32
26
 
33
27
  def primary(value = nil, options = {}, &block)
34
- add_css_class!(options, "btn btn-primary")
35
- render_button(value, options, &block)
28
+ button(value, options.merge!(variant: "primary"), &block)
36
29
  end
37
30
 
38
31
  def danger(value = nil, options = {}, &block)
39
- add_css_class!(options, "btn btn-danger")
40
- render_button(value, options, &block)
32
+ button(value, options.merge!(variant: "danger"), &block)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def button_variant_class(options)
39
+ case options.delete(:variant)
40
+ when "secondary" then "btn-secondary"
41
+ when "primary" then "btn-primary"
42
+ when "danger" then "btn-danger"
43
+ else ""
44
+ end
45
+ end
46
+
47
+ def extract_button_value_and_options(value, options)
48
+ return [nil, value.merge(options)] if value.is_a?(Hash)
49
+
50
+ [value, options]
51
+ end
52
+
53
+ def add_button_css_classes!(options, bootstrap)
54
+ add_css_class!(options, "btn")
55
+ add_css_class!(options, button_variant_class(options))
56
+ end
57
+
58
+ def render_button_html(value, options, bootstrap, &block)
59
+ if bootstrap.render_as_button? || block
60
+ button_tag(value, options, &block)
61
+ else
62
+ submit(value, options)
63
+ end
64
+ end
65
+
66
+ def wrap_button_html(button_html, bootstrap)
67
+ if bootstrap.layout_inline?
68
+ tag.div(class: "col-12") { button_html }
69
+ else
70
+ button_html
41
71
  end
42
72
  end
43
73
  end
@@ -7,76 +7,92 @@ module RailsBootstrapForm
7
7
  module Choice
8
8
  extend ActiveSupport::Concern
9
9
 
10
- def self.included(base_class)
11
- [:check_box, :radio_button].each do |tag_name|
12
- define_method("#{tag_name}_label") do |attribute, value, options, bootstrap|
13
- unless bootstrap.skip_label?
14
- label_options = {
15
- class: choice_label_classes(attribute, bootstrap, options)
16
- }
17
- label_options[:value] = value if tag_name.eql?(:radio_button)
18
- label_options[:for] = options[:id] if options[:id].present?
10
+ private
19
11
 
20
- label_text = label_text(attribute, bootstrap)
12
+ [:check_box, :radio_button].each do |tag_name|
13
+ define_method("#{tag_name}_label") do |attribute, value, options, bootstrap|
14
+ unless bootstrap.skip_label?
15
+ label_options = {
16
+ class: choice_label_classes(attribute, bootstrap, options)
17
+ }
18
+ label_options[:value] = value if tag_name.eql?(:radio_button)
19
+ label_options[:for] = options[:id] if options[:id].present?
21
20
 
22
- label(attribute, label_text, label_options)
23
- end
21
+ label_text = label_text(attribute, bootstrap)
22
+
23
+ label(attribute, label_text, label_options)
24
24
  end
25
25
  end
26
+ end
26
27
 
27
- [:check_box, :radio_button].each do |tag_name|
28
- define_method("bootstrap_#{tag_name}") do |attribute, value, options, bootstrap|
29
- options[:class] = choice_classes(attribute, bootstrap, options)
30
-
31
- if tag_name.eql?(:check_box)
32
- choice_field = check_box_without_bootstrap(attribute, options, value, nil)
33
- choice_label = check_box_label(attribute, value, options, bootstrap)
34
- else
35
- choice_field = radio_button_without_bootstrap(attribute, value, options)
36
- choice_label = radio_button_label(attribute, value, options, bootstrap)
37
- end
28
+ [:check_box, :radio_button].each do |tag_name|
29
+ define_method("bootstrap_#{tag_name}") do |attribute, value, options, bootstrap|
30
+ options[:class] = choice_classes(attribute, bootstrap, options)
38
31
 
39
- choice_field + choice_label
32
+ if tag_name.eql?(:check_box)
33
+ choice_field = check_box_without_bootstrap(attribute, options, value, nil)
34
+ choice_label = check_box_label(attribute, value, options, bootstrap)
35
+ else
36
+ choice_field = radio_button_without_bootstrap(attribute, value, options)
37
+ choice_label = radio_button_label(attribute, value, options, bootstrap)
40
38
  end
41
- end
42
39
 
43
- def choice_classes(attribute, bootstrap, options)
44
- classes = Array("form-check-input") << [bootstrap.additional_field_class || options[:class]]
45
- classes << "is-invalid" if is_invalid?(attribute)
46
- classes.flatten.compact
40
+ choice_field + choice_label
47
41
  end
42
+ end
48
43
 
49
- def choice_label_classes(attribute, bootstrap, options)
50
- classes = Array("form-check-label") << bootstrap.additional_label_class
51
- classes << "required" if is_field_required?(attribute, options)
52
- classes << "is-invalid" if is_invalid?(attribute)
53
- classes << bootstrap.hide_class if bootstrap.hide_label?
54
- classes.flatten.compact
44
+ [:check_box, :radio_button].each do |tag_name|
45
+ define_method("build_#{tag_name}_html") do |attribute, value, bootstrap, options|
46
+ tag.div(class: choice_wrapper_classes(bootstrap)) do
47
+ concat(send("bootstrap_#{tag_name}", attribute, value, options, bootstrap))
48
+ concat(help_text(attribute, bootstrap))
49
+ concat(generate_error(attribute)) if is_invalid?(attribute)
50
+ end
55
51
  end
52
+ end
56
53
 
57
- def choice_container_classes(bootstrap)
58
- classes = Array(bootstrap.field_col_wrapper_class)
59
- classes << field_offset_class(bootstrap.label_col_wrapper_class)
60
- classes.flatten.compact
54
+ [:check_box, :radio_button].each do |tag_name|
55
+ define_method("build_wrapped_#{tag_name}_html") do |bootstrap, tag_html|
56
+ tag.div(**field_wrapper_options(bootstrap)) do
57
+ if bootstrap.layout_horizontal?
58
+ tag.div(class: choice_container_classes(bootstrap)) { tag_html }
59
+ else
60
+ tag_html
61
+ end
62
+ end
61
63
  end
64
+ end
62
65
 
63
- def choice_wrapper_classes(bootstrap)
64
- classes = Array("form-check")
65
- classes << "form-check-inline" if bootstrap.inline?
66
- classes << "form-switch" if bootstrap.switch?
67
- classes.flatten.compact
68
- end
66
+ def choice_classes(attribute, bootstrap, options)
67
+ classes = Array("form-check-input") << [bootstrap.additional_field_class || options[:class]]
68
+ classes << "is-invalid" if is_invalid?(attribute)
69
+ classes.flatten.compact
70
+ end
69
71
 
70
- def collection_input_checked?(checked, obj, input_value)
71
- checked == input_value || Array(checked).try(:include?, input_value) ||
72
- checked == obj || Array(checked).try(:include?, obj)
73
- end
72
+ def choice_label_classes(attribute, bootstrap, options)
73
+ classes = Array("form-check-label") << bootstrap.additional_label_class
74
+ classes << "required" if is_field_required?(attribute, options)
75
+ classes << "is-invalid" if is_invalid?(attribute)
76
+ classes << bootstrap.hide_class if bootstrap.hide_label?
77
+ classes.flatten.compact
78
+ end
79
+
80
+ def choice_container_classes(bootstrap)
81
+ classes = Array(bootstrap.field_col_wrapper_class)
82
+ classes << field_offset_class(bootstrap.label_col_wrapper_class)
83
+ classes.flatten.compact
84
+ end
85
+
86
+ def choice_wrapper_classes(bootstrap)
87
+ classes = Array("form-check")
88
+ classes << "form-check-inline" if bootstrap.inline?
89
+ classes << "form-switch" if bootstrap.switch?
90
+ classes.flatten.compact
91
+ end
74
92
 
75
- private :choice_classes, :choice_label_classes,
76
- :choice_container_classes, :choice_wrapper_classes,
77
- :check_box_label, :radio_button_label,
78
- :bootstrap_check_box, :bootstrap_radio_button,
79
- :collection_input_checked?
93
+ def collection_input_checked?(checked, obj, input_value)
94
+ checked == input_value || Array(checked).try(:include?, input_value) ||
95
+ checked == obj || Array(checked).try(:include?, obj)
80
96
  end
81
97
  end
82
98
  end