rails_bootstrap_form 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ed3da45cc2109afbc15026f1a12f80000890c7b7759bc7dbbbe855f3184bf0a2
4
- data.tar.gz: 5e7897c4c60eb47a89f2eaf26a3f3ad46b5cc78cdc5519ff1dfe755302e7ab40
3
+ metadata.gz: aa73fbc58e6dd0b2dc4b823cd6732de17b0e84e52d9177bce4684b61609238e0
4
+ data.tar.gz: f190515751e2cbc2bf51c195c8aa0ac7e2459ae040f1ba8dd544532f6887a514
5
5
  SHA512:
6
- metadata.gz: 6b84a087632ef6236a0b1975910ae6cabe61f5f1f70aa0f2950c765f53c23caa93de366b00517622a5fffabff0fb34794a563d04eb7ebe8bf4061bec6858ef57
7
- data.tar.gz: 288e303d2c86355e5cd4a88d9046a84704d9091954981d7dee818f3f57f583995fa8bde1f28aa7f067ff851724deb6c8e00c03912a6c240da266000d353bd385
6
+ metadata.gz: f41528a442151162803ec730b18fde0d10cd002446e5a402c037acb4aea7933e5749a5f5d3824762a83fc41a5fc2d87ecedafc7371755c45cdeafcb783f13a7f
7
+ data.tar.gz: 139a3f9bd52be5936676ddc8b730a9a211f715f32b8ca8ba12ad6d045233ed9836e90317bf33a43ab07e27ac52c0ae77e5c9c290e9157e75def6b07341a96a30
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.5.2)
4
+ rails_bootstrap_form (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -20,3 +20,6 @@ label.required::after {
20
20
  display: inline-block;
21
21
  width: auto;
22
22
  }
23
+ .invalid-feedback {
24
+ display: block;
25
+ }
@@ -3,3 +3,5 @@
3
3
  <% end %>
4
4
 
5
5
  <%= render partial: "users/vertical_form" %>
6
+
7
+ <%= render partial: "users/horizontal_form" %>
@@ -0,0 +1,32 @@
1
+ <div class="card card-primary my-3">
2
+ <div class="card-header fw-bold">
3
+ Profile Form (Horizontal layout)
4
+ </div>
5
+ <div class="card-body">
6
+ <%= bootstrap_form_for @user, bootstrap_form: {layout: :horizontal} do |form| %>
7
+ <%= form.text_field :name, autocomplete: "new-name" %>
8
+ <%= form.text_field :email, autocomplete: "new-email" %>
9
+ <%= form.text_field :password, autocomplete: "new-password" %>
10
+ <%= form.phone_field :mobile_number %>
11
+ <%= form.date_field :birth_date %>
12
+ <%= form.radio_button :terms, 1, required: true %>
13
+ <%= form.range_field :excellence %>
14
+ <%= form.url_field :blog_url %>
15
+ <%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {bootstrap_form: {}, checked: form.object.fruit_id} %>
16
+ <%= form.color_field :favorite_color %>
17
+ <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name, {bootstrap_form: {}} %>
18
+ <%= form.fields_for :address, include_id: false, bootstrap_form: {layout: :horizontal} do |address_form| %>
19
+ <%= address_form.text_area :street %>
20
+ <%= address_form.text_field :state %>
21
+ <%= address_form.grouped_collection_select :city, ::Country.includes(:cities), :cities, :name, :id, :name, {include_blank: "Select city"} %>
22
+ <%= address_form.text_field :postal_code %>
23
+ <%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id),
24
+ {include_blank: "Select Country", bootstrap_form: {}} %>
25
+ <% end %>
26
+ <div class="mt-3">
27
+ <%= form.submit "Register", class: "btn btn-primary" %>
28
+ <%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
29
+ </div>
30
+ <% end %>
31
+ </div>
32
+ </div>
@@ -12,13 +12,13 @@
12
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
+ <%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {bootstrap_form: {}, checked: form.object.fruit_id} %>
16
16
  <%= form.color_field :favorite_color %>
