rails_bootstrap_form 0.5.0 → 0.5.2

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: d6b50f9d02bd4dce2b18bcbf5ce63159d4b62ef9c9b28a4e6e696b9780721e5f
4
- data.tar.gz: fdd32e9dff7e170d3a220f7ba06fb981e5fe1c24f7a292aa9f20133d624fc27f
3
+ metadata.gz: ed3da45cc2109afbc15026f1a12f80000890c7b7759bc7dbbbe855f3184bf0a2
4
+ data.tar.gz: 5e7897c4c60eb47a89f2eaf26a3f3ad46b5cc78cdc5519ff1dfe755302e7ab40
5
5
  SHA512:
6
- metadata.gz: e769de6bd7e33924dda68e244acc498b8586ff7eb23242abe228c959335290f1869142dbff651166c958e5f7aa863961b69e0c85ee35ce3b2c7fef94c76e573c
7
- data.tar.gz: 9e2dc6b8c51126c2b05760752c656ce6098c4ac178e66c9d7fc44f2e9a0013391dd82b3c36efb07562a6473adfff1af318905e3eb3a392bd6cce2db2dc83431d
6
+ metadata.gz: 6b84a087632ef6236a0b1975910ae6cabe61f5f1f70aa0f2950c765f53c23caa93de366b00517622a5fffabff0fb34794a563d04eb7ebe8bf4061bec6858ef57
7
+ data.tar.gz: 288e303d2c86355e5cd4a88d9046a84704d9091954981d7dee818f3f57f583995fa8bde1f28aa7f067ff851724deb6c8e00c03912a6c240da266000d353bd385
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.5.0)
4
+ rails_bootstrap_form (0.5.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -83,6 +83,7 @@ GEM
83
83
  crass (1.0.6)
84
84
  date (3.3.3)
85
85
  diff-lcs (1.5.0)
86
+ docile (1.4.0)
86
87
  erubi (1.12.0)
87
88
  execjs (2.8.1)
88
89
  ffi (1.15.5)
@@ -176,6 +177,12 @@ GEM
176
177
  sprockets (> 3.0)
177
178
  sprockets-rails
178
179
  tilt
180
+ simplecov (0.22.0)
181
+ docile (~> 1.1)
182
+ simplecov-html (~> 0.11)
183
+ simplecov_json_formatter (~> 0.1)
184
+ simplecov-html (0.12.3)
185
+ simplecov_json_formatter (0.1.4)
179
186
  sprockets (4.2.0)
180
187
  concurrent-ruby (~> 1.0)
181
188
  rack (>= 2.2.4, < 4)
@@ -207,6 +214,7 @@ DEPENDENCIES
207
214
  rake (~> 13.0)
208
215
  rspec-rails
209
216
  sassc-rails
217
+ simplecov
210
218
  sprockets-rails
211
219
  sqlite3 (~> 1.4)
212
220
 
@@ -4,7 +4,7 @@
4
4
  </div>
5
5
  <div class="card-body">
6
6
  <%= bootstrap_form_for @user, bootstrap_form: {} do |form| %>
7
- <%= form.text_field :name, autocomplete: "new-name", bootstrap_form: {} %>
7
+ <%= form.text_field :name, autocomplete: "new-name", class: "dsds", bootstrap_form: {} %>
8
8
  <%= form.text_field :email, autocomplete: "new-email", bootstrap_form: {} %>
9
9
  <%= form.text_field :password, autocomplete: "new-password" %>
10
10
  <%= form.phone_field :mobile_number %>
@@ -18,7 +18,7 @@
18
18
  <%= form.fields_for :address, include_id: false do |address_form| %>
19
19
  <%= address_form.text_area :street %>
20
20
  <%= address_form.text_field :state %>
21
- <%= address_form.grouped_collection_select :city, ::Country.all, :cities, :name, :id, :name %>
21
+ <%= address_form.grouped_collection_select :city, ::Country.all, :cities, :name, :id, :name, {include_blank: "Select city"} %>
22
22
  <%= address_form.text_field :postal_code %>
23
23
  <%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id),
