labelled_form 0.2.3 → 0.3.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: 38262f7dcfe077ea7678b0f28e01a3baacb4bf0e1fed446f3c87ab3b07803b6e
4
- data.tar.gz: e03071200fdf6bf0858f08cc7d7f0bc45d273dc9070245dc356973327252bcc0
3
+ metadata.gz: e8726fb2089a7a687b5975773e7992cedc323e955b87db3d1e236fb1791cba33
4
+ data.tar.gz: 0e9b1cd1b765656dc8bc00f7895fca70c1c4fa5154cc81d7b8d74c163d643812
5
5
  SHA512:
6
- metadata.gz: 4566ed311aad19b51a3ced101f045c56ada1c66c5753b961841418e81385e81058318fe216bf08fd64731e7530b7588ff6ac1bd760991cd40697f22eb3d13f64
7
- data.tar.gz: 2b28af8ff3b88b78d960f55dafab2ae882e3973ff45b82db99d4355635587627eaad96e88071564da1dd76a23af5ed58cc00d05ea3a04be70da29663d5f00f27
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.3"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/labelled_form.rb CHANGED
@@ -9,72 +9,143 @@ module LabelledForm
9
9
  end if defined?(Rails)
10
10
 
11
11
  class Builder < ActionView::Helpers::FormBuilder
12
- def text_area method, options = {}
13
- if label_text = options.delete(:label)
14
- label_text = method.to_s.humanize if label_text === true
15
- 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
16
33
  else
34
+ field_html
35
+ end
36
+ end
37
+
38
+ def text_area method, options = {}
39
+ with_label(method, options) do
17
40
  super
18
41
  end
19
42
  end
20
43
 
21
44
  def date_field method, options = {}
22
- if label_text = options.delete(:label)
23
- label_text = method.to_s.humanize if label_text === true
24
- label(method, label_text) + " ".html_safe + super
25
- 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
26
52
  super
27
53
  end
28
54
  end
29
55
 
30
56
  def email_field method, options = {}
31
- if label_text = options.delete(:label)
32
- label_text = method.to_s.humanize if label_text === true
33
- label(method, label_text) + " ".html_safe + super
34
- 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
35
64
  super
36
65
  end
37
66
  end
38
67
 
39
68
  def text_field method, options = {}
40
- if label_text = options.delete(:label)
41
- label_text = method.to_s.humanize if label_text === true
42
- label(method, label_text) + " ".html_safe + super
43
- else
69
+ with_label(method, options) do
44
70
  super
45
71
  end
46
72
  end
47
73
 
48
74
  def file_field method, options = {}
49
- if label_text = options.delete(:label)
50
- label_text = method.to_s.humanize if label_text === true
51
- label(method, label_text) + " ".html_safe + super
52
- 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
53
96
  super
54
97
  end
55
98
  end
56
99
 
57
100
  def radio_button(method, tag_value, options = {})
101
+ options = options.dup
58
102
  label_text = options.delete(:label)
103
+ label_class = options.delete(:label_class)
59
104
  super.tap do |out|
60
105
  if label_text
61
106
  label_text = tag_value if label_text === true
62
107
  out << " ".html_safe
63
- out << label(method, label_text, value: tag_value)
108
+ out << label(method, label_text, value: tag_value, class: label_class)
64
109
  end
65
110
  end
66
111
  end
67
112
 
68
113
  def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
114
+ options = options.dup
69
115
  label_text = options.delete(:label)
116
+ label_class = options.delete(:label_class)
70
117
  super.tap do |out|
71
118
  if label_text
72
119
  label_text = checked_value == "1" ? nil : checked_value if label_text === true
73
120
  out << " ".html_safe
74
121
  label_options = options[:multiple] ? { value: checked_value } : {}
75
- out << label(method, label_text, label_options)
122
+ out << label(method, label_text, label_options.merge(class: label_class))
76
123
  end
77
124
  end
78
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
79
150
  end
80
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.3
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: 2021-01-20 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.0.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