rails_bootstrap_form 0.4.2 → 0.5.0

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: 5874a38745d73c1b0bb05c751711cdc380be8684c71187069e07224e9d40333a
4
- data.tar.gz: ffe2043c13700e6015d7741b51550c52642098c412f29b1921a7e98ed20ec9cf
3
+ metadata.gz: d6b50f9d02bd4dce2b18bcbf5ce63159d4b62ef9c9b28a4e6e696b9780721e5f
4
+ data.tar.gz: fdd32e9dff7e170d3a220f7ba06fb981e5fe1c24f7a292aa9f20133d624fc27f
5
5
  SHA512:
6
- metadata.gz: 657b116576955954f11ecd923cd61277613bd732d7b986effe3d5b908ed38531549413baa47436167fd7d675fb5aab709863cc53bfb19f99d4a39abb2148de63
7
- data.tar.gz: '09d24eb35c72a56c834d0031ce85152e1a0241a1d4614dc69ebb5bc820b45780380d80132a9c882086b6524d8bb9fd5826d41c339f45bd87b335fcb0ceb97eeb'
6
+ metadata.gz: e769de6bd7e33924dda68e244acc498b8586ff7eb23242abe228c959335290f1869142dbff651166c958e5f7aa863961b69e0c85ee35ce3b2c7fef94c76e573c
7
+ data.tar.gz: 9e2dc6b8c51126c2b05760752c656ce6098c4ac178e66c9d7fc44f2e9a0013391dd82b3c36efb07562a6473adfff1af318905e3eb3a392bd6cce2db2dc83431d
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.4.2)
4
+ rails_bootstrap_form (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1,8 +1,8 @@
1
1
  label.is-invalid, .invalid-feedback {
2
- color: var(--bs-danger);
2
+ color: var(--bs-form-invalid-color);
3
3
  }
4
4
  label.required::after {
5
- color: var(--bs-danger);
5
+ color: var(--bs-form-invalid-color);
6
6
  content: "*";
7
7
  padding-left: .25rem;
8
8
  top: -2px;
@@ -3,6 +3,8 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  class Address < ApplicationRecord
6
+ validates :street, :city, :country_id, presence: true
7
+
6
8
  belongs_to :user
7
9
  belongs_to :country
8
10
 
@@ -4,7 +4,15 @@
4
4
 
5
5
  class User < ApplicationRecord
6
6
  validates :name, presence: true, length: {in: 2..50}
7
- validates :email, presence: true
7
+ validates :terms, acceptance: true
8
+ validates :email,
9
+ :password,
10
+ :mobile_number,
11
+ :birth_date,
12
+ :favorite_color,
13
+ :fruit_id,
14
+ :skill_ids,
15
+ presence: true
8
16
 
9
17
  has_one :address, dependent: :destroy
10
18
 
@@ -9,15 +9,12 @@
9
9
  <%= form.text_field :password, autocomplete: "new-password" %>
10
10
  <%= form.phone_field :mobile_number %>
11
11
  <%= form.date_field :birth_date %>
12
- <%= form.check_box :terms %>
12
+ <%= form.check_box :terms, bootstrap_form: {switch: false}, required: true %>
13
13
  <%= form.range_field :excellence %>
14
14
  <%= form.url_field :blog_url %>
15
+ <%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {bootstrap_form: {layout: :inline}, checked: form.object.fruit_id} %>
15
16
  <%= form.color_field :favorite_color %>
16
- <%= form.select :fruit_id, options_for_select(::Fruit.pluck(:name, :id), form.object.fruit_id),
17
- {include_blank: "Select Favorite Fruit"} %>
18
- <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name do |b| %>
19
- <%= b.check_box + b.text %>
20
- <% end %>
17
+ <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name, {bootstrap_form: {layout: :inline}} %>
21
18
  <%= form.fields_for :address, include_id: false do |address_form| %>
22
19
  <%= address_form.text_area :street %>
23
20
  <%= address_form.text_field :state %>
@@ -42,7 +42,8 @@
42
42
  },
43
43
  help_texts: {
44
44
  user: {
45
- email: "Please use official email address"
45
+ email: "Please use official email address",
46
+ terms: "You must first accept terms and conditions in order to continue"
46
47
  }
47
48
  },
48
49
  }