17
- <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name, {bootstrap_form: {layout: :inline}} %>
17
+ <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name, {bootstrap_form: {}} %>
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, {include_blank: "Select city"} %>
21
+ <%= address_form.grouped_collection_select :city, ::Country.includes(:cities), :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: {}} %>
@@ -50,8 +50,9 @@ module RailsBootstrapForm
50
50
  # readers. Default is `visually-hidden`
51
51
  attr_accessor :hide_class
52
52
 
53
- # Default CSS class that will be applied to all label tags.
54
- # Default is `form-label`.
53
+ # Default CSS class that will be applied to all label tags when layout is
54
+ # vertical.
55
+ # The default value is `form-label`.
55
56
  attr_accessor :label_class
56
57
 
57
58
  # An additional CSS class that will be added along with the existing
@@ -98,29 +99,53 @@ module RailsBootstrapForm
98
99
  # The valid values are `sm` and `lg`. The default value is `nil`.
99
100
  attr_accessor :size
100
101
 
102
+ # Option to render checkboxes and radio buttons inline.
103
+ # The default value if `false`.
104
+ #
105
+ # Example:
106
+ # form.collection_radio_buttons :choices, ["yes", "no"], :to_s, :to_s, bootstrap_form: {inline: true}
107
+ attr_accessor :inline
108
+
109
+ # Default CSS class that will be applied to all label tags when layout is
110
+ # horizontal.
111
+ # The default value is `col-form-label`.
112
+ attr_accessor :label_col_class
113
+
114
+ # Default CSS class for label column when using horizontal form.
115
+ # The default value is `col-sm-2`.
116
+ attr_accessor :label_col_wrapper_class
117
+
118
+ # Default CSS class for control column when using horizontal form.
119
+ # The default value is `col-sm-10`.
120
+ attr_accessor :field_col_wrapper_class
121
+
101
122
  def initialize(options = {})
102
123
  set_defaults
103
124
  set_bootstrap_form_options(options)
104
125
  end
105
126
 
106
- def horizontal?
127
+ def layout_horizontal?
107
128
  @layout.to_s == "horizontal"
108
129
  end
109
130
 
110
- def inline?
131
+ def layout_inline?
111
132
  @layout.to_s == "inline"
112
133
  end
113
134
 
114
- def vertical?
135
+ def layout_vertical?
115
136
  @layout.to_s == "vertical"
116
137
  end
117
138
 
139
+ def inline?
140
+ self.inline
141
+ end
142
+
118
143
  # This will return a copy of `BootstrapFormOptions` object with new options set
119
144
  # that don't affect original object. This way we can have options specific
120
145
  # to a given form field. For example, we can change grid just for one field:
121
146
  #
122
147
  # bootstrap_form_with model: @user do |f|
123
- # f.text_field :email, bootstrap_form: {label_col_class: "col-md-6", control_col_class: "col-md-6"}
148
+ # f.text_field :email, bootstrap_form: {label_col_wrapper_class: "col-md-6", field_col_wrapper_class: "col-md-6"}
124
149
  # f.password_field :password
125
150
  # end
126
151
  #
@@ -160,6 +185,12 @@ module RailsBootstrapForm
160
185
  @wrapper_options = {}
161
186
 
162
187
  @size = nil
188
+
189
+ @inline = false
190
+
191
+ @label_col_class = "col-form-label"
192
+ @label_col_wrapper_class = "col-sm-2"
193
+ @field_col_wrapper_class = "col-sm-10"
163
194
  end
164
195
 
165
196
  private :set_defaults
@@ -7,6 +7,8 @@ module RailsBootstrapForm
7
7
  module CheckBox
8
8
  extend ActiveSupport::Concern
9
9
 
10
+ include RailsBootstrapForm::Helpers
11
+
10
12
  def self.included(base_class)
11
13
  def check_box_label(attribute, checked_value, options, bootstrap_options, &block)
12
14
  unless bootstrap_options.skip_label
@@ -61,11 +63,19 @@ module RailsBootstrapForm
61
63
  def check_box_wrapper_class(bootstrap_options)
62
64
  classes = Array("form-check")
63
65
  classes << "form-switch" if bootstrap_options.switch