24
24
  {include_blank: "Select Country", bootstrap_form: {}} %>
@@ -43,7 +43,9 @@
43
43
  help_texts: {
44
44
  user: {
45
45
  email: "Please use official email address",
46
- terms: "You must first accept terms and conditions in order to continue"
46
+ terms: "You must first accept terms and conditions in order to continue",
47
+ skill_ids: "Select your strong skills",
48
+ fruit_id: "Select your favorite fruit"
47
49
  }
48
50
  },
49
51
  }
@@ -18,6 +18,9 @@ gem "bootstrap", "~> 5.3.0.alpha3"
18
18
  # Use sqlite3 as the database for Active Record
19
19
  gem "sqlite3", "~> 1.4"
20
20
 
21
+ # Library to show coverage of the code.
22
+ gem "simplecov"
23
+
21
24
  group :development do
22
25
  gem "puma"
23
26
  end
@@ -25,10 +25,6 @@ module RailsBootstrapForm
25
25
  options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
26
26
  end
27
27
 
28
- def control_specific_class(field_tag_name)
29
- "rails-bootstrap-forms-#{field_tag_name.to_s.tr("_", "-")}"
30
- end
31
-
32
- private :apply_default_form_options, :control_specific_class
28
+ private :apply_default_form_options
33
29
  end
34
30
  end
@@ -90,6 +90,14 @@ module RailsBootstrapForm
90
90
  # Default is `false`.
91
91
  attr_accessor :switch
92
92
 
93
+ # Controls the HTML attributes and options that will be added to the field wrapper.
94
+ # Default is `{}`.
95
+ attr_accessor :wrapper_options
96
+
97
+ # Option to specify the size of input groups and fields.
98
+ # The valid values are `sm` and `lg`. The default value is `nil`.
99
+ attr_accessor :size
100
+
93
101
  def initialize(options = {})
94
102
  set_defaults
95
103
  set_bootstrap_form_options(options)
@@ -107,10 +115,6 @@ module RailsBootstrapForm
107
115
  @layout.to_s == "vertical"
108
116
  end
109
117
 
110
- def floating?
111
- @layout.to_s == "floating"
112
- end
113
-
114
118
  # This will return a copy of `BootstrapFormOptions` object with new options set
115
119
  # that don't affect original object. This way we can have options specific
116
120
  # to a given form field. For example, we can change grid just for one field:
@@ -152,6 +156,10 @@ module RailsBootstrapForm
152
156
  @static_field_class = "form-control-plaintext"
153
157
 
154
158
  @switch = false
159
+
160
+ @wrapper_options = {}
161
+
162
+ @size = nil
155
163
  end
156
164
 
157
165
  private :set_defaults
@@ -27,6 +27,12 @@ module RailsBootstrapForm
27
27
  end
28
28
  end
29
29
 
30
+ def check_box_wrapper_options(bootstrap_options)
31
+ {}.tap do |option|
32
+ option[:class] = check_box_wrapper_class(bootstrap_options)
33
+ end.merge(bootstrap_options.wrapper_options)
34
+ end
35
+
30
36
  def check_box_label_text(attribute, options, bootstrap_options, &block)
31
37
  block ? capture(&block) : label_text(attribute, bootstrap_options)
32
38
  end
@@ -46,16 +52,16 @@ module RailsBootstrapForm
46
52
 
47
53
  def check_box_label_class(attribute, bootstrap_options, options)
48
54
  classes = Array("form-check-label") << bootstrap_options.additional_label_class
49
- classes << "required" if is_field_required?(attribute, options)
55
+ classes << "required" if is_field_required?(attribute, options) && !bootstrap_options.inline?
50
56
  classes << "is-invalid" if is_invalid?(attribute)
