rails_bootstrap_form 0.3.0 → 0.4.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: 54148096404aa5a60bc550aca63ac09e15c0ec5b6bc9de993bd908879d366908
4
- data.tar.gz: 85a7d48bf6ccf530828395265f9aa41db7be3b1bf9fc6a8f83455614449ec02a
3
+ metadata.gz: 25354c2cb11c5971bf3435e2415ca37c0fcfabeeb45cd269a8dbecba7d582fd7
4
+ data.tar.gz: f695f139348b6fb1f2c054e90f79c6d5c53805ec047ac6d239df946a09039278
5
5
  SHA512:
6
- metadata.gz: d9569aeb686d7bef2a2c15018c46e14413c35ecb9eb18ce784cc4b074dcfdc2e5205e583cbf9ad85a6d98da8ab2939af772903c12c5d1a2d3bc0812bd908bf4f
7
- data.tar.gz: 6b7222a2477195f38db5b63aeddecf28777e8f1de3dcb17ddbcd3126c747aa61903d411bad1341976590ccfb92e1cab07c93dcb31c4e49e7743b607478acce8b
6
+ metadata.gz: ed91e2feb0354818a43161b165ed8bba83b8e29a9f3013d2a3f7df54e23158768332d2381f36132399dedb7531a65abcd9234a70814c9b05f63c6af3ed66c245
7
+ data.tar.gz: ea1cf098b34f86c6f5ca4bb1cfc6031a93f81865515e484f6322934e33673b9980c053ccf02db70d302feb520e8766998678505f3e406b805b426dada7d000ee
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rails_bootstrap_form (0.3.0)
4
+ rails_bootstrap_form (0.4.0)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
@@ -14,3 +14,9 @@ label.required::after {
14
14
  background-position: right 0.5rem center, center right 2rem !important;
15
15
  background-size: 24px 24px, calc(0.75em + 0.375rem) calc(0.75em + 0.375rem) !important;
16
16
  }
17
+ .rails-bootstrap-forms-date-select select,
18
+ .rails-bootstrap-forms-time-select select,
19
+ .rails-bootstrap-forms-datetime-select select {
20
+ display: inline-block;
21
+ width: auto;
22
+ }
@@ -3,6 +3,9 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  class User < ApplicationRecord
6
+ validates :name, presence: true, length: {in: 2..50}
7
+ validates :email, presence: true
8
+
6
9
  has_one :address, dependent: :destroy
7
10
 
8
11
  has_many :user_skills, dependent: :destroy
@@ -4,7 +4,7 @@
4
4
  </div>
5
5
  <div class="card-body">
6
6
  <%= bootstrap_form_for @user, bootstrap_form: {} do |form| %>
7
- <%= form.text_field :name, autocomplete: "new-name", required: true, bootstrap_form: {} %>
7
+ <%= form.text_field :name, autocomplete: "new-name", bootstrap_form: {} %>
8
8
  <%= form.text_field :email, autocomplete: "new-email", bootstrap_form: {} %>
9
9
  <%= form.text_field :password, autocomplete: "new-password" %>
10
10
  <%= form.phone_field :mobile_number %>
@@ -12,8 +12,8 @@
12
12
  <%= form.check_box :terms %>
13
13
  <%= form.range_field :excellence %>
14
14
  <%= form.url_field :blog_url %>
15
- <%= form.url_field :favorite_color %>
16
- <%= form.select :fruit_id, options_for_select(::Fruit.pluck(:name, :id), form.object.fruit_id), include_blank: "Select Favorite Fruit" %>
15
+ <%= form.color_field :favorite_color %>
16
+ <%= form.select :fruit_id, options_for_select(::Fruit.pluck(:name, :id), form.object.fruit_id), {include_blank: "Select Favorite Fruit"} %>
17
17
  <%= form.collection_check_boxes :skill_ids, ::Skill.all, :id, :name do |b| %>
18
18
  <%= b.check_box + b.text %>
19
19
  <% end %>
@@ -22,7 +22,7 @@
22
22
  <%= address_form.text_field :state %>
23
23
  <%= address_form.text_field :city %>
24
24
  <%= address_form.text_field :postal_code %>
25
- <%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id), {include_blank: "Select Country", bootstrap: {}} %>
25
+ <%= address_form.select :country_id, options_for_select(::Country.pluck(:name, :id), address_form.object.country_id), {include_blank: "Select Country", bootstrap_form: {}} %>
26
26
  <% end %>
27
27
  <div class="mt-3">
28
28
  <%= form.submit "Register", class: "btn btn-primary" %>
@@ -26,6 +26,7 @@
26
26
  fruit_id: "Favorite fruit",
27
27
  terms: "I accept terms and conditions",
28
28
  excellence: "Excellence",
29
+ skill_ids: "Skills",
29
30
  },
