labelled_form 0.2.4 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +22 -7
- data/lib/labelled_form/version.rb +1 -1
- data/lib/labelled_form.rb +93 -27
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b2fc864539f7d0758c17fe82bf4b2d55f7e7e62f599b7d0c3d161f36b2cc290
|
4
|
+
data.tar.gz: 952f0fd500ac7000d91cf605a584aaa24badb53fe304198554a943cfb0007d90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 221b4bc6a8f23855d77eca419d12ea21df6ecd417b26d601db565d2d8035bbff3d15ade3de9806057e0a69197a382a5f4bc813ff6682b9dca4f2395fbff8b33d
|
7
|
+
data.tar.gz: 1c7cb2d94739ab73d5c576f5b569eee32faf1ed04da086926a679a5c192918db40008428b48010304e2a6fb52b862ad8b32b9b9c5ddac9ad2799b60bf8aa4d50
|
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
|
+
|
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
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
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
|
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
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
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,41 @@ 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
|
+
|
146
|
+
out
|
147
|
+
end
|
148
|
+
|
149
|
+
private def multiple_check_box_fields
|
150
|
+
@multiple_check_box_fields ||= Set.new
|
151
|
+
end
|
86
152
|
end
|
87
153
|
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.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-10-27 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.
|
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
|