rails_bootstrap_form 0.2.0 → 0.2.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: 7902b4c3c6edd102d01a2008a8d69a639b6afde3a93997c202e9267dcda93e4b
4
- data.tar.gz: 4e65ba157ac848d45093d2aeda0673e840335bafd24b5861b0551dddc6cd130b
3
+ metadata.gz: eeb7afe12b186dfa741279347931a85c33ff4950dcb691d6089e18767d5edb80
4
+ data.tar.gz: 18787c6cfd1ad0c3156f1611395551fb5c9c41d1e17983ac1393b7987d099455
5
5
  SHA512:
6
- metadata.gz: 1641aaa9fa84627e32bd9d5d59c7bfb6c89f12fd7498a6ed443309125ee11b4a79a8f7636b55da81e28a003146e00707c89df0d10c2e2d58957a975a035af3dc
7
- data.tar.gz: 9ecea91f9b5c2af60282eb938e957e18cbfcbc7b089341b126db05728cdb44dd41cb4af9632e1cf6e6a14b6146a3b5c8841af3cd3c95a24b7c77af8dabcd6d10
6
+ metadata.gz: 7de97d7da63f5742b0db6816e908335540c1b66eaab7509bca8257005dc30e7bb2dd7f1974d3f7d0cf45dcdb909e9644c4cd29bddd7de6450bad05e2216f7c64
7
+ data.tar.gz: 97997858453d5b926fb23a8a64fe36294d5cf08c7d164a8a984eb5b42ee5d956451908567d4f3473bf1cd369b2570119bb211c8071f26096e78e3b90852bdc54
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.2.0)
4
+ rails_bootstrap_form (0.2.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -6,11 +6,44 @@
6
6
  en: {
7
7
  activerecord: {
8
8
  attributes: {
9
-
9
+ country: {
10
+ name: "Name",
11
+ },
12
+ fruit: {
13
+ name: "Name",
14
+ },
15
+ skill: {
16
+ name: "Name",
17
+ },
18
+ user: {
19
+ name: "Name",
20
+ email: "Email address",
21
+ password: "Password",
22
+ password_confirmation: "Confirm password",
23
+ mobile_number: "Mobile number",
24
+ blog_url: "Blog URL",
25
+ birth_date: "Birth date",
26
+ fruit_id: "Favorite fruit",
27
+ terms: "I accept terms and conditions",
28
+ excellence: "Excellence",
29
+ },
30
+ user_skill: {
31
+ user_id: "User",
32
+ skill_id: "Skill"
33
+ },
34
+ address: {
35
+ street: "Street",
36
+ state: "State",
37
+ city: "City",
38
+ postal_code: "Postal code",
39
+ country_id: "Country"
40
+ },
10
41
  },
11
42
  help_texts: {
12
-
43
+ user: {
44
+ email: "Please use official email address"
45
+ }
13
46
  },
14
- },
47
+ }
15
48
  }
16
49
  }
@@ -4,6 +4,11 @@
4
4
 
5
5
  module RailsBootstrapForm
6
6
  class BootstrapFormBuilder < ActionView::Helpers::FormBuilder
7
+
8
+ include RailsBootstrapForm::FieldWrapperBuilder
9
+ include RailsBootstrapForm::Components
10
+ include RailsBootstrapForm::Inputs
11
+
7
12
  delegate :capture, :concat, :tag, to: :@template
8
13
 
9
14
  attr_accessor :bootstrap_form_options
@@ -20,6 +20,41 @@ module RailsBootstrapForm
20
20
  # It can also be "floating" if field should have floating labels.
21
21
  attr_accessor :layout
22
22
 
23
+ # A CSS class that will be applied to all form fields and it can be overridden
24
+ # by the `BootstrapFormOptions` object. Default is `form-control`.
25
+ attr_accessor :field_class
26
+
27
+ # An additional CSS class that will be added along with the existing
28
+ # `field_class` of the field. Default is nil.
29
+ attr_accessor :additional_field_class
30
+
31
+ # Describes help text for the HTML field. Help text is automatically read
32
+ # from translation file. If you want to customize it, you can pass string.
33
+ # You can also set it false if you do not want help text displayed.
34
+ # Default is nil.
35
+ attr_accessor :help_text
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
+
23
58
  def initialize(options = {})
24
59
  set_defaults
25
60
  set_bootstrap_form_options(options)
@@ -64,6 +99,18 @@ module RailsBootstrapForm
64
99
 
65
100
  def set_defaults
66
101
  @layout = "vertical"
102
+
103
+ @field_class = "form-control"
104
+ @additional_field_class = nil
105
+
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
67
114
  end
68
115
 