30
31
  user_skill: {
31
32
  user_id: "User",
@@ -25,6 +25,10 @@ module RailsBootstrapForm
25
25
  options[:html].reverse_merge!(RailsBootstrapForm.config.default_form_attributes)
26
26
  end
27
27
 
28
- private :apply_default_form_options
28
+ def control_specific_class(field_tag_name)
29
+ "rails-bootstrap-forms-#{field_tag_name.to_s.tr("_", "-")}"
30
+ end
31
+
32
+ private :apply_default_form_options, :control_specific_class
29
33
  end
30
34
  end
@@ -82,6 +82,10 @@ module RailsBootstrapForm
82
82
  # Default is false.
83
83
  attr_accessor :floating
84
84
 
85
+ # Default CSS class that will be applied to all static fields.
86
+ # Default is `form-control-plaintext`.
87
+ attr_accessor :static_field_class
88
+
85
89
  def initialize(options = {})
86
90
  set_defaults
87
91
  set_bootstrap_form_options(options)
@@ -140,6 +144,8 @@ module RailsBootstrapForm
140
144
  @additional_label_class = nil
141
145
 
142
146
  @floating = false
147
+
148
+ @static_field_class = "form-control-plaintext"
143
149
  end
144
150
 
145
151
  private :set_defaults
@@ -10,13 +10,17 @@ module RailsBootstrapForm
10
10
  def input_group_wrapper(attribute, bootstrap_options, &block)
11
11
  input = capture(&block) || ActiveSupport::SafeBuffer.new
12
12
 
13
- prepend = attach_input(bootstrap_options, :prepend)
14
- append = attach_input(bootstrap_options, :append)
13
+ if input_group_required?(bootstrap_options)
14
+ prepend = attach_input(bootstrap_options, :prepend)
15
+ append = attach_input(bootstrap_options, :append)
15
16
 
16
- input = prepend + input + append
17
- input += generate_error(attribute)
17
+ input = prepend + input + append
18
+ input += generate_error(attribute)
18
19
 
19
- input = tag.div(input, class: input_group_classes(attribute, bootstrap_options))
20
+ input = tag.div(input, class: input_group_classes(attribute, bootstrap_options))
21
+ else
22
+ input += generate_error(attribute)
23
+ end
20
24
 
21
25
  input
22
26
  end
@@ -42,8 +46,15 @@ module RailsBootstrapForm
42
46
  tag.span(content.html_safe, class: "input-group-text")
43
47
  end
44
48
 
49
+ def input_group_required?(bootstrap_options)
50
+ [
51
+ bootstrap_options.prepend,
52
+ bootstrap_options.append
53
+ ].any?(&:present?)
54
+ end
55
+
45
56
  private :input_group_wrapper, :input_group_classes, :attach_input,
46
- :input_group_content
57
+ :input_group_content, :input_group_required?
47
58
  end
48
59
  end
49
60
  end
@@ -24,6 +24,12 @@ module RailsBootstrapForm
24
24
  week_field
25
25
  ].freeze
26
26
 
27
+ DATE_SELECT_HELPERS = %i[
28
+ date_select
29
+ time_select
30
+ datetime_select
31
+ ].freeze
32
+
27
33
  FIELD_HELPERS.each do |field_tag_name|
28
34
  define_method(field_tag_name) do |attribute, options = {}|
29
35
  field_wrapper_builder(attribute, options) do
@@ -32,6 +38,18 @@ module RailsBootstrapForm
32
38
  end
33
39
  end
34
40
 
41
+ DATE_SELECT_HELPERS.each do |field_tag_name|
42
+ define_method(field_tag_name) do |attribute, options = {}, html_options = {}, &block|
43
+ options = options.reverse_merge(bootstrap_form: {field_class: "form-select"})
44
+
45
+ field_wrapper_builder(attribute, options, html_options) do
46
+ tag.div(class: control_specific_class(field_tag_name)) do
47
+ super(attribute, options, html_options)
48
+ end
49
+ end
50
+ end
51
+ end
52
+
35
53
  def select(attribute, choices = nil, options = {}, html_options = {}, &block)
36
54
  options = options.reverse_merge(bootstrap_form: {field_class: "form-select"})
37
55
 
@@ -39,5 +57,60 @@ module RailsBootstrapForm
39
57
  super(attribute, choices, options, html_options, &block)
40
58
  end
41
59
  end
60
+
61
+ def collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {})
62
+ options = options.reverse_merge(bootstrap_form: {field_class: "form-select"})
63
+
64
+ field_wrapper_builder(attribute, options, html_options) do
65
+ super(attribute, collection, value_method, text_method, options, html_options)
66
+ end
67
+ end
68
+
69
+ def range_field(attribute, options = {})
70
+ options = options.reverse_merge(bootstrap_form: {field_class: "form-range"})
71
+
72
+ field_wrapper_builder(attribute, options) do
73
+ super(attribute, options)
74
+ end
75
+ end
76
+
77
+ def color_field(attribute, options = {})
78
+ options = options.reverse_merge(bootstrap_form: {field_class: "form-control form-control-color"})
79
+
80
+ field_wrapper_builder(attribute, options) do
81
+ super(attribute, options)
82
+ end
83
+ end
84
+
85
+ def time_zone_select(attribute, priority_zones = nil, options = {}, html_options = {})
86
+ options = options.reverse_merge(bootstrap_form: {field_class: "form-select"})
87
+
88
+ field_wrapper_builder(attribute, options, html_options) do
89
+ super(attribute, priority_zones, options, html_options)
90
+ end
91
+ end
92
+
93
+ def hidden_field(attribute, options = {})
94
+ options[:value] = object.send(attribute) unless options.key?(:value)
95
+
96
+ super(attribute, options)
97
+ end
98
+
99
+ def static_field(*args)
100
+ options = args.extract_options!
101
+ attribute = args.first
102
+
103
+ static_options = options.merge(
104
+ readonly: true,
105
+ disabled: true,
106
+ bootstrap_form: {
107
+ field_class: bootstrap_form_options.static_field_class
108
+ }
109
+ )
110
+
111
+ static_options[:value] = object.send(attribute) unless options.key?(:value)
112
+
113
+ text_field(attribute, static_options)
114
+ end
42
115
  end
43
116
  end
@@ -3,6 +3,6 @@
3
3
  # -*- warn_indent: true -*-
4
4
 
5
5
  module RailsBootstrapForm
6
- VERSION = "0.3.0".freeze
6
+ VERSION = "0.4.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.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harshal LADHE (shivam091)