labelled_form 0.2.4 → 0.3.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: 2b58e84e22bb1f12df2f02c2e136989e5be00ecb3eba4581f677d52f021fc471
4
- data.tar.gz: acb13a7a2520c75ced8bcaaef5f391e3360c7120ca22c65b89b8b0594030a3c8
3
+ metadata.gz: e8726fb2089a7a687b5975773e7992cedc323e955b87db3d1e236fb1791cba33
4
+ data.tar.gz: 0e9b1cd1b765656dc8bc00f7895fca70c1c4fa5154cc81d7b8d74c163d643812
5
5
  SHA512:
6
- metadata.gz: f46c9d747077fab1a02e9d578d823bb2d65c4630c128d68692f6127731053bceab8b7cc41a015711a0f79a70cbaff88bcab4aad34897a483f833508b3eaed483
7
- data.tar.gz: fc2498ec4eaca65425d8911b6c3676df43504c734685df7b090d1d336d29b5bf784f5935031b725d5c1cda88eabd22d2defa4e0635dd45a29d9035dfa96590b8
6
+ metadata.gz: 3c3c559e1401e7c0f242f5ac0da78a026478a6da08734a9153ae36c4d4dcdcd516e30b891cc8e928c02d5157f48438c6efd025b4275a7ad91958a0866673dc1c
7
+ data.tar.gz: 343fa9c7add0f5100e8d1d32e26e00b657ef500814c8238ca528af6382fb526a44f0037f274c5cbd19a37a80137ebec14c395f97eb3c88ecbfc8298cff210569
data/README.md CHANGED
@@ -7,29 +7,43 @@ Adds `label:` option to Rails form helpers.
7
7
  Works like you'd expect, or at least, works like I'd expect! Examples:
8
8
 
9
9
  ```ruby
10
- form_with :user do |form|
10
+ = form_with :user do |form|
11
11
 
12
12
  # Prepends label to field and infers label text
13
- form.text_field :email, label: true
13
+ = form.text_field :email, label: true
14
14
  # <label for="user_email">Email</label>
15
15
  # <input type="text" name="user[email]" id="user_email">
16
16
 
17
17
  # Supports specifying different label text
18
- form.text_field :name, label: "Full name"
18
+ = form.text_field :name, label: "Full name"
19
19
  # <label for="user_name">Full name</label>
20
20
  # <input type="text" name="user[name]" id="user_name">
21
21
 
22
22
  # Appends label to field for radio buttons and checkboxes
23
- form.radio_button :gender, "Male", label: true
23
+ = form.radio_button :gender, "Male", label: true
24
24
  # <input type="radio" name="user[gender]" value="Male" id="user_gender_male">
25
25
  # <label for="user_gender_male">Male</label>
26
26
 
27
+ # Specify label class
28
+ = form.text_field :email, label_class: "hidden"
29
+ # <label for="user_email" class="hidden">Email</label>
30
+ # <input type="text" name="user[email]" id="user_email">
31
+
32
+ # Append colon to label
33
+ = form.text_field :email, label_with_colon: true
34
+ # <label for="user_email">Email:</label>
35
+ # <input type="text" name="user[email]" id="user_email">
36
+
37
+ # Wrap whole thing in a <dfn>
38
+ = form.text_field :email, wrap_in_dfn: true
39
+ # <dfn>
40
+ # <dt><label for="user_email">Email</label></dt>
41
+ # <dd><input type="text" name="user[email]" id="user_email"></dd>
42
+ # </dfn>
43
+
27
44
  end
28
45
  ```
29
46
 
30
- ## Todo
31
- Figure out how to support options for the label tag, like `class:` and, uh, idk, other stuff. Maybe just pass options hash directly to `label:`? Maybe just start with additional `label_class:` option? Is there any other potential option for e.g. `form.label` we would care about? Probably should just cross this bridge when we get to it.
32
-
33
47
  ## Contributing
34
48
 
35
49
  Bug reports and pull requests are welcome on GitHub at https://github.com/botandrose/labelled_form.
@@ -37,3 +51,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/botand
37
51
  ## License
38
52
 
39
53
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
54
+
@@ -1,3 +1,3 @@
1
1
  module LabelledForm
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/labelled_form.rb CHANGED
@@ -9,52 +9,90 @@ module LabelledForm
9
9
  end if defined?(Rails)
10
10
 
11
11
  class Builder < ActionView::Helpers::FormBuilder
12
- def text_area method, options = {}
13
- options = options.dup
14
- if label_text = options.delete(:label)
15
- label_text = method.to_s.humanize if label_text === true
16
- label(method, label_text) + " ".html_safe + super
12
+ def with_label method, options
13
+ wrap_in_dfn = options.delete(:wrap_in_dfn)
14
+ label_class = options.delete(:label_class)
15
+ label_with_colon = options.delete(:label_with_colon)
16
+ label_text = options.delete(:label) || label_class.present? || label_with_colon
17
+
18
+ label_text = method.to_s.humanize if label_text === true
19
+ label_text&.sub!(/_id$/,"")
20
+ label_text << ":" if label_with_colon && !label_text.ends_with?(":")
21
+
22
+ field_html = yield
23
+
24
+ if label_text
25
+ if wrap_in_dfn
26
+ @template.tag.dfn do
27
+ @template.tag.dt(label(method, label_text, class: label_class)) +
28
+ @template.tag.dd(field_html)
29
+ end
30
+ else
31
+ label(method, label_text, class: label_class) + " ".html_safe + field_html
32
+ end
17
33
  else