64
- classes << (bootstrap_options.inline? ? "form-check-inline" : "mb-3")
66
+ classes << "form-check-inline" if bootstrap_options.inline?
67
+ classes << "mb-3" unless (bootstrap_options.layout_horizontal? || bootstrap_options.inline?)
68
+ classes.flatten.compact
69
+ end
70
+
71
+ def check_box_container_classes(bootstrap_options)
72
+ classes = Array(bootstrap_options.field_col_wrapper_class)
73
+ classes << field_offset_class(bootstrap_options.label_col_wrapper_class)
65
74
  classes.flatten.compact
66
75
  end
67
76
 
68
- private :check_box_label, :check_box_classes, :check_box_label_class, :check_box_wrapper_class
77
+ private :check_box_label, :check_box_classes, :check_box_label_class,
78
+ :check_box_wrapper_class, :check_box_container_classes
69
79
  end
70
80
  end
71
81
  end
@@ -21,7 +21,13 @@ module RailsBootstrapForm
21
21
  end
22
22
 
23
23
  def label_classes(attribute, bootstrap_options)
24
- classes = [bootstrap_options.label_class, bootstrap_options.additional_label_class]
24
+ classes = []
25
+ classes << if bootstrap_options.layout_horizontal?
26
+ [bootstrap_options.label_col_class, bootstrap_options.label_col_wrapper_class]
27
+ else
28
+ bootstrap_options.label_class
29
+ end
30
+ classes << bootstrap_options.additional_label_class
25
31
  classes << bootstrap_options.hide_class if bootstrap_options.hide_label
26
32
  classes << "required" if is_attribute_required?(attribute)
27
33
  classes << "is-invalid" if is_invalid?(attribute)
@@ -7,6 +7,8 @@ module RailsBootstrapForm
7
7
  module RadioButton
8
8
  extend ActiveSupport::Concern
9
9
 
10
+ include RailsBootstrapForm::Helpers
11
+
10
12
  def self.included(base_class)
11
13
  def radio_button_label(attribute, value, options, bootstrap_options)
12
14
  unless bootstrap_options.skip_label
@@ -57,11 +59,19 @@ module RailsBootstrapForm
57
59
 
58
60
  def radio_button_wrapper_class(bootstrap_options)
59
61
  classes = Array("form-check")
60
- classes << (bootstrap_options.inline? ? "form-check-inline" : "mb-3")
62
+ classes << "form-check-inline" if bootstrap_options.inline?
63
+ classes << "mb-3" unless (bootstrap_options.layout_horizontal? || bootstrap_options.inline?)
64
+ classes.flatten.compact
65
+ end
66
+
67
+ def radio_button_container_classes(bootstrap_options)
68
+ classes = Array(bootstrap_options.field_col_wrapper_class)
69
+ classes << field_offset_class(bootstrap_options.label_col_wrapper_class)
61
70
  classes.flatten.compact
62
71
  end
63
72
 
64
- private :radio_button_label, :radio_button_classes, :radio_button_label_class, :radio_button_wrapper_class
73
+ private :radio_button_label, :radio_button_classes, :radio_button_label_class,
74
+ :radio_button_wrapper_class, :radio_button_container_classes
65
75
  end
66
76
  end
67
77
  end
@@ -16,35 +16,49 @@ module RailsBootstrapForm
16
16
  label = draw_label(attribute, options, bootstrap_options)
17
17
  help_text = help_text(attribute, bootstrap_options)
18
18
 
19
- if bootstrap_options.floating
19
+ if bootstrap_options.layout_horizontal?
20
20
  tag.div(**field_wrapper_options(bootstrap_options)) do