51
57
  classes << bootstrap_options.hide_class if bootstrap_options.hide_label
52
58
  classes.flatten.compact
53
59
  end
54
60
 
55
61
  def check_box_wrapper_class(bootstrap_options)
56
- classes = ["mb-3", "form-check"]
62
+ classes = Array("form-check")
57
63
  classes << "form-switch" if bootstrap_options.switch
58
- classes << "form-check-inline" if bootstrap_options.inline?
64
+ classes << (bootstrap_options.inline? ? "form-check-inline" : "mb-3")
59
65
  classes.flatten.compact
60
66
  end
61
67
 
@@ -8,12 +8,15 @@ module RailsBootstrapForm
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  def self.included(base_class)
11
- def draw_label(attribute, bootstrap_options)
11
+ def draw_label(attribute, options, bootstrap_options)
12
12
  unless bootstrap_options.skip_label
13
- label_class = label_classes(attribute, bootstrap_options)
13
+ label_options = {
14
+ class: label_classes(attribute, bootstrap_options)
15
+ }
16
+ label_options[:for] = options[:id] if options[:id].present?
14
17
  label_text = label_text(attribute, bootstrap_options)
15
18
 
16
- label(attribute, label_text, class: label_class)
19
+ label(attribute, label_text, label_options)
17
20
  end
18
21
  end
19
22
 
@@ -28,6 +28,12 @@ module RailsBootstrapForm
28
28
  end
29
29
  end
30
30
 
31
+ def radio_button_wrapper_options(bootstrap_options)
32
+ {}.tap do |option|
33
+ option[:class] = radio_button_wrapper_class(bootstrap_options)
34
+ end.merge(bootstrap_options.wrapper_options)
35
+ end
36
+
31
37
  def radio_button_value(attribute, value)
32
38
  # label's `for` attribute needs to match checkbox tag's id,
33
39
  # IE sanitized value, IE
@@ -43,15 +49,15 @@ module RailsBootstrapForm
43
49
 
44
50
  def radio_button_label_class(attribute, bootstrap_options, options)
45
51
  classes = Array("form-check-label") << bootstrap_options.additional_label_class
46
- classes << "required" if is_field_required?(attribute, options)
52
+ classes << "required" if is_field_required?(attribute, options) && !bootstrap_options.inline?
47
53
  classes << "is-invalid" if is_invalid?(attribute)
48
54
  classes << bootstrap_options.hide_class if bootstrap_options.hide_label
49
55
  classes.flatten.compact
50
56
  end
51
57
 
52
58
  def radio_button_wrapper_class(bootstrap_options)
53
- classes = ["mb-3", "form-check"]
54
- classes << "form-check-inline" if bootstrap_options.inline?
59
+ classes = Array("form-check")
60
+ classes << (bootstrap_options.inline? ? "form-check-inline" : "mb-3")
55
61
  classes.flatten.compact
56
62
  end
57
63
 
@@ -13,11 +13,11 @@ module RailsBootstrapForm
13
13
  end
14
14
 
15
15
  def field_wrapper(attribute, bootstrap_options, options, &block)
16
- label = draw_label(attribute, bootstrap_options)
16
+ label = draw_label(attribute, options, bootstrap_options)
17
17
  help_text = help_text(attribute, bootstrap_options)
18
18
 
19
19
  if bootstrap_options.floating
