rails_bootstrap_form 0.6.1 → 0.7.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: 34303679b08cf0617516305e5b4e1f21d8c201ffcd9f4cc8eb90f26a587a7f7a
4
- data.tar.gz: 4e01e2321eb0318e222e802ce84e8c63648296c50e78de5de9f3c8e14be9570e
3
+ metadata.gz: ad6e14683d4d77bee3ee35855f75a8af8258bf8208f47504678e2a9d9b3be743
4
+ data.tar.gz: f0a40a462746c5990a5df46ef604fce57a43359cda481b42557ee5e182e65019
5
5
  SHA512:
6
- metadata.gz: c7f4349d0933eeea1f1965df83eb80ec0fd83392a3a47e2299214a797ec36e5c198e647d727969443779d60910741f91b49f88973a6ebcf6d93a29302f308b46
7
- data.tar.gz: adab966d0ca4b8064094e7e941296d95e75e594452384e9cb6e7370ca19fd288a4e9d9cea1e56dbf7981804ef58f9cafc1398d1e0203bb1eefb80004f27ab18d
6
+ metadata.gz: c4db5095b17363d92f5fe629d05a8cc1031e925aeecc461a113a905dd98e3b6b6bff3298c3154332b82160a3adfd72e358cef2578bc7ecb0af24296d0bdd9102
7
+ data.tar.gz: 603478636468cc3a2d379b6499a15674b21d990aed61b333b2aa3db356b96af703dc423b67c2fb1b0a4a74a1589c02bd11e0c11c91229d526a65c330e6bbda41
data/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ 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.7.0](https://github.com/shivam091/rails_bootstrap_form/compare/v0.6.2...v0.7.0) - 2023-05-24
8
+
9
+ ### What's new
10
+ - Added helper methods for rendering submit buttons on forms.
11
+
12
+ ## [0.6.2](https://github.com/shivam091/rails_bootstrap_form/compare/v0.6.1...v0.6.2) - 2023-05-24
13
+
14
+ ### What's fixed
15
+ - Added support for setting bootstrap options of parent form to `fields_for` ([#10](https://github.com/shivam091/rails_bootstrap_form/issues/10))
16
+ - Ignore skip_label option when field has floating labels ([#9](https://github.com/shivam091/rails_bootstrap_form/issues/9))
17
+
7
18
  ## [0.6.1](https://github.com/shivam091/rails_bootstrap_form/compare/v0.6.0...v0.6.1) - 2023-05-24
8
19
 
9
20
  ### What's changed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.6.1)
4
+ rails_bootstrap_form (0.7.0)
5
5
  actionpack (~> 7.0)
6
6
  activemodel (~> 7.0)
7
7
 
@@ -14,7 +14,7 @@
14
14
  <%= form.collection_radio_buttons :fruit_id, ::Fruit.all, :id, :name, {checked: form.object.fruit_id} %>
15
15
  <%= form.color_field :favorite_color %>
16
16
  <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name %>
17
- <%= form.fields_for :address, include_id: false, bootstrap_form: {layout: :horizontal} do |address_form| %>
17
+ <%= form.fields_for :address, include_id: false do |address_form| %>
18
18
  <%= address_form.text_area :street %>
19
19
  <%= address_form.text_field :state %>
20
20
  <%= address_form.grouped_collection_select :city, ::Country.includes(:cities), :cities, :name, :id, :name, {include_blank: "Select city"} %>
@@ -24,7 +24,7 @@
24
24
  <% end %>
25
25
  <%= form.check_box :terms, required: true %>
26
26
  <div class="mt-3">
27
- <%= form.submit "Register", class: "btn btn-primary" %>
27
+ <%= form.primary "Register" %>
28
28
  <%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
29
29
  </div>
30
30
  <% end %>
@@ -24,7 +24,7 @@
24
24
  <% end %>
25
25
  <%= form.check_box :terms, required: true %>
26
26
  <div class="mt-3">
27
- <%= form.submit "Register", class: "btn btn-primary" %>
27
+ <%= form.primary "Register" %>
28
28
  <%= link_to "Cancel", users_path, class: "btn btn-secondary" %>
29
29
  </div>
30
30
  <% end %>
@@ -15,16 +15,28 @@ module RailsBootstrapForm
15
15
  attr_accessor :bootstrap_form_options
16
16
 
17
17
  def initialize(object_name, object, template, options)
18
- @bootstrap_form_options = RailsBootstrapForm::BootstrapFormOptions.new(options.delete(:bootstrap_form))
18
+ @bootstrap_form_options = RailsBootstrapForm::BootstrapFormOptions.new(options[:bootstrap_form])
19
19
  apply_default_form_options(options)
20
20
  super(object_name, object, template, options)
21
21
  end
22
22
 
23
+ def fields_for(record_name, record_object = nil, fields_options = {}, &block)
24
+ fields_options = fields_for_options(record_object, fields_options)
25
+ record_object = nil if record_object.is_a?(Hash) && record_object.extractable_options?
26
+ super(record_name, record_object, fields_options, &block)
27
+ end
28
+
23
29
  def apply_default_form_options(options)
24
30
  options[:html] ||= {}
25
31
  options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
26
32
  end
27
33
 
28
- private :apply_default_form_options
34
+ def fields_for_options(record_object, fields_options)
35
+ field_options = record_object if record_object.is_a?(Hash) && record_object.extractable_options?
36
+ field_options = {bootstrap_form: options[:bootstrap_form]}.deep_merge!(field_options)
37
+ field_options
38
+ end
39
+
40
+ private :apply_default_form_options, :fields_for_options
29
41
  end
30
42
  end
@@ -119,6 +119,11 @@ module RailsBootstrapForm
119
119
  # The default value is `col-sm-10`.
120
120
  attr_accessor :field_col_wrapper_class
121
121
 
122
+ # Option to render submit button using `<button type="submit">` instead of
123
+ # `<input type="submit">`.
124
+ # The default value is `false`.
125
+ attr_accessor :render_as_button
126
+
122
127
  def initialize(options = {})
123
128
  set_defaults
124
129
  set_bootstrap_form_options(options)
@@ -136,10 +141,6 @@ module RailsBootstrapForm
136
141
  @layout.to_s == "vertical"
137
142
  end
138
143
 
139
- def inline?
140
- self.inline
141
- end
142
-
143
144
  # This will return a copy of `BootstrapFormOptions` object with new options set
144
145
  # that don't affect original object. This way we can have options specific
145
146
  # to a given form field. For example, we can change grid just for one field:
@@ -161,6 +162,10 @@ module RailsBootstrapForm
161
162
  end
162
163
  end
163
164
 
165
+ %i(inline floating switch skip_label hide_label render_as_button).each do |method|
166
+ define_method("#{method}?") { self.send(method) }
167
+ end
168
+
164
169
  def set_defaults
165
170
  @layout = "vertical"
166
171
 
@@ -191,6 +196,8 @@ module RailsBootstrapForm
191
196
  @label_col_class = "col-form-label"
192
197
  @label_col_wrapper_class = "col-sm-2"
193
198
  @field_col_wrapper_class = "col-sm-10"
199
+
200
+ @render_as_button = false
194
201
  end
195
202
 
196
203
  private :set_defaults
@@ -27,7 +27,7 @@ module RailsBootstrapForm
27
27
  end)
28
28
  end
29
29
  else
30
- if bootstrap_options.floating
30
+ if bootstrap_options.floating?
31
31
  tag.div(**field_wrapper_options(bootstrap_options)) do
32
32
  concat(input_group_wrapper(attribute, bootstrap_options) do
33
33
  tag.div(class: floating_label_classes(attribute)) do
@@ -82,7 +82,7 @@ module RailsBootstrapForm
82
82
  css_options[:class] = field_classes.flatten.compact
83
83
  css_options.merge!(required_field_options(attribute, options))
84
84
 
85
- if (bootstrap_options.floating && !bootstrap_options.layout_horizontal?)
85
+ if (bootstrap_options.floating? && !bootstrap_options.layout_horizontal?)
86
86
  css_options[:placeholder] ||= label_text(attribute, bootstrap_options)
87
87
  end
88
88
 
@@ -0,0 +1,39 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Helpers
7
+ module Buttons
8
+ extend ActiveSupport::Concern
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)
13
+ bootstrap_options = bootstrap_form_options.scoped(options.delete(:bootstrap_form))
14
+
15
+ if (bootstrap_options.render_as_button? || block)
16
+ button(value, options, &block)
17
+ else
18
+ submit(value, options)
19
+ end
20
+ end
21
+
22
+ def secondary(value = nil, options = {}, &block)
23
+ add_css_class!(options, "btn btn-secondary")
24
+ render_button(value, options, &block)
25
+ end
26
+
27
+ def primary(value = nil, options = {}, &block)
28
+ add_css_class!(options, "btn btn-primary")
29
+ render_button(value, options, &block)
30
+ end
31
+
32
+ def danger(value = nil, options = {}, &block)
33
+ add_css_class!(options, "btn btn-danger")
34
+ render_button(value, options, &block)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -9,7 +9,7 @@ module RailsBootstrapForm
9
9
 
10
10
  def self.included(base_class)
11
11
  def check_box_label(attribute, checked_value, options, bootstrap_options, &block)
12
- unless bootstrap_options.skip_label
12
+ unless bootstrap_options.skip_label?
13
13
  label_options = {
14
14
  class: check_box_label_class(attribute, bootstrap_options, options)
15
15
  }
@@ -54,13 +54,13 @@ module RailsBootstrapForm
54
54
  classes = Array("form-check-label") << bootstrap_options.additional_label_class
55
55
  classes << "required" if is_field_required?(attribute, options) && !bootstrap_options.inline?
56
56
  classes << "is-invalid" if is_invalid?(attribute)
57
- classes << bootstrap_options.hide_class if bootstrap_options.hide_label
57
+ classes << bootstrap_options.hide_class if bootstrap_options.hide_label?
58
58
  classes.flatten.compact
59
59
  end
60
60
 
61
61
  def check_box_wrapper_class(bootstrap_options)
62
62
  classes = Array("form-check")
63
- classes << "form-switch" if bootstrap_options.switch
63
+ classes << "form-switch" if bootstrap_options.switch?
64
64
  classes << "form-check-inline" if bootstrap_options.inline?
65
65
  classes << "mb-3" unless (bootstrap_options.layout_horizontal? || bootstrap_options.inline?)
66
66
  classes.flatten.compact
@@ -9,7 +9,7 @@ module RailsBootstrapForm
9
9
 
10
10
  def self.included(base_class)
11
11
  def draw_label(attribute, options, bootstrap_options)
12
- unless bootstrap_options.skip_label
12
+ unless bootstrap_options.skip_label? && !bootstrap_options.floating?
13
13
  label_options = {
14
14
  class: label_classes(attribute, bootstrap_options)
15
15
  }
@@ -28,7 +28,7 @@ module RailsBootstrapForm
28
28
  bootstrap_options.label_class
29
29
  end
30
30
  classes << bootstrap_options.additional_label_class
31
- classes << bootstrap_options.hide_class if bootstrap_options.hide_label
31
+ classes << bootstrap_options.hide_class if bootstrap_options.hide_label?
32
32
  classes << "required" if is_attribute_required?(attribute)
33
33
  classes << "is-invalid" if is_invalid?(attribute)
34
34
  classes.flatten.compact
@@ -9,7 +9,7 @@ module RailsBootstrapForm
9
9
 
10
10
  def self.included(base_class)
11
11
  def radio_button_label(attribute, value, options, bootstrap_options)
12
- unless bootstrap_options.skip_label
12
+ unless bootstrap_options.skip_label?
13
13
  label_options = {
14
14
  value: value,
15
15
  class: radio_button_label_class(attribute, bootstrap_options, options)
@@ -51,7 +51,7 @@ module RailsBootstrapForm
51
51
  classes = Array("form-check-label") << bootstrap_options.additional_label_class
52
52
  classes << "required" if is_field_required?(attribute, options) && !bootstrap_options.inline?
53
53
  classes << "is-invalid" if is_invalid?(attribute)
54
- classes << bootstrap_options.hide_class if bootstrap_options.hide_label
54
+ classes << bootstrap_options.hide_class if bootstrap_options.hide_label?
55
55
  classes.flatten.compact
56
56
  end
57
57
 
@@ -13,6 +13,7 @@ module RailsBootstrapForm
13
13
  autoload :Errors
14
14
  autoload :CheckBox
15
15
  autoload :RadioButton
16
+ autoload :Buttons
16
17
 
17
18
  include HelpText
18
19
  include Labels
@@ -20,6 +21,7 @@ module RailsBootstrapForm
20
21
  include Errors
21
22
  include CheckBox
22
23
  include RadioButton
24
+ include Buttons
23
25
 
24
26
  def self.included(base_class)
25
27
  def collection_input_checked?(checked, obj, input_value)
@@ -39,7 +41,19 @@ module RailsBootstrapForm
39
41
  label_col_wrapper_class.gsub(/\bcol-(\w+)-(\d)\b/, 'offset-\1-\2')
40
42
  end
41
43
 
42
- private :collection_input_checked?, :control_specific_class, :is_size_valid?
44
+ def add_css_class!(options, css_class)
45
+ the_class = [options[:class], css_class].compact
46
+ options[:class] = the_class if the_class.present?
47
+ end
48
+
49
+ def remove_css_class!(options, css_class)
50
+ the_class = options[:class].to_s.split(" ")
51
+ options[:class] = (the_class - [css_class]).compact.join(" ")
52
+ options.delete(:class) if options[:class].blank?
53
+ end
54
+
55
+ private :collection_input_checked?, :control_specific_class, :is_size_valid?,
56
+ :add_css_class!, :remove_css_class!
43
57
  end
44
58
  end
45
59
  end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.6.1".freeze
6
+ VERSION = "0.7.0".freeze
7
7
  REQUIRED_RAILS_VERSION = "~> 7.0".freeze
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_bootstrap_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE (shivam091)
@@ -143,6 +143,7 @@ files:
143
143
  - lib/rails_bootstrap_form/engine.rb
144
144
  - lib/rails_bootstrap_form/field_wrapper_builder.rb
145
145
  - lib/rails_bootstrap_form/helpers.rb
146
+ - lib/rails_bootstrap_form/helpers/buttons.rb
146
147
  - lib/rails_bootstrap_form/helpers/check_box.rb
147
148
  - lib/rails_bootstrap_form/helpers/errors.rb
148
149
  - lib/rails_bootstrap_form/helpers/help_text.rb