21
- concat(input_group_wrapper(attribute, bootstrap_options) do
22
- tag.div(class: floating_label_classes(attribute)) do
23
- concat(capture(&block))
24
- concat(label)
25
- end
21
+ concat(label)
22
+ concat(tag.div(class: bootstrap_options.field_col_wrapper_class) do
23
+ concat(input_group_wrapper(attribute, bootstrap_options) do
24
+ capture(&block)
25
+ end)
26
+ concat(help_text)
26
27
  end)
27
- concat(help_text)
28
28
  end
29
29
  else
30
- tag.div(**field_wrapper_options(bootstrap_options)) do
31
- concat(label)
32
- concat(input_group_wrapper(attribute, bootstrap_options) do
33
- capture(&block)
34
- end)
35
- concat(help_text)
30
+ if bootstrap_options.floating
31
+ tag.div(**field_wrapper_options(bootstrap_options)) do
32
+ concat(input_group_wrapper(attribute, bootstrap_options) do
33
+ tag.div(class: floating_label_classes(attribute)) do
34
+ concat(capture(&block))
35
+ concat(label)
36
+ end
37
+ end)
38
+ concat(help_text)
39
+ end
40
+ else
41
+ tag.div(**field_wrapper_options(bootstrap_options)) do
42
+ concat(label)
43
+ concat(input_group_wrapper(attribute, bootstrap_options) do
44
+ capture(&block)
45
+ end)
46
+ concat(help_text)
47
+ end
36
48
  end
37
49
  end
38
50
  end
39
51
 
40
52
  def field_wrapper_options(bootstrap_options)
41
53
  {}.tap do |option|
42
- option[:class] = field_wrapper_classes
54
+ option[:class] = field_wrapper_classes(bootstrap_options)
43
55
  end.merge(bootstrap_options.wrapper_options)
44
56
  end
45
57
 
46
- def field_wrapper_classes
47
- classes = [form_wrapper_default_class]
58
+ def field_wrapper_classes(bootstrap_options)
59
+ classes = []
60
+ classes << "row" if bootstrap_options.layout_horizontal?
61
+ classes << form_wrapper_default_class
48
62
  classes.flatten.compact
49
63
  end
50
64
 
@@ -68,7 +82,7 @@ module RailsBootstrapForm
68
82
  css_options[:class] = field_classes.flatten.compact
69
83
  css_options.merge!(required_field_options(attribute, options))
70
84
 
71
- if bootstrap_options.floating
85
+ if (bootstrap_options.floating && !bootstrap_options.layout_horizontal?)
72
86
  css_options[:placeholder] ||= label_text(attribute, bootstrap_options)
73
87
  end
74
88
 
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Helpers
7
+ extend ActiveSupport::Concern
8
+
9
+ def self.included(base_class)
10
+ def collection_input_checked?(checked, obj, input_value)
11
+ checked == input_value || Array(checked).try(:include?, input_value) ||
12
+ checked == obj || Array(checked).try(:include?, obj)
13
+ end
14
+
15
+ def control_specific_class(field_tag_name)
16
+ "rails-bootstrap-forms-#{field_tag_name.to_s.tr("_", "-")}"
17
+ end
18
+
19
+ def is_size_valid?(bootstrap_options)
20
+ bootstrap_options.size && %i(sm lg).include?(bootstrap_options.size)
21
+ end
22
+
23
+ def field_offset_class(label_col_wrapper_class)
24
+ label_col_wrapper_class.gsub(/\bcol-(\w+)-(\d)\b/, 'offset-\1-\2')
25
+ end
26
+
27
+ private :collection_input_checked?, :control_specific_class, :is_size_valid?
28
+ end
29
+ end
30
+ end
@@ -3,14 +3,9 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- module Inputs
7
-
8
- extend ActiveSupport::Autoload
9
-
10
- autoload :Base
11
-
12
- include Base
6
+ include Helpers
13
7
 
8
+ module Inputs
14
9
  FIELD_HELPERS = %i[
15
10
  text_field
16
11
  url_field
@@ -141,10 +136,22 @@ module RailsBootstrapForm
141
136
  concat(check_box_field)
142
137
  concat(check_box_label)
143
138
  concat(check_box_help_text) unless bootstrap_options.inline?
144
- concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline?
139
+ concat(generate_error(attribute)) if (is_invalid?(attribute) && !bootstrap_options.inline?)
145
140
  end
146
141
 
147
- check_box_html
142
+ if bootstrap_options.inline?
143
+ check_box_html
144
+ else
145
+ if bootstrap_options.layout_horizontal?
146
+ tag.div(class: field_wrapper_classes(bootstrap_options)) do
147
+ tag.div(class: check_box_container_classes(bootstrap_options)) do
148
+ check_box_html
149
+ end
150
+ end
151
+ else
152
+ check_box_html
153
+ end
154
+ end
148
155
  end
149
156
 
150
157
  def radio_button(attribute, value, options = {})
@@ -161,10 +168,22 @@ module RailsBootstrapForm
161
168
  concat(radio_button_field)
162
169
  concat(radio_button_label)
163
170
  concat(radio_button_help_text) unless bootstrap_options.inline?
164
- concat(generate_error(attribute)) if is_invalid?(attribute) && !bootstrap_options.inline?
171
+ concat(generate_error(attribute)) if (is_invalid?(attribute) && !bootstrap_options.inline?)
165
172
  end
166
173
 
167
- radio_button_html
174
+ if bootstrap_options.inline?
175
+ radio_button_html
176
+ else
177
+ if bootstrap_options.layout_horizontal?
178
+ tag.div(class: field_wrapper_classes(bootstrap_options)) do
179
+ tag.div(class: radio_button_container_classes(bootstrap_options)) do
180
+ radio_button_html
181
+ end
182
+ end
183
+ else
184
+ radio_button_html
185
+ end
186
+ end
168
187
  end
169
188
 
170
189
  def collection_check_boxes(attribute, collection, value_method, text_method, options = {}, html_options = {}, &block)
@@ -176,7 +195,8 @@ module RailsBootstrapForm
176
195
  input_value = value_method.respond_to?(:call) ? value_method.call(object) : object.send(value_method)
177
196
  input_options = {
178
197
  bootstrap_form: {
179
- label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method)
198
+ label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method),
199
+ inline: true
180
200
  }
181
201
  }.deep_merge!(options)