20
- tag.div(class: field_wrapper_classes) do
20
+ tag.div(**field_wrapper_options(bootstrap_options)) do
21
21
  concat(input_group_wrapper(attribute, bootstrap_options) do
22
22
  tag.div(class: floating_label_classes(attribute)) do
23
23
  concat(capture(&block))
@@ -27,7 +27,7 @@ module RailsBootstrapForm
27
27
  concat(help_text)
28
28
  end
29
29
  else
30
- tag.div(class: field_wrapper_classes) do
30
+ tag.div(**field_wrapper_options(bootstrap_options)) do
31
31
  concat(label)
32
32
  concat(input_group_wrapper(attribute, bootstrap_options) do
33
33
  capture(&block)
@@ -37,14 +37,17 @@ module RailsBootstrapForm
37
37
  end
38
38
  end
39
39
 
40
+ def field_wrapper_options(bootstrap_options)
41
+ {}.tap do |option|
42
+ option[:class] = field_wrapper_classes
43
+ end.merge(bootstrap_options.wrapper_options)
44
+ end
45
+
40
46
  def field_wrapper_classes
41
47
  classes = [form_wrapper_default_class]
42
48
  classes.flatten.compact
43
49
  end
44
50
 
45
- def field_wrapper_options
46
- end
47
-
48
51
  def form_wrapper_default_class
49
52
  "mb-3"
50
53
  end
@@ -52,14 +55,17 @@ module RailsBootstrapForm
52
55
  def field_css_options(attribute, bootstrap_options, options, html_options)
53
56
  css_options = (html_options || options)
54
57
 
55
- field_classes = [
58
+ field_classes = Array(options[:class])
59
+ field_classes << [
56
60
  bootstrap_options.field_class,
57
61
  bootstrap_options.additional_field_class
58
62
  ]
59
63
  field_classes << "is-invalid" if is_invalid?(attribute)
64
+ if is_size_valid?(bootstrap_options)
65
+ field_classes << "#{bootstrap_options.field_class}-#{bootstrap_options.size}"
66
+ end
60
67
 
61
68
  css_options[:class] = field_classes.flatten.compact
62
-
63
69
  css_options.merge!(required_field_options(attribute, options))
64
70
 
65
71
  if bootstrap_options.floating
@@ -26,7 +26,10 @@ module RailsBootstrapForm
26
26
  end
27
27
 
28
28
  def input_group_classes(attribute, bootstrap_options)
29
- classes = ["input-group", bootstrap_options.additional_input_group_class]
29
+ classes = Array("input-group") << bootstrap_options.additional_input_group_class
30
+ if is_size_valid?(bootstrap_options)
31
+ classes << "input-group-#{bootstrap_options.size}"
32
+ end
30
33
  # Require `has-validation` class if field has errors.
31
34
  classes << "has-validation" if is_invalid?(attribute)
32
35
  classes.flatten.compact
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Inputs
7
+ module Base
8
+ extend ActiveSupport::Concern
9
+
10
+ def self.included(base_class)
11
+ def collection_input_checked?(checked, obj, input_value)
12
+ checked == input_value || Array(checked).try(:include?, input_value) ||
13
+ checked == obj || Array(checked).try(:include?, obj)
14
+ end
15
+
16
+ def control_specific_class(field_tag_name)
17
+ "rails-bootstrap-forms-#{field_tag_name.to_s.tr("_", "-")}"
18
+ end
19
+
20
+ def is_size_valid?(bootstrap_options)
21
+ bootstrap_options.size && %i(sm lg).include?(bootstrap_options.size)
22
+ end
23
+
24
+ private :collection_input_checked?, :control_specific_class, :is_size_valid?
25
+ end
26
+ end
27
+ end
28
+ end
@@ -5,6 +5,12 @@
5
5
  module RailsBootstrapForm
6
6
  module Inputs
7
7
 
8
+ extend ActiveSupport::Autoload
9
+
10
+ autoload :Base
11
+
12
+ include Base
13
+
8
14
  FIELD_HELPERS = %i[
9
15
  text_field
10
16
  url_field
@@ -39,7 +45,7 @@ module RailsBootstrapForm
39
45
  end
40
46
 
41
47
  DATE_SELECT_HELPERS.each do |field_tag_name|
42
- define_method(field_tag_name) do |attribute, options = {}, html_options = {}, &block|
48
+ define_method(field_tag_name) do |attribute, options = {}, html_options = {}|
43
49
  options = {bootstrap_form: {field_class: "form-select"}}.deep_merge!(options)
44
50
 
45
51
  field_wrapper_builder(attribute, options, html_options) do
@@ -131,11 +137,11 @@ module RailsBootstrapForm
131
137
 
132
138
  check_box_label = check_box_label(attribute, checked_value, options, bootstrap_options, &block)
133
139
 
134
- check_box_html = tag.div(class: check_box_wrapper_class(bootstrap_options)) do
140
+ check_box_html = tag.div(**check_box_wrapper_options(bootstrap_options)) do
135
141
  concat(check_box_field)
136
142
  concat(check_box_label)
137
- concat(check_box_help_text)
138
- concat(generate_error(attribute)) if is_invalid?(attribute)
143
+ concat(check_box_help_text) unless bootstrap_options.inline?
144
+ concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline?
139
145
  end
140
146
 
141
147
  check_box_html
@@ -151,11 +157,11 @@ module RailsBootstrapForm
151
157
 
152
158
  radio_button_label = radio_button_label(attribute, value, options, bootstrap_options)
153
159
 
154
- radio_button_html = tag.div(class: radio_button_wrapper_class(bootstrap_options)) do
160
+ radio_button_html = tag.div(**radio_button_wrapper_options(bootstrap_options)) do
155
161
  concat(radio_button_field)
156
162
  concat(radio_button_label)
157
- concat(radio_button_help_text)
158
- concat(generate_error(attribute)) if is_invalid?(attribute)
163
+ concat(radio_button_help_text) unless bootstrap_options.inline?
164
+ concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline?
159
165
  end
160
166
 
161
167
  radio_button_html
@@ -177,7 +183,15 @@ module RailsBootstrapForm
177
183
  inputs << check_box(attribute, input_options, input_value, nil)
178
184
  end
179
185
 
180
- inputs
186
+ if options.delete(:include_hidden) { true }
187
+ inputs.prepend hidden_field(attribute, value: "", multiple: true)
188
+ end
189
+
190
+ field_wrapper_builder(attribute, options, html_options) do
191
+ concat(tag.div(class: control_specific_class(:collection_check_boxes)) do
192
+ concat(inputs)
193
+ end)
194
+ end
181
195
  end
182
196
 
183
197
  def collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
@@ -198,14 +212,11 @@ module RailsBootstrapForm
198
212
  inputs << radio_button(attribute, input_value, input_options)
199
213
  end
200
214
 
201
- inputs
202
- end
203
-
204
- def collection_input_checked?(checked, obj, input_value)
205
- checked == input_value || Array(checked).try(:include?, input_value) ||
206
- checked == obj || Array(checked).try(:include?, obj)
215
+ field_wrapper_builder(attribute, options, html_options) do
216
+ concat(tag.div(class: control_specific_class(:collection_radio_buttons)) do
217
+ concat(inputs)
218
+ end)
219
+ end
207
220
  end
208
-
209
- private :collection_input_checked?
210
221
  end
211
222
  end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.5.0".freeze
6
+ VERSION = "0.5.2".freeze
7
7
  REQUIRED_RAILS_VERSION = "~> 7.0".freeze
8
8
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_bootstrap_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE (shivam091)
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-20 00:00:00.000000000 Z
11
+ date: 2023-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: generator_spec
@@ -122,6 +122,7 @@ files:
122
122
  - lib/rails_bootstrap_form/field_wrapper_builder.rb
123
123
  - lib/rails_bootstrap_form/input_group_builder.rb
124
124
  - lib/rails_bootstrap_form/inputs.rb
125
+ - lib/rails_bootstrap_form/inputs/base.rb
125
126
  - lib/rails_bootstrap_form/version.rb
126
127
  - sig/rails_bootstrap_form.rbs
127
128
  homepage: https://github.com/shivam091/rails_bootstrap_form