@@ -86,6 +86,10 @@ module RailsBootstrapForm
86
86
  # Default is `form-control-plaintext`.
87
87
  attr_accessor :static_field_class
88
88
 
89
+ # Option to control whether the check box should look like bootstrap switches.
90
+ # Default is `false`.
91
+ attr_accessor :switch
92
+
89
93
  def initialize(options = {})
90
94
  set_defaults
91
95
  set_bootstrap_form_options(options)
@@ -146,6 +150,8 @@ module RailsBootstrapForm
146
150
  @floating = false
147
151
 
148
152
  @static_field_class = "form-control-plaintext"
153
+
154
+ @switch = false
149
155
  end
150
156
 
151
157
  private :set_defaults
@@ -0,0 +1,66 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Components
7
+ module CheckBox
8
+ extend ActiveSupport::Concern
9
+
10
+ def self.included(base_class)
11
+ def check_box_label(attribute, checked_value, options, bootstrap_options, &block)
12
+ unless bootstrap_options.skip_label
13
+ label_options = {
14
+ class: check_box_label_class(attribute, bootstrap_options, options)
15
+ }
16
+ label_options[:for] = options[:id] if options[:id].present?
17
+
18
+ label_name = if options[:multiple]
19
+ check_box_value(attribute, checked_value)
20
+ else
21
+ attribute
22
+ end
23
+
24
+ label_text = check_box_label_text(attribute, options, bootstrap_options, &block)
25
+
26
+ label(label_name, label_text, label_options)
27
+ end
28
+ end
29
+
30
+ def check_box_label_text(attribute, options, bootstrap_options, &block)
31
+ block ? capture(&block) : label_text(attribute, bootstrap_options)
32
+ end
33
+
34
+ def check_box_value(attribute, value)
35
+ # label's `for` attribute needs to match checkbox tag's id,
36
+ # IE sanitized value, IE
37
+ # https://github.com/rails/rails/blob/5-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L123-L125
38
+ "#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase}"
39
+ end
40
+
41
+ def check_box_classes(attribute, options)
42
+ classes = Array(options[:class]) << "form-check-input"
43
+ classes << "is-invalid" if is_invalid?(attribute)
44
+ classes.flatten.compact
45
+ end
46
+
47
+ def check_box_label_class(attribute, bootstrap_options, options)
48
+ classes = Array("form-check-label") << bootstrap_options.additional_label_class
49
+ classes << "required" if is_field_required?(attribute, options)
50
+ classes << "is-invalid" if is_invalid?(attribute)
51
+ classes << bootstrap_options.hide_class if bootstrap_options.hide_label
52
+ classes.flatten.compact
53
+ end
54
+
55
+ def check_box_wrapper_class(bootstrap_options)
56
+ classes = ["mb-3", "form-check"]
57
+ classes << "form-switch" if bootstrap_options.switch
58
+ classes << "form-check-inline" if bootstrap_options.inline?
59
+ classes.flatten.compact
60
+ end
61
+
62
+ private :check_box_label, :check_box_classes, :check_box_label_class, :check_box_wrapper_class
63
+ end
64
+ end
65
+ end
66
+ end
@@ -13,7 +13,7 @@ module RailsBootstrapForm
13
13
 
14
14
  help_text = (bootstrap_options.help_text || scoped_help_text(attribute))
15
15
 
16
- tag.span(help_text, class: "form-text text-muted") if help_text.present?
16
+ tag.div(help_text, class: "form-text text-muted") if help_text.present?
17
17
  end
18
18
 
19
19
  def object_class
@@ -8,12 +8,12 @@ module RailsBootstrapForm
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  def self.included(base_class)
11
- def label(attribute, bootstrap_options)
11
+ def draw_label(attribute, bootstrap_options)
12
12
  unless bootstrap_options.skip_label
13
13
  label_class = label_classes(attribute, bootstrap_options)
