lifeform 0.5 → 0.8.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 +4 -4
- data/.rubocop.yml +11 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +1 -0
- data/Gemfile.lock +36 -27
- data/README.md +15 -3
- data/lib/lifeform/capturing_renderable.rb +17 -0
- data/lib/lifeform/form.rb +53 -45
- data/lib/lifeform/libraries/default/button.rb +19 -27
- data/lib/lifeform/libraries/default/input.rb +26 -36
- data/lib/lifeform/libraries/default/submit_button.rb +1 -1
- data/lib/lifeform/libraries/shoelace/input.rb +2 -0
- data/lib/lifeform/version.rb +1 -1
- data/lib/lifeform.rb +11 -1
- data/lifeform.gemspec +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 84cc7c165874ab122f164fe3edae5c7e7f68d7ba4936f115151f5fec3ecdd217
|
|
4
|
+
data.tar.gz: 690ab60f63f813cfedf830416d1fe3761ad4dfd090344dc227e598e62c5bb497
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: aec711376fecf90651b305a8a8f3a7044dcbdb022a4c17713e0433f8e90590f9502668794143183bc0c71d8d3a0de1dbe85cc3e989b764552190367234a10bed
|
|
7
|
+
data.tar.gz: f057b69cbf5d7f43f5cba4a30f79df5d0f3735af41fb363bf7147853d327b07315e730708c8feaae2063993a477ee39c9e9d22f0f1385f4c3c9991d9360a4a98
|
data/.rubocop.yml
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
|
+
require:
|
|
2
|
+
- rubocop-minitest
|
|
3
|
+
- rubocop-rake
|
|
4
|
+
|
|
1
5
|
AllCops:
|
|
2
6
|
TargetRubyVersion: 2.7
|
|
3
7
|
NewCops: enable
|
|
4
8
|
|
|
9
|
+
Lint/MissingSuper:
|
|
10
|
+
Enabled: false
|
|
11
|
+
|
|
5
12
|
Metrics/AbcSize:
|
|
6
13
|
Exclude:
|
|
7
14
|
- test/**/*.rb
|
|
8
15
|
|
|
16
|
+
Metrics/ClassLength:
|
|
17
|
+
Exclude:
|
|
18
|
+
- test/**/*.rb
|
|
19
|
+
|
|
9
20
|
Metrics/MethodLength:
|
|
10
21
|
Max: 20
|
|
11
22
|
Exclude:
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
|
@@ -1,69 +1,75 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
lifeform (0.
|
|
4
|
+
lifeform (0.8.0)
|
|
5
5
|
activesupport (>= 6.0)
|
|
6
|
-
|
|
6
|
+
phlex (~> 0.3)
|
|
7
7
|
zeitwerk (~> 2.5)
|
|
8
8
|
|
|
9
9
|
GEM
|
|
10
10
|
remote: https://rubygems.org/
|
|
11
11
|
specs:
|
|
12
|
-
activesupport (7.0.
|
|
12
|
+
activesupport (7.0.4)
|
|
13
13
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
14
14
|
i18n (>= 1.6, < 2)
|
|
15
15
|
minitest (>= 5.1)
|
|
16
16
|
tzinfo (~> 2.0)
|
|
17
|
+
ansi (1.5.0)
|
|
17
18
|
ast (2.4.2)
|
|
18
19
|
backport (1.2.0)
|
|
19
20
|
benchmark (0.2.0)
|
|
21
|
+
builder (3.2.4)
|
|
20
22
|
concurrent-ruby (1.1.10)
|
|
21
23
|
diff-lcs (1.5.0)
|
|
22
24
|
e2mmap (0.1.0)
|
|
23
|
-
|
|
24
|
-
i18n (1.10.0)
|
|
25
|
+
i18n (1.12.0)
|
|
25
26
|
concurrent-ruby (~> 1.0)
|
|
26
27
|
jaro_winkler (1.5.4)
|
|
27
|
-
|
|
28
|
+
json (2.6.2)
|
|
29
|
+
kramdown (2.4.0)
|
|
28
30
|
rexml
|
|
29
31
|
kramdown-parser-gfm (1.1.0)
|
|
30
32
|
kramdown (~> 2.0)
|
|
31
|
-
minitest (5.
|
|
32
|
-
|
|
33
|
+
minitest (5.16.3)
|
|
34
|
+
minitest-reporters (1.5.0)
|
|
35
|
+
ansi
|
|
36
|
+
builder
|
|
37
|
+
minitest (>= 5.0)
|
|
38
|
+
ruby-progressbar
|
|
39
|
+
nokogiri (1.13.8-arm64-darwin)
|
|
33
40
|
racc (~> 1.4)
|
|
34
|
-
nokogiri (1.13.
|
|
41
|
+
nokogiri (1.13.8-x86_64-linux)
|
|
35
42
|
racc (~> 1.4)
|
|
36
|
-
papercraft (0.24)
|
|
37
|
-
escape_utils (~> 1.2.1)
|
|
38
|
-
kramdown (~> 2.3.1)
|
|
39
|
-
kramdown-parser-gfm (~> 1.1.0)
|
|
40
|
-
rouge (~> 3.27.0)
|
|
41
43
|
parallel (1.22.1)
|
|
42
|
-
parser (3.1.2.
|
|
44
|
+
parser (3.1.2.1)
|
|
43
45
|
ast (~> 2.4.1)
|
|
46
|
+
phlex (0.3.2)
|
|
47
|
+
syntax_tree (~> 3.6)
|
|
48
|
+
zeitwerk (~> 2.6)
|
|
49
|
+
prettier_print (0.1.0)
|
|
44
50
|
racc (1.6.0)
|
|
45
51
|
rails-dom-testing (2.0.3)
|
|
46
52
|
activesupport (>= 4.2.0)
|
|
47
53
|
nokogiri (>= 1.6)
|
|
48
54
|
rainbow (3.1.1)
|
|
49
55
|
rake (13.0.6)
|
|
50
|
-
regexp_parser (2.
|
|
56
|
+
regexp_parser (2.6.0)
|
|
51
57
|
reverse_markdown (2.1.1)
|
|
52
58
|
nokogiri
|
|
53
59
|
rexml (3.2.5)
|
|
54
|
-
|
|
55
|
-
|
|
60
|
+
rubocop (1.36.0)
|
|
61
|
+
json (~> 2.3)
|
|
56
62
|
parallel (~> 1.10)
|
|
57
|
-
parser (>= 3.1.
|
|
63
|
+
parser (>= 3.1.2.1)
|
|
58
64
|
rainbow (>= 2.2.2, < 4.0)
|
|
59
65
|
regexp_parser (>= 1.8, < 3.0)
|
|
60
66
|
rexml (>= 3.2.5, < 4.0)
|
|
61
|
-
rubocop-ast (>= 1.
|
|
67
|
+
rubocop-ast (>= 1.20.1, < 2.0)
|
|
62
68
|
ruby-progressbar (~> 1.7)
|
|
63
69
|
unicode-display_width (>= 1.4.0, < 3.0)
|
|
64
|
-
rubocop-ast (1.
|
|
70
|
+
rubocop-ast (1.21.0)
|
|
65
71
|
parser (>= 3.1.1.0)
|
|
66
|
-
rubocop-minitest (0.20.
|
|
72
|
+
rubocop-minitest (0.20.1)
|
|
67
73
|
rubocop (>= 0.90, < 2.0)
|
|
68
74
|
rubocop-rake (0.6.0)
|
|
69
75
|
rubocop (~> 1.0)
|
|
@@ -83,15 +89,17 @@ GEM
|
|
|
83
89
|
thor (~> 1.0)
|
|
84
90
|
tilt (~> 2.0)
|
|
85
91
|
yard (~> 0.9, >= 0.9.24)
|
|
92
|
+
syntax_tree (3.6.2)
|
|
93
|
+
prettier_print
|
|
86
94
|
thor (1.2.1)
|
|
87
|
-
tilt (2.0.
|
|
88
|
-
tzinfo (2.0.
|
|
95
|
+
tilt (2.0.11)
|
|
96
|
+
tzinfo (2.0.5)
|
|
89
97
|
concurrent-ruby (~> 1.0)
|
|
90
|
-
unicode-display_width (2.
|
|
98
|
+
unicode-display_width (2.3.0)
|
|
91
99
|
webrick (1.7.0)
|
|
92
|
-
yard (0.9.
|
|
100
|
+
yard (0.9.28)
|
|
93
101
|
webrick (~> 1.7.0)
|
|
94
|
-
zeitwerk (2.
|
|
102
|
+
zeitwerk (2.6.1)
|
|
95
103
|
|
|
96
104
|
PLATFORMS
|
|
97
105
|
arm64-darwin-21
|
|
@@ -100,6 +108,7 @@ PLATFORMS
|
|
|
100
108
|
DEPENDENCIES
|
|
101
109
|
lifeform!
|
|
102
110
|
minitest (~> 5.0)
|
|
111
|
+
minitest-reporters (~> 1.5)
|
|
103
112
|
rails-dom-testing (~> 2.0)
|
|
104
113
|
rake (~> 13.0)
|
|
105
114
|
rubocop (~> 1.21)
|
data/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Lifeform
|
|
2
2
|
|
|
3
|
-
Component-centric form object rendering for Ruby.
|
|
3
|
+
Component-centric form object rendering for Ruby. Powered by [Phlex](https://www.phlex.fun)
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -28,13 +28,25 @@ end
|
|
|
28
28
|
And a template rendering of:
|
|
29
29
|
|
|
30
30
|
```erb
|
|
31
|
-
|
|
31
|
+
<!-- ERB -->
|
|
32
|
+
<%= render TestForm.new(url: "/path") do |f| %>
|
|
32
33
|
<%= render f.field(:occupation) %>
|
|
33
34
|
<%= render f.field(:age, value: 47) %>
|
|
34
35
|
<%= render f.field(:submit) %>
|
|
35
36
|
<% end %>
|
|
36
37
|
```
|
|
37
38
|
|
|
39
|
+
```rb
|
|
40
|
+
# Or Phlex:
|
|
41
|
+
def template
|
|
42
|
+
render TestForm.new(url: "/path") do |f|
|
|
43
|
+
render f.field(:occupation)
|
|
44
|
+
render f.field(:age, value: 47)
|
|
45
|
+
render f.field(:submit)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
```
|
|
49
|
+
|
|
38
50
|
You get the following HTML output:
|
|
39
51
|
|
|
40
52
|
```html
|
|
@@ -55,7 +67,7 @@ You get the following HTML output:
|
|
|
55
67
|
|
|
56
68
|
Nested names based on models (aka `profile[name]`) and inferred action paths are supported as well.
|
|
57
69
|
|
|
58
|
-
Multiple component libraries and input types—and easy customizability via [
|
|
70
|
+
Multiple component libraries and input types—and easy customizability via [Phlex](https://github.com/joeldrapper/phlex) templates—are a fundamental aspect of the architecture of Lifeform.
|
|
59
71
|
|
|
60
72
|
### Automatic Field Rendering
|
|
61
73
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lifeform
|
|
4
|
+
module CapturingRenderable
|
|
5
|
+
# NOTE: the previous `with_output_buffer` stuff is for some reason incompatible with Serbea.
|
|
6
|
+
# So we'll use a simpler capture.
|
|
7
|
+
def render_in(view_context, &block)
|
|
8
|
+
if block
|
|
9
|
+
call(view_context: view_context) do |*args, **kwargs|
|
|
10
|
+
raw(view_context.capture(*args, **kwargs, &block))
|
|
11
|
+
end.html_safe
|
|
12
|
+
else
|
|
13
|
+
call(view_context: view_context).html_safe
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
data/lib/lifeform/form.rb
CHANGED
|
@@ -6,7 +6,8 @@ module Lifeform
|
|
|
6
6
|
FieldDefinition = Struct.new(:type, :library, :parameters)
|
|
7
7
|
|
|
8
8
|
# A form object which stores field definitions and can be rendered as a component
|
|
9
|
-
class Form # rubocop:todo Metrics/ClassLength
|
|
9
|
+
class Form < Phlex::View # rubocop:todo Metrics/ClassLength
|
|
10
|
+
include CapturingRenderable
|
|
10
11
|
MODEL_PATH_HELPER = :polymorphic_path
|
|
11
12
|
|
|
12
13
|
class << self
|
|
@@ -43,34 +44,35 @@ module Lifeform
|
|
|
43
44
|
@library ||= :default
|
|
44
45
|
end
|
|
45
46
|
|
|
46
|
-
def
|
|
47
|
+
def process_value(key, value)
|
|
48
|
+
return value if key == :if
|
|
49
|
+
|
|
47
50
|
case value
|
|
48
|
-
when TrueClass
|
|
49
|
-
|
|
51
|
+
when TrueClass
|
|
52
|
+
key.to_s
|
|
53
|
+
when FalseClass
|
|
54
|
+
nil
|
|
55
|
+
when Symbol, Integer
|
|
56
|
+
value.to_s
|
|
50
57
|
else
|
|
51
|
-
|
|
58
|
+
value
|
|
52
59
|
end
|
|
53
60
|
end
|
|
54
61
|
|
|
55
62
|
# @param kwargs [Hash]
|
|
56
63
|
def parameters_to_attributes(kwargs)
|
|
57
|
-
previous_value = EscapeUtils.html_secure
|
|
58
|
-
EscapeUtils.html_secure = false
|
|
59
|
-
|
|
60
64
|
attributes = {}
|
|
61
65
|
kwargs.each do |key, value|
|
|
62
66
|
case value
|
|
63
67
|
when Hash
|
|
64
68
|
value.each do |inner_key, inner_value|
|
|
65
|
-
attributes[:"#{key}_#{inner_key}"] =
|
|
69
|
+
attributes[:"#{key}_#{inner_key}"] = process_value(inner_key, inner_value)
|
|
66
70
|
end
|
|
67
71
|
else
|
|
68
|
-
attributes[key] =
|
|
72
|
+
attributes[key] = process_value(key, value) unless value.nil?
|
|
69
73
|
end
|
|
70
74
|
end
|
|
71
75
|
|
|
72
|
-
EscapeUtils.html_secure = previous_value
|
|
73
|
-
|
|
74
76
|
attributes
|
|
75
77
|
end
|
|
76
78
|
|
|
@@ -84,6 +86,16 @@ module Lifeform
|
|
|
84
86
|
model.class.name.underscore.tr("/", "_")
|
|
85
87
|
end
|
|
86
88
|
end
|
|
89
|
+
|
|
90
|
+
# @return [Array<Symbol>]
|
|
91
|
+
def param_keys
|
|
92
|
+
fields.keys
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# @return [Array<String>]
|
|
96
|
+
def param_string_keys
|
|
97
|
+
fields.keys.map(&:to_s)
|
|
98
|
+
end
|
|
87
99
|
end
|
|
88
100
|
|
|
89
101
|
# @return [Object]
|
|
@@ -112,7 +124,7 @@ module Lifeform
|
|
|
112
124
|
@library = Libraries.const_get(@library_name.to_s.classify)
|
|
113
125
|
@subform_instances = {}
|
|
114
126
|
|
|
115
|
-
parameters[:method] ||= model.respond_to?(:persisted?) && model.persisted? ? :patch : :post
|
|
127
|
+
@method = parameters[:method] ||= model.respond_to?(:persisted?) && model.persisted? ? :patch : :post
|
|
116
128
|
parameters[:accept_charset] ||= "UTF-8"
|
|
117
129
|
verify_method
|
|
118
130
|
end
|
|
@@ -120,20 +132,27 @@ module Lifeform
|
|
|
120
132
|
def verify_method
|
|
121
133
|
return if %w[get post].include?(parameters[:method].to_s.downcase)
|
|
122
134
|
|
|
123
|
-
@method_tag =
|
|
124
|
-
|
|
125
|
-
|
|
135
|
+
@method_tag = Class.new(Phlex::View) do # TODO: break this out into a real component
|
|
136
|
+
def initialize(method:)
|
|
137
|
+
@method = method
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def template
|
|
141
|
+
input type: "hidden", name: "_method", value: @method, autocomplete: "off"
|
|
142
|
+
end
|
|
143
|
+
end.new(method: @parameters[:method].to_s.downcase)
|
|
144
|
+
|
|
126
145
|
parameters[:method] = :post
|
|
127
146
|
end
|
|
128
147
|
|
|
129
|
-
def add_authenticity_token
|
|
130
|
-
if
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
elsif
|
|
136
|
-
|
|
148
|
+
def add_authenticity_token # rubocop:disable Metrics/AbcSize
|
|
149
|
+
if helpers.respond_to?(:token_tag, true) # Rails
|
|
150
|
+
helpers.send(:token_tag, nil, form_options: {
|
|
151
|
+
action: parameters[:action].to_s,
|
|
152
|
+
method: parameters[:method].to_s.downcase
|
|
153
|
+
})
|
|
154
|
+
elsif helpers.respond_to?(:csrf_tag, true) # Roda
|
|
155
|
+
helpers.send(:csrf_tag, parameters[:action].to_s, @method.to_s)
|
|
137
156
|
else
|
|
138
157
|
raise Lifeform::Error, "Missing token tag helper. Override `add_authenticity_token' in your Form object"
|
|
139
158
|
end
|
|
@@ -161,30 +180,19 @@ module Lifeform
|
|
|
161
180
|
)
|
|
162
181
|
end
|
|
163
182
|
|
|
164
|
-
def
|
|
183
|
+
def template(&block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
|
|
165
184
|
form_tag = library::FORM_TAG
|
|
166
|
-
parameters[:action] ||= url || (model ?
|
|
167
|
-
|
|
168
|
-
content = if block
|
|
169
|
-
view_context.capture(self, &block)
|
|
170
|
-
else
|
|
171
|
-
self.class.fields.map { |k, _v| field(k).render_in(self) }.join.then do |renderings|
|
|
172
|
-
renderings.respond_to?(:html_safe) ? renderings.html_safe : renderings
|
|
173
|
-
end
|
|
174
|
-
end
|
|
185
|
+
parameters[:action] ||= url || (model ? helpers.send(self.class.const_get(:MODEL_PATH_HELPER), model) : nil)
|
|
175
186
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
# content += %(\n<button type="submit" style="position: absolute; left: -9999px"></button>).html_safe
|
|
183
|
-
# end
|
|
187
|
+
send(form_tag, **attributes) do
|
|
188
|
+
raw(add_authenticity_token) unless parameters[:method].to_s.downcase == "get"
|
|
189
|
+
raw @method_tag&.() || ""
|
|
190
|
+
block ? yield_content(&block) : auto_render_fields
|
|
191
|
+
end
|
|
192
|
+
end
|
|
184
193
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
end.render(attributes)
|
|
194
|
+
def auto_render_fields
|
|
195
|
+
self.class.fields.map { |k, _v| render(field(k)) }
|
|
188
196
|
end
|
|
189
197
|
end
|
|
190
198
|
end
|
|
@@ -3,48 +3,40 @@
|
|
|
3
3
|
module Lifeform
|
|
4
4
|
module Libraries
|
|
5
5
|
class Default
|
|
6
|
-
class Button
|
|
6
|
+
class Button < Phlex::View
|
|
7
|
+
include CapturingRenderable
|
|
8
|
+
|
|
7
9
|
attr_reader :form, :field_definition, :attributes
|
|
8
10
|
|
|
9
11
|
WRAPPER_TAG = :form_button
|
|
10
12
|
BUTTON_TAG = :button
|
|
11
13
|
|
|
14
|
+
register_element WRAPPER_TAG
|
|
15
|
+
|
|
12
16
|
def initialize(form, field_definition, **attributes)
|
|
13
17
|
@form = form
|
|
14
18
|
@field_definition = field_definition
|
|
15
19
|
@attributes = Lifeform::Form.parameters_to_attributes(field_definition.parameters).merge(attributes)
|
|
16
20
|
@if = @attributes.delete(:if)
|
|
17
21
|
@label = @attributes.delete(:label) || "Unlabeled Button"
|
|
18
|
-
@attributes[:type] ||=
|
|
22
|
+
@attributes[:type] ||= "button"
|
|
19
23
|
end
|
|
20
24
|
|
|
21
|
-
def
|
|
22
|
-
|
|
23
|
-
@content = block
|
|
24
|
-
return "" if !@if.nil? && !@if
|
|
25
|
+
def template(&block)
|
|
26
|
+
return if !@if.nil? && !@if
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
wrapper_tag = self.class.const_get(:WRAPPER_TAG)
|
|
29
|
+
button_tag = self.class.const_get(:BUTTON_TAG)
|
|
30
|
+
|
|
31
|
+
field_body = proc {
|
|
32
|
+
send(button_tag, **@attributes) do
|
|
33
|
+
raw(@label.to_s) unless block
|
|
34
|
+
yield_content(&block)
|
|
35
|
+
end
|
|
36
|
+
}
|
|
37
|
+
return field_body.() unless wrapper_tag
|
|
28
38
|
|
|
29
|
-
|
|
30
|
-
Papercraft.html do |wrapper_tag:, button_tag:, attributes:, field_data:|
|
|
31
|
-
field_body = proc {
|
|
32
|
-
send(button_tag, **attributes) do
|
|
33
|
-
emit field_data[:content] || field_data[:label]
|
|
34
|
-
end
|
|
35
|
-
}
|
|
36
|
-
next field_body.call unless wrapper_tag
|
|
37
|
-
|
|
38
|
-
send wrapper_tag, name: attributes[:name], &field_body
|
|
39
|
-
end.render(
|
|
40
|
-
wrapper_tag: self.class.const_get(:WRAPPER_TAG),
|
|
41
|
-
button_tag: self.class.const_get(:BUTTON_TAG),
|
|
42
|
-
attributes: attributes,
|
|
43
|
-
field_data: {
|
|
44
|
-
label: @label,
|
|
45
|
-
content: @content && @view_context.capture(&@content)
|
|
46
|
-
}
|
|
47
|
-
)
|
|
39
|
+
send wrapper_tag, name: @attributes[:name], &field_body
|
|
48
40
|
end
|
|
49
41
|
end
|
|
50
42
|
end
|
|
@@ -3,11 +3,15 @@
|
|
|
3
3
|
module Lifeform
|
|
4
4
|
module Libraries
|
|
5
5
|
class Default
|
|
6
|
-
class Input
|
|
6
|
+
class Input < Phlex::View
|
|
7
|
+
include CapturingRenderable
|
|
8
|
+
|
|
7
9
|
attr_reader :form, :field_definition, :attributes
|
|
8
10
|
|
|
9
|
-
INPUT_TAG = :input
|
|
10
11
|
WRAPPER_TAG = :form_field
|
|
12
|
+
INPUT_TAG = :input
|
|
13
|
+
|
|
14
|
+
register_element WRAPPER_TAG
|
|
11
15
|
|
|
12
16
|
def initialize(form, field_definition, **attributes)
|
|
13
17
|
@form = form
|
|
@@ -40,44 +44,30 @@ module Lifeform
|
|
|
40
44
|
end
|
|
41
45
|
|
|
42
46
|
def handle_labels
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
end.render(
|
|
46
|
-
attributes[:label].to_s,
|
|
47
|
-
(attributes[:id] || attributes[:name]).to_s
|
|
48
|
-
).tap do
|
|
49
|
-
@attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h
|
|
50
|
-
end
|
|
51
|
-
end
|
|
47
|
+
label_text = attributes[:label].to_s
|
|
48
|
+
label_name = (attributes[:id] || attributes[:name]).to_s
|
|
52
49
|
|
|
53
|
-
|
|
54
|
-
@view_context = view_context
|
|
55
|
-
@content = block
|
|
56
|
-
return "" if !@if.nil? && !@if
|
|
50
|
+
@attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h
|
|
57
51
|
|
|
58
|
-
|
|
52
|
+
proc {
|
|
53
|
+
label(for: label_name) { raw label_text }
|
|
54
|
+
}
|
|
59
55
|
end
|
|
60
56
|
|
|
61
|
-
def template
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
field_data: {
|
|
76
|
-
type: @field_type,
|
|
77
|
-
label: @label,
|
|
78
|
-
content: @content && @view_context.capture(&@content)
|
|
79
|
-
}
|
|
80
|
-
)
|
|
57
|
+
def template(&block)
|
|
58
|
+
return if !@if.nil? && !@if
|
|
59
|
+
|
|
60
|
+
wrapper_tag = self.class.const_get(:WRAPPER_TAG)
|
|
61
|
+
input_tag = self.class.const_get(:INPUT_TAG)
|
|
62
|
+
|
|
63
|
+
field_body = proc {
|
|
64
|
+
@label&.()
|
|
65
|
+
send input_tag, type: @field_type.to_s, **@attributes
|
|
66
|
+
yield_content(&block)
|
|
67
|
+
}
|
|
68
|
+
return field_body.() unless wrapper_tag
|
|
69
|
+
|
|
70
|
+
send wrapper_tag, name: @attributes[:name], &field_body
|
|
81
71
|
end
|
|
82
72
|
end
|
|
83
73
|
end
|
data/lib/lifeform/version.rb
CHANGED
data/lib/lifeform.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require "
|
|
3
|
+
require "phlex"
|
|
4
|
+
require "active_support/core_ext/string/output_safety"
|
|
4
5
|
|
|
5
6
|
require "zeitwerk"
|
|
6
7
|
loader = Zeitwerk::Loader.for_gem
|
|
@@ -9,3 +10,12 @@ loader.setup
|
|
|
9
10
|
module Lifeform
|
|
10
11
|
class Error < StandardError; end
|
|
11
12
|
end
|
|
13
|
+
|
|
14
|
+
if defined?(Bridgetown)
|
|
15
|
+
# Check compatibility
|
|
16
|
+
raise "The Lifeform support for Bridgetown requires v1.2 or newer" if Bridgetown::VERSION.to_f < 1.2
|
|
17
|
+
|
|
18
|
+
Bridgetown.initializer :lifeform do |config|
|
|
19
|
+
# no extra config at the moment
|
|
20
|
+
end
|
|
21
|
+
end
|
data/lifeform.gemspec
CHANGED
|
@@ -25,6 +25,6 @@ Gem::Specification.new do |spec|
|
|
|
25
25
|
|
|
26
26
|
# Uncomment to register a new dependency of your gem
|
|
27
27
|
spec.add_dependency "activesupport", ">= 6.0"
|
|
28
|
-
spec.add_dependency "
|
|
28
|
+
spec.add_dependency "phlex", "~> 0.3"
|
|
29
29
|
spec.add_dependency "zeitwerk", "~> 2.5"
|
|
30
30
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: lifeform
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 0.8.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bridgetown Team
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2022-
|
|
11
|
+
date: 2022-10-09 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -25,19 +25,19 @@ dependencies:
|
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
26
|
version: '6.0'
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
28
|
+
name: phlex
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
30
30
|
requirements:
|
|
31
31
|
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '0.
|
|
33
|
+
version: '0.3'
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '0.
|
|
40
|
+
version: '0.3'
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: zeitwerk
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -72,6 +72,7 @@ files:
|
|
|
72
72
|
- bin/rubocop
|
|
73
73
|
- bin/setup
|
|
74
74
|
- lib/lifeform.rb
|
|
75
|
+
- lib/lifeform/capturing_renderable.rb
|
|
75
76
|
- lib/lifeform/form.rb
|
|
76
77
|
- lib/lifeform/libraries/default.rb
|
|
77
78
|
- lib/lifeform/libraries/default/button.rb
|