34
+ field_html
35
+ end
36
+ end
37
+
38
+ def text_area method, options = {}
39
+ with_label(method, options) do
18
40
  super
19
41
  end
20
42
  end
21
43
 
22
44
  def date_field method, options = {}
23
- options = options.dup
24
- if label_text = options.delete(:label)
25
- label_text = method.to_s.humanize if label_text === true
26
- label(method, label_text) + " ".html_safe + super
27
- else
45
+ with_label(method, options) do
46
+ super
47
+ end
48
+ end
49
+
50
+ def phone_field method, options = {}
51
+ with_label(method, options) do
28
52
  super
29
53
  end
30
54
  end
31
55
 
32
56
  def email_field method, options = {}
33
- options = options.dup
34
- if label_text = options.delete(:label)
35
- label_text = method.to_s.humanize if label_text === true
36
- label(method, label_text) + " ".html_safe + super
37
- else
57
+ with_label(method, options) do
58
+ super
59
+ end
60
+ end
61
+
62
+ def url_field method, options = {}
63
+ with_label(method, options) do
38
64
  super
39
65
  end
40
66
  end
41
67
 
42
68
  def text_field method, options = {}
43
- options = options.dup
44
- if label_text = options.delete(:label)
45
- label_text = method.to_s.humanize if label_text === true
46
- label(method, label_text) + " ".html_safe + super
47
- else
69
+ with_label(method, options) do
48
70
  super
49
71
  end
50
72
  end
51
73
 
52
74
  def file_field method, options = {}
53
- options = options.dup
54
- if label_text = options.delete(:label)
55
- label_text = method.to_s.humanize if label_text === true
56
- label(method, label_text) + " ".html_safe + super
57
- else
75
+ with_label(method, options) do
76
+ super
77
+ end
78
+ end
79
+
80
+ def select(method, choices = nil, options = {}, html_options = {}, &block)
81
+ label_options = (options.keys & %i[wrap_in_dfn label_class label_with_colon label]).any? ? options : html_options
82
+ with_label(method, label_options) do
83
+ super
84
+ end
85
+ end
86
+
87
+ def collection_select(method, collection, value_method, text_method, options = {}, html_options = {})
88
+ label_options = (options.keys & %i[wrap_in_dfn label_class label_with_colon label]).any? ? options : html_options
89
+ with_label(method, label_options) do
90
+ super
91
+ end
92
+ end
93
+
94
+ def number_field method, options = {}
95
+ with_label(method, options) do
58
96
  super
59
97
  end
60
98
  end
@@ -62,11 +100,12 @@ module LabelledForm
62
100
  def radio_button(method, tag_value, options = {})
63
101
  options = options.dup
64
102
  label_text = options.delete(:label)
103
+ label_class = options.delete(:label_class)
65
104
  super.tap do |out|
66
105
  if label_text
67
106
  label_text = tag_value if label_text === true
68
107
  out << " ".html_safe
69
- out << label(method, label_text, value: tag_value)
108
+ out << label(method, label_text, value: tag_value, class: label_class)
70
109
  end
71
110
  end
72
111
  end
@@ -74,14 +113,39 @@ module LabelledForm
74
113
  def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
75
114
  options = options.dup
76
115
  label_text = options.delete(:label)
116
+ label_class = options.delete(:label_class)
77
117
  super.tap do |out|
78
118
  if label_text
79
119
  label_text = checked_value == "1" ? nil : checked_value if label_text === true
80
120
  out << " ".html_safe
81
121
  label_options = options[:multiple] ? { value: checked_value } : {}
82
- out << label(method, label_text, label_options)
122
+ out << label(method, label_text, label_options.merge(class: label_class))
83
123
  end
84
124
  end
85
125
  end
126
+
127
+ def multiple_check_box method, value, options={}
128
+ options = options.dup
129
+ label_text = options.delete(:label)
130
+ label_class = options.delete(:label_class)
131
+ out = "".html_safe
132
+
133
+ if !multiple_check_box_fields.include?(method)
134
+ multiple_check_box_fields << method
135
+ out << hidden_field(method, value: "", multiple: true)
136
+ end
137
+
138
+ out << check_box(method, { multiple: true }.reverse_merge(options), value, nil)
139
+
140
+ if label_text
141
+ label_text = value == "1" ? nil : value if label_text === true
142
+ out << " ".html_safe
143
+ out << label(method, label_text, value: value, class: label_class)
144
+ end
145
+ end
146
+
147
+ private def multiple_check_box_fields
148
+ @multiple_check_box_fields ||= Set.new
149
+ end
86
150
  end
87
151
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: labelled_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Micah Geisel
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-15 00:00:00.000000000 Z
11
+ date: 2023-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionview
@@ -104,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
104
  - !ruby/object:Gem::Version
105
105
  version: '0'
106
106
  requirements: []
107
- rubygems_version: 3.2.3
107
+ rubygems_version: 3.1.6
108
108
  signing_key:
109
109
  specification_version: 4
110
110
  summary: Adds label option to Rails form helpers