14
14
  label_text = label_text(attribute, bootstrap_options)
15
15
 
16
- super(attribute, label_text, class: label_class)
16
+ label(attribute, label_text, class: label_class)
17
17
  end
18
18
  end
19
19
 
@@ -29,7 +29,7 @@ module RailsBootstrapForm
29
29
  bootstrap_options.label_text || object&.class.try(:human_attribute_name, attribute)
30
30
  end
31
31
 
32
- private :label, :label_classes, :label_text
32
+ private :draw_label, :label_classes, :label_text
33
33
  end
34
34
  end
35
35
  end
@@ -0,0 +1,62 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Components
7
+ module RadioButton
8
+ extend ActiveSupport::Concern
9
+
10
+ def self.included(base_class)
11
+ def radio_button_label(attribute, value, options, bootstrap_options)
12
+ unless bootstrap_options.skip_label
13
+ label_options = {
14
+ value: value,
15
+ class: radio_button_label_class(attribute, bootstrap_options, options)
16
+ }
17
+ label_options[:for] = options[:id] if options[:id].present?
18
+
19
+ label_name = if options[:multiple]
20
+ radio_button_value(attribute, checked_value)
21
+ else
22
+ attribute
23
+ end
24
+
25
+ label_text = label_text(attribute, bootstrap_options)
26
+
27
+ label(label_name, label_text, label_options)
28
+ end
29
+ end
30
+
31
+ def radio_button_value(attribute, value)
32
+ # label's `for` attribute needs to match checkbox tag's id,
33
+ # IE sanitized value, IE
34
+ # https://github.com/rails/rails/blob/5-0-stable/actionview/lib/action_view/helpers/tags/base.rb#L123-L125
35
+ "#{attribute}_#{value.to_s.gsub(/\s/, "_").gsub(/[^-[[:word:]]]/, "").mb_chars.downcase}"
36
+ end
37
+
38
+ def radio_button_classes(attribute, options)
39
+ classes = Array(options[:class]) << "form-check-input"
40
+ classes << "is-invalid" if is_invalid?(attribute)
41
+ classes.flatten.compact
42
+ end
43
+
44
+ def radio_button_label_class(attribute, bootstrap_options, options)
45
+ classes = Array("form-check-label") << bootstrap_options.additional_label_class
46
+ classes << "required" if is_field_required?(attribute, options)
47
+ classes << "is-invalid" if is_invalid?(attribute)
48
+ classes << bootstrap_options.hide_class if bootstrap_options.hide_label
49
+ classes.flatten.compact
50
+ end
51
+
52
+ def radio_button_wrapper_class(bootstrap_options)
53
+ classes = ["mb-3", "form-check"]
54
+ classes << "form-check-inline" if bootstrap_options.inline?
55
+ classes.flatten.compact
56
+ end
57
+
58
+ private :radio_button_label, :radio_button_classes, :radio_button_label_class, :radio_button_wrapper_class
59
+ end
60
+ end
61
+ end
62
+ end
@@ -10,10 +10,14 @@ module RailsBootstrapForm
10
10
  autoload :Labels
11
11
  autoload :RequiredField
12
12
  autoload :Errors
13
+ autoload :CheckBox
14
+ autoload :RadioButton
13
15
 
14
16
  include HelpText
15
17
  include Labels
16
18
  include RequiredField
17
19
  include Errors
20
+ include CheckBox
21
+ include RadioButton
18
22
  end
19
23
  end
@@ -13,7 +13,7 @@ module RailsBootstrapForm
13
13
  end
14
14
 
15
15
  def field_wrapper(attribute, bootstrap_options, options, &block)
16
- label = label(attribute, bootstrap_options)
16
+ label = draw_label(attribute, bootstrap_options)
17
17
  help_text = help_text(attribute, bootstrap_options)
18
18
 
19
19
  if bootstrap_options.floating
@@ -120,5 +120,92 @@ module RailsBootstrapForm
120
120
 
121
121
  text_field(attribute, static_options)
122
122
  end