182
202
 
@@ -184,7 +204,7 @@ module RailsBootstrapForm
184
204
  end
185
205
 
186
206
  if options.delete(:include_hidden) { true }
187
- inputs.prepend hidden_field(attribute, value: "", multiple: true)
207
+ inputs.prepend(hidden_field(attribute, value: "", multiple: options[:multiple]))
188
208
  end
189
209
 
190
210
  field_wrapper_builder(attribute, options, html_options) do
@@ -201,7 +221,8 @@ module RailsBootstrapForm
201
221
  input_value = value_method.respond_to?(:call) ? value_method.call(object) : object.send(value_method)
202
222
  input_options = {
203
223
  bootstrap_form: {
204
- label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method)
224
+ label_text: text_method.respond_to?(:call) ? text_method.call(object) : object.send(text_method),
225
+ inline: true
205
226
  }
206
227
  }.deep_merge!(options)
207
228
 
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.5.2".freeze
6
+ VERSION = "0.6.0".freeze
7
7
  REQUIRED_RAILS_VERSION = "~> 7.0".freeze
8
8
  end
@@ -17,6 +17,7 @@ module RailsBootstrapForm
17
17
  autoload :FieldWrapperBuilder
18
18
  autoload :InputGroupBuilder
19
19
  autoload :Inputs
20
+ autoload :Helpers
20
21
  end
21
22
 
22
23
  class << self
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.2
4
+ version: 0.6.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-21 00:00:00.000000000 Z
11
+ date: 2023-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: generator_spec
@@ -65,6 +65,7 @@ files:
65
65
  - demo/app/views/layouts/application.html.erb
66
66
  - demo/app/views/users/_form.html.erb
67
67
  - demo/app/views/users/_form_without_bootstrap_helpers.html.erb
68
+ - demo/app/views/users/_horizontal_form.html.erb
68
69
  - demo/app/views/users/_vertical_form.html.erb
69
70
  - demo/app/views/users/edit.html.erb
70
71
  - demo/app/views/users/index.html.erb
@@ -120,9 +121,9 @@ files:
120
121
  - lib/rails_bootstrap_form/configuration.rb
121
122
  - lib/rails_bootstrap_form/engine.rb
122
123
  - lib/rails_bootstrap_form/field_wrapper_builder.rb
124
+ - lib/rails_bootstrap_form/helpers.rb
123
125
  - lib/rails_bootstrap_form/input_group_builder.rb
124
126
  - lib/rails_bootstrap_form/inputs.rb
125
- - lib/rails_bootstrap_form/inputs/base.rb
126
127
  - lib/rails_bootstrap_form/version.rb
127
128
  - sig/rails_bootstrap_form.rbs
128
129
  homepage: https://github.com/shivam091/rails_bootstrap_form
@@ -1,28 +0,0 @@
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