superform 0.4.6 → 0.4.7
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of superform might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +76 -7
- data/lib/superform/rails.rb +24 -2
- data/lib/superform/version.rb +1 -1
- data/lib/superform.rb +1 -1
- 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: 625660f353f992d6002a6dc8c21912cd9f21bd28e63a0b64f9b135d10ce6bb7d
|
4
|
+
data.tar.gz: b6c3bf4c93146a80676c8c6f4bc05d5059b6f45d14fa2ff6a7bcb2005671fc6e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3dd0f0dd8f6ceed91419c6dad2234c72adcb12fcd4262d6b1309064ec3c67b5427af434e375b73ec02fd117b0d6f16df27f1c7b80e9091189d080a6e53582c1d
|
7
|
+
data.tar.gz: ab30922107e8d7258e6610240703a351edc02d264c8a56087e2ff35205c0e080aab997b06c621d0f22b3c8de0f758a3f3e098cff9b2982f3fee94ee6a4f245f5
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -104,9 +104,60 @@ Then render it from Erb.
|
|
104
104
|
|
105
105
|
Much better!
|
106
106
|
|
107
|
-
|
107
|
+
## Namespaces & Collections
|
108
108
|
|
109
|
-
|
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.
|
110
161
|
|
111
162
|
```ruby
|
112
163
|
class UserForm < Superform::Rails::Form
|
@@ -116,13 +167,13 @@ class UserForm < Superform::Rails::Form
|
|
116
167
|
end
|
117
168
|
|
118
169
|
render LoginForm.new(User.new)
|
119
|
-
#
|
170
|
+
# Renders input with the name `user[email]`
|
120
171
|
|
121
172
|
render LoginForm.new(Admin::User.new)
|
122
|
-
#
|
173
|
+
# Renders input with the name `admin_user[email]`
|
123
174
|
```
|
124
175
|
|
125
|
-
|
176
|
+
To customize the form namespace, like an ActiveRecord model nested within a module, the `key` method can be overriden.
|
126
177
|
|
127
178
|
```ruby
|
128
179
|
class UserForm < Superform::Rails::Form
|
@@ -136,11 +187,11 @@ class UserForm < Superform::Rails::Form
|
|
136
187
|
end
|
137
188
|
|
138
189
|
render UserForm.new(User.new)
|
139
|
-
#
|
190
|
+
# Renders input with the name `user[email]`
|
140
191
|
|
141
192
|
render UserForm.new(Admin::User.new)
|
142
193
|
# This will also render inputs with the name `user[email]`
|
143
|
-
|
194
|
+
```
|
144
195
|
|
145
196
|
## Form field guide
|
146
197
|
|
@@ -200,6 +251,24 @@ class SignupForm < ApplicationForm
|
|
200
251
|
end
|
201
252
|
```
|
202
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
|
+
|
203
272
|
|
204
273
|
## Extending Superforms
|
205
274
|
|
data/lib/superform/rails.rb
CHANGED
@@ -128,7 +128,11 @@ module Superform
|
|
128
128
|
end
|
129
129
|
|
130
130
|
def _method_field_value
|
131
|
-
@method ||
|
131
|
+
@method || resource_method_field_value
|
132
|
+
end
|
133
|
+
|
134
|
+
def resource_method_field_value
|
135
|
+
@model.persisted? ? "patch" : "post"
|
132
136
|
end
|
133
137
|
|
134
138
|
def submit_value
|
@@ -272,7 +276,25 @@ module Superform
|
|
272
276
|
end
|
273
277
|
|
274
278
|
def field_attributes
|
275
|
-
{
|
279
|
+
{
|
280
|
+
id: dom.id,
|
281
|
+
name: dom.name,
|
282
|
+
type: type,
|
283
|
+
value: value
|
284
|
+
}
|
285
|
+
end
|
286
|
+
|
287
|
+
def client_provided_value?
|
288
|
+
case type
|
289
|
+
when "file", "image"
|
290
|
+
false
|
291
|
+
else
|
292
|
+
true
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
def value
|
297
|
+
dom.value unless client_provided_value?
|
276
298
|
end
|
277
299
|
|
278
300
|
def type
|
data/lib/superform/version.rb
CHANGED
data/lib/superform.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: superform
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brad Gessler
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phlex-rails
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
- !ruby/object:Gem::Version
|
85
85
|
version: '0'
|
86
86
|
requirements: []
|
87
|
-
rubygems_version: 3.5.
|
87
|
+
rubygems_version: 3.5.6
|
88
88
|
signing_key:
|
89
89
|
specification_version: 4
|
90
90
|
summary: Build forms in Rails
|