123
+
124
+ def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0", &block)
125
+ bootstrap_options = bootstrap_form_options.scoped(options.delete(:bootstrap_form))
126
+
127
+ options[:class] = check_box_classes(attribute, options)
128
+
129
+ check_box_field = super(attribute, options, checked_value, unchecked_value)
130
+ check_box_help_text = help_text(attribute, bootstrap_options)
131
+
132
+ check_box_label = check_box_label(attribute, checked_value, options, bootstrap_options, &block)
133
+
134
+ check_box_html = tag.div(class: check_box_wrapper_class(bootstrap_options)) do
135
+ concat(check_box_field)
136
+ concat(check_box_label)
137
+ concat(check_box_help_text)
138
+ concat(generate_error(attribute)) if is_invalid?(attribute)
139
+ end
140
+
141
+ check_box_html
142
+ end
143
+
144
+ def radio_button(attribute, value, options = {})
145
+ bootstrap_options = bootstrap_form_options.scoped(options.delete(:bootstrap_form))
146
+
147
+ options[:class] = radio_button_classes(attribute, options)
148
+
149
+ radio_button_field = super(attribute, value, options)
150
+ radio_button_help_text = help_text(attribute, bootstrap_options)
151
+
152
+ radio_button_label = radio_button_label(attribute, value, options, bootstrap_options)
153
+
154
+ radio_button_html = tag.div(class: radio_button_wrapper_class(bootstrap_options)) do
155
+ concat(radio_button_field)
156
+ concat(radio_button_label)
157
+ concat(radio_button_help_text)
158
+ concat(generate_error(attribute)) if is_invalid?(attribute)
159
+ end
160
+
161
+ radio_button_html
162
+ end
163
+
164
+ def collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
165
+ options[:multiple] = true
166
+
167
+ inputs = ActiveSupport::SafeBuffer.new
168
+
169
+ collection.each do |object|
170
+ input_value = value_method.respond_to?(:call) ? value_method.call(object) : object.send(value_method)
171
+ input_options = {
172
+ bootstrap_form: {
173
+ label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method)
174
+ }
175
+ }.deep_merge!(options)
176
+
177
+ inputs << check_box(attribute, input_options, input_value, nil)
178
+ end
179
+
180
+ inputs
181
+ end
182
+
183
+ def collection_radio_buttons(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
184
+ inputs = ActiveSupport::SafeBuffer.new
185
+
186
+ collection.each do |object|
187
+ input_value = value_method.respond_to?(:call) ? value_method.call(object) : object.send(value_method)
188
+ input_options = {
189
+ bootstrap_form: {
190
+ label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method)
191
+ }
192
+ }.deep_merge!(options)
193
+
194
+ if (checked = input_options[:checked])
195
+ input_options[:checked] = collection_input_checked?(checked, object, object.send(value_method))
196
+ end
197
+
198
+ inputs << radio_button(attribute, input_value, input_options)
199
+ end
200
+
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)
207
+ end
208
+
209
+ private :collection_input_checked?
123
210
  end
124
211
  end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.4.2".freeze
6
+ VERSION = "0.5.0".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.4.2
4
+ version: 0.5.0
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-16 00:00:00.000000000 Z
11
+ date: 2023-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: generator_spec
@@ -111,9 +111,11 @@ files:
111
111
  - lib/rails_bootstrap_form/bootstrap_form_builder.rb
112
112
  - lib/rails_bootstrap_form/bootstrap_form_options.rb
113
113
  - lib/rails_bootstrap_form/components.rb
114
+ - lib/rails_bootstrap_form/components/check_box.rb
114
115
  - lib/rails_bootstrap_form/components/errors.rb
115
116
  - lib/rails_bootstrap_form/components/help_text.rb
116
117
  - lib/rails_bootstrap_form/components/labels.rb
118
+ - lib/rails_bootstrap_form/components/radio_button.rb
117
119
  - lib/rails_bootstrap_form/components/required_field.rb
118
120
  - lib/rails_bootstrap_form/configuration.rb
119
121
  - lib/rails_bootstrap_form/engine.rb