rails_bootstrap_form 0.2.1 → 0.2.3

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: 1e721e3897b40c0457a2cd5cfdd2d70be38f43b1c707f103e3799ea03a9c398d
4
- data.tar.gz: baef33d0746182ad54d2dd1b0ca0b4e827bb1ff4ad70762fca8c44d4826f94c8
3
+ metadata.gz: 58960cc1bb39e990327bb9653961f25761070edbd1d8cd0ddb281506fc0cf564
4
+ data.tar.gz: 6d19cd1958e1667a51ad91d60a09144934ddfaf3b5bdbc21972bfb00ed2cd2cb
5
5
  SHA512:
6
- metadata.gz: 8f1916dfbb809f724be629beeff70e6e7ca287967bf769dc9d5d3501f3ee00beb641ca73966661d33d3293972090667390672a4d444c32318f5a9ca74863822a
7
- data.tar.gz: 6357308e7ddd7df6667d9a36a96cd506039526c258438bfa19583cc898f36221ffff2f96c4dcdb06f34fb75b39c9fbebfd3a9ced43ab5257f5323b61921bd8e9
6
+ metadata.gz: 1baea47dd00f08b64d4f7c156748b4dace0e30cdf16f95ebd568dd6db4de2d869748728a5d11d4844dfce943873136767d30cdc0c5262b2ac4c7a6dc898c6188
7
+ data.tar.gz: bf4c3178074b47e4ab37f7cc0ec0649d681e82c6a44f80196a071312235ed6503cc493385c08fde619dc1bb92636d8f0bc3f19b6ecca8599e925a150fa1a80ba
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.2.1)
4
+ rails_bootstrap_form (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -1 +1,10 @@
1
-
1
+ label.is-invalid, .invalid-feedback {
2
+ color: var(--bs-danger);
3
+ }
4
+ label.required::after {
5
+ color: var(--bs-danger);
6
+ content: "*";
7
+ padding-left: .25rem;
8
+ top: -2px;
9
+ font-weight: bolder;
10
+ }
@@ -34,6 +34,27 @@ module RailsBootstrapForm
34
34
  # Default is nil.
35
35
  attr_accessor :help_text
36
36
 
37
+ # An option to override automatically generated label text.
38
+ attr_accessor :label_text
39
+
40
+ # An option to custmize whether the label is to be displayed or not.
41
+ attr_accessor :skip_label
42
+
43
+ # An option to customize whether the label is only visible to screen readers.
44
+ attr_accessor :hide_label
45
+
46
+ # The CSS class that will be used when the label is only accessible by screen
47
+ # readers. Default is `visually-hidden`
48
+ attr_accessor :hide_class
49
+
50
+ # Default CSS class that will be applied to all label tags.
51
+ # Default is `form-label`.
52
+ attr_accessor :label_class
53
+
54
+ # An additional CSS class that will be added along with the existing
55
+ # `label_class` of the label. Default is nil.
56
+ attr_accessor :additional_label_class
57
+
37
58
  def initialize(options = {})
38
59
  set_defaults
39
60
  set_bootstrap_form_options(options)
@@ -83,6 +104,13 @@ module RailsBootstrapForm
83
104
  @additional_field_class = nil
84
105
 
85
106
  @help_text = nil
107
+
108
+ @label_text = ""
109
+ @skip_label = false
110
+ @hide_label = false
111
+ @hide_class = "visually-hidden"
112
+ @label_class = "form-label"
113
+ @additional_label_class = nil
86
114
  end
87
115
 
88
116
  private :set_defaults
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Components
7
+ module Labels
8
+ extend ActiveSupport::Concern
9
+
10
+ def self.included(base_class)
11
+ def label(attribute, bootstrap_options)
12
+ unless bootstrap_options.skip_label
13
+ label_class = label_classes(attribute, bootstrap_options)
14
+ label_text = label_text(attribute, bootstrap_options)
15
+
16
+ super(attribute, label_text, class: label_class)
17
+ end
18
+ end
19
+
20
+ def label_classes(attribute, bootstrap_options)
21
+ classes = [bootstrap_options.label_class, bootstrap_options.additional_label_class]
22
+ classes << bootstrap_options.hide_class if bootstrap_options.hide_label
23
+ classes << "required" if is_attribute_required?(attribute)
24
+ classes.flatten.compact
25
+ end
26
+
27
+ def label_text(attribute, bootstrap_options)
28
+ bootstrap_options.label_text || object&.class.try(:human_attribute_name, attribute)
29
+ end
30
+
31
+ private :label, :label_classes, :label_text
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,64 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Components
7
+ module RequiredField
8
+ extend ActiveSupport::Concern
9
+
10
+ def self.included(base_class)
11
+ def is_field_required?(attribute, options)
12
+ return false unless attribute
13
+
14
+ if options.key?(:required)
15
+ options[:required]
16
+ else
17
+ is_attribute_required?(attribute)
18
+ end
19
+ end
20
+
21
+ def required_field_options(options, attribute)
22
+ required = is_field_required?(attribute, options)
23
+
24
+ {}.tap do |option|
25
+ option[:aria] = {required: true} if required
26
+ option[:required] = required
27
+ end
28
+ end
29
+
30
+ def is_attribute_required?(attribute)
31
+ return false unless attribute
32
+
33
+ target = object.instance_of?(Class) ? object : object.class
34
+ return false unless target.respond_to?(:validators_on)
35
+
36
+ has_presence_validator?(target_validators(target, attribute)) ||
37
+ is_required_association?(target, attribute)
38
+ end
39
+
40
+ def target_validators(target, attribute)
41
+ target.validators_on(attribute).map(&:class)
42
+ end
43
+
44
+ def has_presence_validator?(target_validators)
45
+ target_validators.include?(ActiveModel::Validations::PresenceValidator) ||
46
+ (defined?(ActiveRecord::Validations::PresenceValidator) &&
47
+ target_validators.include?(ActiveRecord::Validations::PresenceValidator))
48
+ end
49
+
50
+ def is_required_association?(target, attribute)
51
+ target.reflections.find do |name, a|
52
+ next unless a.is_a?(ActiveRecord::Reflection::BelongsToReflection)
53
+ next unless a.foreign_key == attribute.to_s
54
+
55
+ has_presence_validator?(target_validators(target, name))
56
+ end
57
+ end
58
+
59
+ private :is_field_required?, :required_field_options, :target_validators,
60
+ :has_presence_validator?, :is_required_association?
61
+ end
62
+ end
63
+ end
64
+ end
@@ -7,7 +7,11 @@ module RailsBootstrapForm
7
7
  extend ActiveSupport::Autoload
8
8
 
9
9
  autoload :HelpText
10
+ autoload :Labels
11
+ autoload :RequiredField
10
12
 
11
13
  include HelpText
14
+ include Labels
15
+ include RequiredField
12
16
  end
13
17
  end
@@ -13,9 +13,11 @@ module RailsBootstrapForm
13
13
  end
14
14
 
15
15
  def field_wrapper(attribute, bootstrap_options, options, &block)
16
+ label = label(attribute, bootstrap_options)
16
17
  help_text = help_text(attribute, bootstrap_options)
17
18
 
18
19
  tag.div(class: field_wrapper_classes) do
20
+ concat(label)
19
21
  concat(capture(&block))
20
22
  concat(help_text)
21
23
  end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.2.1".freeze
6
+ VERSION = "0.2.3".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.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE (shivam091)
@@ -109,6 +109,8 @@ files:
109
109
  - lib/rails_bootstrap_form/bootstrap_form_options.rb
110
110
  - lib/rails_bootstrap_form/components.rb
111
111
  - lib/rails_bootstrap_form/components/help_text.rb
112
+ - lib/rails_bootstrap_form/components/labels.rb
113
+ - lib/rails_bootstrap_form/components/required_field.rb
112
114
  - lib/rails_bootstrap_form/configuration.rb
113
115
  - lib/rails_bootstrap_form/engine.rb
114
116
  - lib/rails_bootstrap_form/field_wrapper_builder.rb