lifeform 0.2.0 → 0.4.1
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/Gemfile.lock +1 -1
- data/README.md +16 -0
- data/lib/lifeform/form.rb +9 -3
- data/lib/lifeform/libraries/default/button.rb +52 -0
- data/lib/lifeform/libraries/default/submit_button.rb +16 -0
- data/lib/lifeform/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9dbdb103263edf279b32445d6faa8d4b0cbb6cacf21fe7b8f520af8a5896d0e0
|
4
|
+
data.tar.gz: 01fe12ea7c14e7b488ea1a383e7158c8d63adce6346a4c869613419046d5ec58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7272ba2e8224d31ed4b8313bec9056dcef6f82c995119ca43f8381d55aa304eff8119e1055bc7a838a17b61225efdb09200485d79edf30d4c5c616551f52a453
|
7
|
+
data.tar.gz: 788fe1a1ec8ea8d70d10b7e90f3993443a2f71040dc68e547095c07c3688d9afd41b16afe1c33456a63dffe8273a2fc2f833c87201b10553c1d5cede1d8b5842
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -20,6 +20,8 @@ Given a form object of:
|
|
20
20
|
class TestForm < Lifeform::Form
|
21
21
|
field :occupation, label: "Your Job", id: "your-occupation", required: true
|
22
22
|
field :age, library: :shoelace, label: "Your Age"
|
23
|
+
|
24
|
+
field :submit, type: :submit_button, label: "Save", class: "font-bold"
|
23
25
|
end
|
24
26
|
```
|
25
27
|
|
@@ -29,6 +31,7 @@ And a template rendering of:
|
|
29
31
|
<%= render TestForm.new(url: "/path") do %>
|
30
32
|
<%= render f.field(:occupation) %>
|
31
33
|
<%= render f.field(:age, value: 47) %>
|
34
|
+
<%= render f.field(:submit) %>
|
32
35
|
<% end %>
|
33
36
|
```
|
34
37
|
|
@@ -44,6 +47,9 @@ You get the following HTML output:
|
|
44
47
|
<form-field name="age">
|
45
48
|
<sl-input type="text" label="Your Age" name="age" value="47" id="age"></sl-input>
|
46
49
|
</form-field>
|
50
|
+
<form-button name="commit">
|
51
|
+
<button class="font-bold" name="commit" type="submit">Save</button>
|
52
|
+
</form-button>
|
47
53
|
</form>
|
48
54
|
```
|
49
55
|
|
@@ -51,6 +57,16 @@ Nested names based on models (aka `profile[name]`) and inferred action paths are
|
|
51
57
|
|
52
58
|
Multiple component libraries and input types—and easy customizability via [Papercraft](https://github.com/digital-fabric/papercraft) templates—are a fundamental aspect of the architecture of Lifeform.
|
53
59
|
|
60
|
+
### Automatic Field Rendering
|
61
|
+
|
62
|
+
For simple forms, you can avoid the need to render fields individually in your template. Given the form example above, you could write in your template:
|
63
|
+
|
64
|
+
```erb
|
65
|
+
<%= render TestForm.new(url: "/path") %>
|
66
|
+
```
|
67
|
+
|
68
|
+
And the fields defined in `TestForm` would render out automatically (since no block was provided to the `render` method).
|
69
|
+
|
54
70
|
## Development
|
55
71
|
|
56
72
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/lifeform/form.rb
CHANGED
@@ -6,7 +6,7 @@ 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
|
9
|
+
class Form # rubocop:todo Metrics/ClassLength
|
10
10
|
MODEL_PATH_HELPER = :polymorphic_path
|
11
11
|
|
12
12
|
class << self
|
@@ -127,11 +127,17 @@ module Lifeform
|
|
127
127
|
)
|
128
128
|
end
|
129
129
|
|
130
|
-
def render_in(view_context, &block) # rubocop:disable Metrics
|
130
|
+
def render_in(view_context, &block) # rubocop:disable Metrics
|
131
131
|
form_tag = library::FORM_TAG
|
132
132
|
parameters[:action] ||= url || (model ? view_context.send(self.class.const_get(:MODEL_PATH_HELPER), model) : nil)
|
133
133
|
|
134
|
-
content =
|
134
|
+
content = if block
|
135
|
+
view_context.capture(self, &block)
|
136
|
+
else
|
137
|
+
self.class.fields.map { |k, _v| field(k).render_in(self) }.join.then do |renderings|
|
138
|
+
renderings.respond_to?(:html_safe) ? renderings.html_safe : renderings
|
139
|
+
end
|
140
|
+
end
|
135
141
|
|
136
142
|
return content unless emit_form_tag
|
137
143
|
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lifeform
|
4
|
+
module Libraries
|
5
|
+
class Default
|
6
|
+
class Button
|
7
|
+
attr_reader :form, :field_definition, :attributes
|
8
|
+
|
9
|
+
WRAPPER_TAG = :form_button
|
10
|
+
BUTTON_TAG = :button
|
11
|
+
|
12
|
+
def initialize(form, field_definition, **attributes)
|
13
|
+
@form = form
|
14
|
+
@field_definition = field_definition
|
15
|
+
@attributes = Lifeform::Form.parameters_to_attributes(field_definition.parameters).merge(attributes)
|
16
|
+
@if = @attributes.delete(:if)
|
17
|
+
@label = @attributes.delete(:label) || "Unlabeled Button"
|
18
|
+
@attributes[:type] ||= :button
|
19
|
+
end
|
20
|
+
|
21
|
+
def render_in(view_context, &block)
|
22
|
+
@view_context = view_context
|
23
|
+
@content = block
|
24
|
+
return "" if !@if.nil? && !@if
|
25
|
+
|
26
|
+
template
|
27
|
+
end
|
28
|
+
|
29
|
+
def template # rubocop:disable Metrics/AbcSize
|
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
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lifeform
|
4
|
+
module Libraries
|
5
|
+
class Default
|
6
|
+
class SubmitButton < Button
|
7
|
+
def initialize(form, field_definition, **attributes)
|
8
|
+
attributes[:name] ||= "commit"
|
9
|
+
attributes[:type] = :submit
|
10
|
+
|
11
|
+
super
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/lifeform/version.rb
CHANGED
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: 0.
|
4
|
+
version: 0.4.1
|
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-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -74,7 +74,9 @@ files:
|
|
74
74
|
- lib/lifeform.rb
|
75
75
|
- lib/lifeform/form.rb
|
76
76
|
- lib/lifeform/libraries/default.rb
|
77
|
+
- lib/lifeform/libraries/default/button.rb
|
77
78
|
- lib/lifeform/libraries/default/input.rb
|
79
|
+
- lib/lifeform/libraries/default/submit_button.rb
|
78
80
|
- lib/lifeform/libraries/shoelace.rb
|
79
81
|
- lib/lifeform/libraries/shoelace/input.rb
|
80
82
|
- lib/lifeform/version.rb
|