phlexi-form 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.ruby-version +1 -0
- data/Appraisals +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +395 -0
- data/Rakefile +14 -0
- data/config.ru +9 -0
- data/gemfiles/default.gemfile +5 -0
- data/gemfiles/default.gemfile.lock +174 -0
- data/lib/generators/superform/install/USAGE +8 -0
- data/lib/generators/superform/install/install_generator.rb +34 -0
- data/lib/generators/superform/install/templates/application_form.rb +31 -0
- data/lib/phlexi/form/base.rb +234 -0
- data/lib/phlexi/form/components/base.rb +37 -0
- data/lib/phlexi/form/components/checkbox.rb +43 -0
- data/lib/phlexi/form/components/collection_checkboxes.rb +30 -0
- data/lib/phlexi/form/components/collection_radio_buttons.rb +29 -0
- data/lib/phlexi/form/components/concerns/has_options.rb +33 -0
- data/lib/phlexi/form/components/error.rb +21 -0
- data/lib/phlexi/form/components/full_error.rb +21 -0
- data/lib/phlexi/form/components/hint.rb +21 -0
- data/lib/phlexi/form/components/input.rb +78 -0
- data/lib/phlexi/form/components/label.rb +26 -0
- data/lib/phlexi/form/components/radio_button.rb +31 -0
- data/lib/phlexi/form/components/select.rb +57 -0
- data/lib/phlexi/form/components/textarea.rb +34 -0
- data/lib/phlexi/form/components/wrapper.rb +31 -0
- data/lib/phlexi/form/field_options/autofocus.rb +18 -0
- data/lib/phlexi/form/field_options/collection.rb +37 -0
- data/lib/phlexi/form/field_options/disabled.rb +18 -0
- data/lib/phlexi/form/field_options/errors.rb +82 -0
- data/lib/phlexi/form/field_options/hints.rb +22 -0
- data/lib/phlexi/form/field_options/labels.rb +28 -0
- data/lib/phlexi/form/field_options/length.rb +53 -0
- data/lib/phlexi/form/field_options/limit.rb +66 -0
- data/lib/phlexi/form/field_options/min_max.rb +92 -0
- data/lib/phlexi/form/field_options/multiple.rb +63 -0
- data/lib/phlexi/form/field_options/pattern.rb +38 -0
- data/lib/phlexi/form/field_options/placeholder.rb +18 -0
- data/lib/phlexi/form/field_options/readonly.rb +18 -0
- data/lib/phlexi/form/field_options/required.rb +37 -0
- data/lib/phlexi/form/field_options/type.rb +155 -0
- data/lib/phlexi/form/field_options/validators.rb +48 -0
- data/lib/phlexi/form/option_mapper.rb +154 -0
- data/lib/phlexi/form/structure/dom.rb +57 -0
- data/lib/phlexi/form/structure/field_builder.rb +199 -0
- data/lib/phlexi/form/structure/field_collection.rb +45 -0
- data/lib/phlexi/form/structure/namespace.rb +123 -0
- data/lib/phlexi/form/structure/namespace_collection.rb +48 -0
- data/lib/phlexi/form/structure/node.rb +18 -0
- data/lib/phlexi/form/version.rb +7 -0
- data/lib/phlexi/form.rb +28 -0
- data/lib/phlexi-form.rb +3 -0
- data/sig/phlexi/form.rbs +6 -0
- metadata +243 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 677bddc668db5f933dadf3bbe2057dc3463dda9efc7ea47cbbb4d79ec907bc9c
|
4
|
+
data.tar.gz: fe050eaa5f4e946c14fd69285ca93e4f1ed4ac0a4b079a7fb8976cadb4695381
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fffc444e797c5fead7b45308e05cc94bc0b9d3da7c389c1eb199867c18b3c834058b7ad8ae7a34a805e0e05c9d15897051b59efdaf213b16a17a991af571d42c
|
7
|
+
data.tar.gz: df80447cf6f4263e5303f39b219ff8615f303e106abce319a55d4289e382aba32aff65df08e746a75045427f15403e742d8bbb38c4cfef611935df1f1706bd09
|
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.2
|
data/Appraisals
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
appraise "default" do
|
2
|
+
# gem "phlex", "~> 1.10"
|
3
|
+
end
|
4
|
+
|
5
|
+
# appraise "rails-7" do
|
6
|
+
# gem "rails", "~> 7.1.3", ">= 7.1.3.4"
|
7
|
+
# gem "sqlite3", "~> 1.4"
|
8
|
+
# gem "puma", ">= 5.0"
|
9
|
+
# gem "importmap-rails"
|
10
|
+
# gem "turbo-rails"
|
11
|
+
# gem "stimulus-rails"
|
12
|
+
# gem "tzinfo-data", platforms: %i[windows jruby]
|
13
|
+
# end
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at bradgessler@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Stefan Froelich
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,395 @@
|
|
1
|
+
# Superform
|
2
|
+
|
3
|
+
Superform aims to be the best way to build forms in Rails applications. Here's what it does differently.
|
4
|
+
|
5
|
+
* **Everything is a component.** Superform is built on top of [Phlex](https://phlex.fun), so every bit of HTML in the form can be customized to your precise needs. Use it with your own CSS Framework or go crazy customizing every last bit of TailwindCSS.
|
6
|
+
|
7
|
+
* **Automatic strong parameters.** Superform automatically permits form fields so you don't have to facepalm yourself after adding a field, wondering why it doesn't persist, only to realize you forgot to add the parameter to your controller. No more! Superform was architected with safety & security in mind, meaning it can automatically permit your form parameters.
|
8
|
+
|
9
|
+
* **Compose complex forms with Plain 'ol Ruby Objects.** Superform is built on top of POROs, so you can easily compose classes, modules, & ruby code together to create complex forms. You can even extend forms to create new forms with a different look and feel.
|
10
|
+
|
11
|
+
It's a complete rewrite of Rails form's internals that's inspired by Reactive component design patterns.
|
12
|
+
|
13
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/0e4dfe2a1ece26e3a59e/maintainability)](https://codeclimate.com/github/rubymonolith/superform/maintainability) [![Ruby](https://github.com/rubymonolith/superform/actions/workflows/main.yml/badge.svg)](https://github.com/rubymonolith/superform/actions/workflows/main.yml)
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add to the Rails application's Gemfile by executing:
|
18
|
+
|
19
|
+
$ bundle add superform
|
20
|
+
|
21
|
+
Then install it.
|
22
|
+
|
23
|
+
$ rails g superform:install
|
24
|
+
|
25
|
+
This will install both Phlex Rails and Superform.
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
Superform streamlines the development of forms on Rails applications by making everything a component.
|
30
|
+
|
31
|
+
After installing, create a form in `app/views/*/form.rb`. For example, a form for a `Post` resource might look like this.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
# ./app/views/posts/form.rb
|
35
|
+
class Posts::Form < ApplicationForm
|
36
|
+
def template(&)
|
37
|
+
row field(:title).input
|
38
|
+
row field(:body).textarea
|
39
|
+
row field(:blog).select Blog.select(:id, :title)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
Then render it in your templates. Here's what it looks like from an Erb file.
|
45
|
+
|
46
|
+
```erb
|
47
|
+
<h1>New post</h1>
|
48
|
+
<%= render Posts::Form.new @post %>
|
49
|
+
```
|
50
|
+
|
51
|
+
## Customization
|
52
|
+
|
53
|
+
Superforms are built out of [Phlex components](https://www.phlex.fun/html/components/). The method names correspeond with the HTML tag, its arguments are attributes, and the blocks are the contents of the tag.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
# ./app/views/forms/application_form.rb
|
57
|
+
class ApplicationForm < Superform::Rails::Form
|
58
|
+
class MyInputComponent < Superform::Rails::Components::InputComponent
|
59
|
+
def template(&)
|
60
|
+
div class: "form-field" do
|
61
|
+
input(**attributes)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
class Field < Superform::Rails::Form::Field
|
67
|
+
def input(**attributes)
|
68
|
+
MyInputComponent.new(self, attributes: attributes)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def labeled(component)
|
73
|
+
div class: "form-row" do
|
74
|
+
render component.field.label
|
75
|
+
render component
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def submit(text)
|
80
|
+
button(type: :submit) { text }
|
81
|
+
end
|
82
|
+
end
|
83
|
+
```
|
84
|
+
|
85
|
+
That looks like a LOT of code, and it is, but look at how easy it is to create forms.
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
# ./app/views/users/form.rb
|
89
|
+
class Users::Form < ApplicationForm
|
90
|
+
def template(&)
|
91
|
+
labeled field(:name).input
|
92
|
+
labeled field(:email).input(type: :email)
|
93
|
+
|
94
|
+
submit "Sign up"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
Then render it from Erb.
|
100
|
+
|
101
|
+
```erb
|
102
|
+
<%= render Users::Form.new @user %>
|
103
|
+
```
|
104
|
+
|
105
|
+
Much better!
|
106
|
+
|
107
|
+
## Namespaces & Collections
|
108
|
+
|
109
|
+
Superform uses a different syntax for namespacing and collections than Rails, which can be a bit confusing since the same terminology is used but the application is slightly different.
|
110
|
+
|
111
|
+
Consider a form for an account that lets people edit the names and email of the owner and users of an account.
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
class AccountForm < Superform::Rails::Form
|
115
|
+
def template
|
116
|
+
# Account#owner returns a single object
|
117
|
+
namespace :owner do |owner|
|
118
|
+
# Renders input with the name `account[owner][name]`
|
119
|
+
render owner.field(:name).input
|
120
|
+
# Renders input with the name `account[owner][email]`
|
121
|
+
render owner.field(:email).input(type: :email)
|
122
|
+
end
|
123
|
+
|
124
|
+
# Account#members returns a collection of objects
|
125
|
+
collection(:members).each do |member|
|
126
|
+
# Renders input with the name `account[members][0][name]`,
|
127
|
+
# `account[members][1][name]`, ...
|
128
|
+
render member.field(:name).input
|
129
|
+
# Renders input with the name `account[members][0][email]`,
|
130
|
+
# `account[members][1][email]`, ...
|
131
|
+
render member.field(:email).input(type: :email)
|
132
|
+
|
133
|
+
# Member#permissions returns an array of values like
|
134
|
+
# ["read", "write", "delete"].
|
135
|
+
member.field(:permissions).collection do |permission|
|
136
|
+
# Renders input with the name `account[members][0][permissions][]`,
|
137
|
+
# `account[members][1][permissions][]`, ...
|
138
|
+
render permission.label do
|
139
|
+
plain permisson.value.humanize
|
140
|
+
render permission.checkbox
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
One big difference between Superform and Rails is the `collection` methods require the use of the `each` method to enumerate over each item in the collection.
|
149
|
+
|
150
|
+
There's three different types of namespaces and collections to consider:
|
151
|
+
|
152
|
+
1. **Namespace** - `namespace(:field_name)` is used to map form fields to a single object that's a child of another object. In ActiveRecord, this could be a `has_one` or `belongs_to` relationship.
|
153
|
+
|
154
|
+
2. **Collection** - `collection(:field_name).each` is used to map a collection of objects to a form. In this case, the members of the account. In ActiveRecord, this could be a `has_many` relationship.
|
155
|
+
|
156
|
+
3. **Field Collection** - `field(:field_name).collection.each` is used when the value of a field is enumerable, like an array of values. In ActiveRecord, this could be an attribute that's an Array type.
|
157
|
+
|
158
|
+
### Change a form's root namespace
|
159
|
+
|
160
|
+
By default Superform namespaces a form based on the ActiveModel model name param key.
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
class UserForm < Superform::Rails::Form
|
164
|
+
def template
|
165
|
+
render field(:email).input
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
render LoginForm.new(User.new)
|
170
|
+
# Renders input with the name `user[email]`
|
171
|
+
|
172
|
+
render LoginForm.new(Admin::User.new)
|
173
|
+
# Renders input with the name `admin_user[email]`
|
174
|
+
```
|
175
|
+
|
176
|
+
To customize the form namespace, like an ActiveRecord model nested within a module, the `key` method can be overriden.
|
177
|
+
|
178
|
+
```ruby
|
179
|
+
class UserForm < Superform::Rails::Form
|
180
|
+
def template
|
181
|
+
render field(:email).input
|
182
|
+
end
|
183
|
+
|
184
|
+
def key
|
185
|
+
"user"
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
render UserForm.new(User.new)
|
190
|
+
# Renders input with the name `user[email]`
|
191
|
+
|
192
|
+
render UserForm.new(Admin::User.new)
|
193
|
+
# This will also render inputs with the name `user[email]`
|
194
|
+
```
|
195
|
+
|
196
|
+
## Form field guide
|
197
|
+
|
198
|
+
Superform tries to strike a balance between "being as close to HTML forms as possible" and not requiring a lot of boilerplate to create forms. This example is contrived, but it shows all the different ways you can render a form.
|
199
|
+
|
200
|
+
In practice, many of the calls below you'd put inside of a method. This cuts down on the number of `render` calls in your HTML code and further reduces boilerplate.
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
# Everything below is intentionally verbose!
|
204
|
+
class SignupForm < ApplicationForm
|
205
|
+
def template
|
206
|
+
# The most basic type of input, which will be autofocused.
|
207
|
+
render field(:name).input.focus
|
208
|
+
|
209
|
+
# Input field with a lot more options on it.
|
210
|
+
render field(:email).input(type: :email, placeholder: "We will sell this to third parties", required: true)
|
211
|
+
|
212
|
+
# You can put fields in a block if that's your thing.
|
213
|
+
render field(:reason) do |f|
|
214
|
+
div do
|
215
|
+
f.label { "Why should we care about you?" }
|
216
|
+
f.textarea(row: 3, col: 80)
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
# Let's get crazy with Selects. They can accept values as simple as 2 element arrays.
|
221
|
+
div do
|
222
|
+
render field(:contact).label { "Would you like us to spam you to death?" }
|
223
|
+
render field(:contact).select(
|
224
|
+
[true, "Yes"], # <option value="true">Yes</option>
|
225
|
+
[false, "No"], # <option value="false">No</option>
|
226
|
+
"Hell no", # <option value="Hell no">Hell no</option>
|
227
|
+
nil # <option></option>
|
228
|
+
)
|
229
|
+
end
|
230
|
+
|
231
|
+
div do
|
232
|
+
render field(:source).label { "How did you hear about us?" }
|
233
|
+
render field(:source).select do |s|
|
234
|
+
# Pretend WebSources is an ActiveRecord scope with a "Social" category that has "Facebook, X, etc"
|
235
|
+
# and a "Search" category with "AltaVista, Yahoo, etc."
|
236
|
+
WebSources.select(:id, :name).group_by(:category) do |category, sources|
|
237
|
+
s.optgroup(label: category) do
|
238
|
+
s.options(sources)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
div do
|
245
|
+
render field(:agreement).label { "Check this box if you agree to give us your first born child" }
|
246
|
+
render field(:agreement).checkbox(checked: true)
|
247
|
+
end
|
248
|
+
|
249
|
+
render button { "Submit" }
|
250
|
+
end
|
251
|
+
end
|
252
|
+
```
|
253
|
+
|
254
|
+
### Upload fields
|
255
|
+
If you want to add file upload fields to your form you will need to initialize your form with the `enctype` attribute set to `multipart/form-data` as shown in the following example code:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
class User::ImageForm < ApplicationForm
|
259
|
+
def template
|
260
|
+
# render label
|
261
|
+
render field(:image).label { "Choose file" }
|
262
|
+
# render file input with accept attribute for png and jpeg images
|
263
|
+
render field(:image).input(type: "file", accept: "image/png, image/jpeg")
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
# IMPORTANT
|
268
|
+
# When rendering the form remember to init the User::ImageForm like that
|
269
|
+
render User::ImageForm.new(@usermodel, enctype: "multipart/form-data")
|
270
|
+
```
|
271
|
+
|
272
|
+
|
273
|
+
## Extending Superforms
|
274
|
+
|
275
|
+
The best part? If you have forms with a completely different look and feel, you can extend the forms just like you would a Ruby class:
|
276
|
+
|
277
|
+
```ruby
|
278
|
+
class AdminForm < ApplicationForm
|
279
|
+
class AdminInput < ApplicationComponent
|
280
|
+
def template(&)
|
281
|
+
input(**attributes)
|
282
|
+
small { admin_tool_tip_for field.key }
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
class Field < Field
|
287
|
+
def tooltip_input(**attributes)
|
288
|
+
AdminInput.new(self, attributes: attributes)
|
289
|
+
end
|
290
|
+
end
|
291
|
+
end
|
292
|
+
```
|
293
|
+
|
294
|
+
Then, just like you did in your Erb, you create the form:
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
class Admin::Users::Form < AdminForm
|
298
|
+
def template(&)
|
299
|
+
labeled field(:name).tooltip_input
|
300
|
+
labeled field(:email).tooltip_input(type: :email)
|
301
|
+
|
302
|
+
submit "Save"
|
303
|
+
end
|
304
|
+
end
|
305
|
+
```
|
306
|
+
|
307
|
+
Since Superforms are just Ruby objects, you can organize them however you want. You can keep your view component classes embedded in your Superform file if you prefer for everything to be in one place, keep the forms in the `app/views/forms/*.rb` folder and the components in `app/views/forms/**/*_component.rb`, use Ruby's `include` and `extend` features to modify different form classes, or put them in a gem and share them with an entire organization or open source community. It's just Ruby code!
|
308
|
+
|
309
|
+
## Automatic strong parameters
|
310
|
+
|
311
|
+
Guess what? Superform eliminates the need for Strong Parameters in Rails by assigning the values of the `params` hash _through_ your form via the `assign` method. Here's what it looks like.
|
312
|
+
|
313
|
+
```ruby
|
314
|
+
class PostsController < ApplicationController
|
315
|
+
include Superform::Rails::StrongParameters
|
316
|
+
|
317
|
+
def create
|
318
|
+
@post = assign params.require(:post), to: Posts::Form.new(Post.new)
|
319
|
+
|
320
|
+
if @post.save
|
321
|
+
# Success path
|
322
|
+
else
|
323
|
+
# Error path
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
def update
|
328
|
+
@post = Post.find(params[:id])
|
329
|
+
|
330
|
+
assign params.require(:post), to: Posts::Form.new(@post)
|
331
|
+
|
332
|
+
if @post.save
|
333
|
+
# Success path
|
334
|
+
else
|
335
|
+
# Error path
|
336
|
+
end
|
337
|
+
end
|
338
|
+
end
|
339
|
+
```
|
340
|
+
|
341
|
+
How does it work? An instance of the form is created, then the hash is assigned to it. If the params include data outside of what a form accepts, it will be ignored.
|
342
|
+
|
343
|
+
## Comparisons
|
344
|
+
|
345
|
+
Rails ships with a lot of great options to make forms. Many of these inspired Superform. The tl;dr:
|
346
|
+
|
347
|
+
1. Rails has a lot of great form helpers. Simple Form and Formtastic both have concise ways of defining HTML forms, but do require frequently opening and closing Erb tags.
|
348
|
+
|
349
|
+
2. Superform is uniquely capable of permitting its own controller parameters, leaving you with one less thing to worry about and test. Additionally it can be extended, shared, and modularized since its Plain' 'ol Ruby, which opens up a world of TailwindCSS form libraries and proprietary form libraries developed internally by organizations.
|
350
|
+
|
351
|
+
### Rails form helpers
|
352
|
+
|
353
|
+
Rails form helpers have lasted for almost 20 years and are super solid, but things get tricky when your application starts to take on different styles of forms. To manage it all you have to cobble together helper methods, partials, and templates. Additionally, the structure of the form then has to be expressed to the controller as strong params, forcing you to repeat yourself.
|
354
|
+
|
355
|
+
With Superform, you build the entire form with Ruby code, so you avoid the Erb gymnastics and helper method soup that it takes in Rails to scale up forms in an organization.
|
356
|
+
|
357
|
+
### Simple Form
|
358
|
+
|
359
|
+
I built some pretty amazing applications with Simple Form and admire its syntax. It requires "Erb soup", which is an opening and closing line of Erb per line. If you follow a specific directory structure or use their component framework, you can get pretty far, but you'll hit a wall when you need to start putting wrappers around forms or inputs.
|
360
|
+
|
361
|
+
https://github.com/heartcombo/simple_form#the-wrappers-api
|
362
|
+
|
363
|
+
The API is there, but when you change the syntax, you have to reboot the server to see the changes. UI development should be reflected immediately when the page is reloaded, which is what Superforms can do.
|
364
|
+
|
365
|
+
Like Rails form helpers, it doesn't self-permit parameters.
|
366
|
+
|
367
|
+
https://www.ruby-toolbox.com/projects/simple_form
|
368
|
+
|
369
|
+
### Formtastic
|
370
|
+
|
371
|
+
Formtastic gives us a nice DSL inside of Erb that we can use to create forms, but like Simple Form, there's a lot of opening and closing Erb tags that make the syntax clunky.
|
372
|
+
|
373
|
+
It has generators that give you Ruby objects that represent HTML form inputs that you can customize, but its limited to very specific parts of the HTML components. Superform lets you customize every aspect of the HTML in your form elements.
|
374
|
+
|
375
|
+
It also does not permit its own parameters.
|
376
|
+
|
377
|
+
https://www.ruby-toolbox.com/projects/formtastic
|
378
|
+
|
379
|
+
## Development
|
380
|
+
|
381
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
382
|
+
|
383
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
384
|
+
|
385
|
+
## Contributing
|
386
|
+
|
387
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/rubymonolith/superform. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/rubymonolith/superform/blob/main/CODE_OF_CONDUCT.md).
|
388
|
+
|
389
|
+
## License
|
390
|
+
|
391
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
392
|
+
|
393
|
+
## Code of Conduct
|
394
|
+
|
395
|
+
Everyone interacting in the Superform project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/rubymonolith/superform/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
require "standard/rake"
|
6
|
+
|
7
|
+
task default: %i[test standard]
|
8
|
+
|
9
|
+
# https://juincc.medium.com/how-to-setup-minitest-for-your-gems-development-f29c4bee13c2
|
10
|
+
Rake::TestTask.new do |t|
|
11
|
+
t.libs << "test"
|
12
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
13
|
+
t.verbose = true
|
14
|
+
end
|
data/config.ru
ADDED