69
116
  private :set_defaults
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Components
7
+ module HelpText
8
+ extend ActiveSupport::Concern
9
+
10
+ def self.included(base_class)
11
+ def help_text(attribute, bootstrap_options)
12
+ return if bootstrap_options.help_text == false
13
+
14
+ help_text = (bootstrap_options.help_text || scoped_help_text(attribute))
15
+
16
+ tag.span(help_text, class: "form-text text-muted") if help_text.present?
17
+ end
18
+
19
+ def object_class
20
+ if !object.class.is_a?(ActiveModel::Naming) &&
21
+ object.respond_to?(:klass) && object.klass.is_a?(ActiveModel::Naming)
22
+ object.klass
23
+ else
24
+ object.class
25
+ end
26
+ end
27
+
28
+ def partial_scope
29
+ if object_class.respond_to?(:model_name)
30
+ object_class.model_name.name
31
+ else
32
+ object_class.name
33
+ end
34
+ end
35
+
36
+ def scoped_help_text(attribute)
37
+ translation_scope = "activerecord.help_texts.#{partial_scope.underscore}"
38
+
39
+ help_text = translated_help_text(attribute, translation_scope).presence
40
+
41
+ help_text
42
+ end
43
+
44
+ def translated_help_text(attribute, scope)
45
+ ActiveSupport::SafeBuffer.new(I18n.t(attribute, scope: scope, default: ""))
46
+ end
47
+
48
+ private :help_text, :partial_scope, :object_class, :scoped_help_text
49
+ :translated_help_text
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
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.flatten.compact
24
+ end
25
+
26
+ def label_text(attribute, bootstrap_options)
27
+ bootstrap_options.label_text || object&.class.try(:human_attribute_name, attribute)
28
+ end
29
+
30
+ private :label, :label_classes, :label_text
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,15 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Components
7
+ extend ActiveSupport::Autoload
8
+
9
+ autoload :HelpText
10
+ autoload :Labels
11
+
12
+ include HelpText
13
+ include Labels
14
+ end
15
+ end
@@ -0,0 +1,53 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module FieldWrapperBuilder
7
+ def field_wrapper_builder(attribute, options, html_options = nil, &block)
8
+ bootstrap_options = bootstrap_form_options.scoped(options.delete(:bootstrap_form))
9
+
10
+ field_options = field_css_options(attribute, bootstrap_options, options, html_options.try(:symbolize_keys!))
11
+
12
+ field_wrapper(attribute, bootstrap_options, field_options, &block)
13
+ end
14
+
15
+ def field_wrapper(attribute, bootstrap_options, options, &block)
16
+ label = label(attribute, bootstrap_options)
17
+ help_text = help_text(attribute, bootstrap_options)
18
+
19
+ tag.div(class: field_wrapper_classes) do
20
+ concat(label)
21
+ concat(capture(&block))
22
+ concat(help_text)
23
+ end
24
+ end
25
+
26
+ def field_wrapper_classes
27
+ classes = [form_wrapper_default_class]
28
+ classes.flatten.compact
29
+ end
30
+
31
+ def field_wrapper_options
32
+ end
33
+
34
+ def form_wrapper_default_class
35
+ "mb-3"
36
+ end
37
+
38
+ def field_css_options(attribute, bootstrap_options, options, html_options)
39
+ css_options = (html_options || options)
40
+
41
+ field_classes = [
42
+ bootstrap_options.field_class,
43
+ bootstrap_options.additional_field_class
44
+ ]
45
+ css_options[:class] = field_classes.flatten.compact
46
+
47
+ css_options
48
+ end
49
+
50
+ private :field_wrapper, :field_wrapper_classes, :form_wrapper_default_class,
51
+ :field_css_options
52
+ end
53
+ end
@@ -0,0 +1,43 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # -*- frozen_string_literal: true -*-
3
+ # -*- warn_indent: true -*-
4
+
5
+ module RailsBootstrapForm
6
+ module Inputs
7
+
8
+ FIELD_HELPERS = %i[
9
+ text_field
10
+ url_field
11
+ search_field
12
+ telephone_field
13
+ number_field
14
+ email_field
15
+ file_field
16
+ phone_field
17
+ password_field
18
+ text_area
19
+ date_field
20
+ time_field
21
+ datetime_field
22
+ datetime_local_field
23
+ month_field
24
+ week_field
25
+ ].freeze
26
+
27
+ FIELD_HELPERS.each do |field_tag_name|
28
+ define_method(field_tag_name) do |attribute, options = {}|
29
+ field_wrapper_builder(attribute, options) do
30
+ super(attribute, options)
31
+ end
32
+ end
33
+ end
34
+
35
+ def select(attribute, choices = nil, options = {}, html_options = {}, &block)
36
+ options = options.reverse_merge(bootstrap_form: {field_class: "form-select"})
37
+
38
+ field_wrapper_builder(attribute, options, html_options) do
39
+ super(attribute, choices, options, html_options, &block)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.2.0".freeze
6
+ VERSION = "0.2.2".freeze
7
7
  REQUIRED_RAILS_VERSION = "~> 7.0".freeze
8
8
  end
@@ -13,11 +13,15 @@ module RailsBootstrapForm
13
13
  autoload :Configuration
14
14
  autoload :BootstrapFormOptions
15
15
  autoload :BootstrapFormBuilder
16
+ autoload :FieldWrapperBuilder
17
+ autoload :Components
18
+ autoload :Inputs
16
19
  end
17
20
 
18
21
  class << self
19
22
  def eager_load!
20
23
  super
24
+ RailsBootstrapForm::Components.eager_load!
21
25
  end
22
26
 
23
27
  def config
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.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE (shivam091)
@@ -107,8 +107,13 @@ files:
107
107
  - lib/rails_bootstrap_form/action_view_extensions/bootstrap_form_helper.rb
108
108
  - lib/rails_bootstrap_form/bootstrap_form_builder.rb
109
109
  - lib/rails_bootstrap_form/bootstrap_form_options.rb
110
+ - lib/rails_bootstrap_form/components.rb
111
+ - lib/rails_bootstrap_form/components/help_text.rb
112
+ - lib/rails_bootstrap_form/components/labels.rb
110
113
  - lib/rails_bootstrap_form/configuration.rb
111
114
  - lib/rails_bootstrap_form/engine.rb
115
+ - lib/rails_bootstrap_form/field_wrapper_builder.rb
116
+ - lib/rails_bootstrap_form/inputs.rb
112
117
  - lib/rails_bootstrap_form/version.rb
113
118
  - sig/rails_bootstrap_form.rbs
114
119
  homepage: https://github.com/shivam091/rails_bootstrap_form