superform 0.4.6 → 0.4.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +76 -7
- data/lib/superform/rails.rb +48 -15
- 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: 9d16f47143b0d36f13ce0da8685af303d7f56f219f15a0fb2e45e7bfe53d3b49
|
4
|
+
data.tar.gz: f2fcc38800000aecfa952683f48393550516b87063521c8b23510dd5058affd4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7cc285d0bb21f5df995e5adc1fc1a141af0e5854a8807930a17533a6d74f123ea843db207de163e3d202e4166d59422a97c80aabd8926b4c5140e823d8c4a8d
|
7
|
+
data.tar.gz: c9e5bb9ba72a9ca35104f41a6dacd63102e795f0cbb882b808efdd976b320b649d51e4cee2951dc7fa463ef0fd441d621e983add1746c96116a91247763e170e
|
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
@@ -14,7 +14,7 @@ module Superform
|
|
14
14
|
# The `Form` component also handles Rails authenticity tokens via the `authenticity_toklen_field`
|
15
15
|
# method and the HTTP verb via the `_method_field`.
|
16
16
|
class Form < Component
|
17
|
-
|
17
|
+
attr_accessor :model
|
18
18
|
|
19
19
|
delegate \
|
20
20
|
:field,
|
@@ -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,23 +276,52 @@ 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 has_client_provided_value?
|
288
|
+
case type.to_s
|
289
|
+
when "file", "image"
|
290
|
+
true
|
291
|
+
else
|
292
|
+
false
|
293
|
+
end
|
294
|
+
end
|
295
|
+
|
296
|
+
def value
|
297
|
+
dom.value unless has_client_provided_value?
|
276
298
|
end
|
277
299
|
|
278
300
|
def type
|
279
|
-
|
280
|
-
when URI
|
281
|
-
"url"
|
282
|
-
when Integer
|
283
|
-
"number"
|
284
|
-
when Date, DateTime
|
285
|
-
"date"
|
286
|
-
when Time
|
287
|
-
"time"
|
288
|
-
else
|
289
|
-
"text"
|
290
|
-
end
|
301
|
+
@type ||= ActiveSupport::StringInquirer.new(attribute_type || value_type)
|
291
302
|
end
|
303
|
+
|
304
|
+
protected
|
305
|
+
def value_type
|
306
|
+
case field.value
|
307
|
+
when URI
|
308
|
+
"url"
|
309
|
+
when Integer, Float
|
310
|
+
"number"
|
311
|
+
when Date, DateTime
|
312
|
+
"date"
|
313
|
+
when Time
|
314
|
+
"time"
|
315
|
+
else
|
316
|
+
"text"
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
def attribute_type
|
321
|
+
if type = @attributes[:type] || @attributes["type"]
|
322
|
+
type.to_s
|
323
|
+
end
|
324
|
+
end
|
292
325
|
end
|
293
326
|
|
294
327
|
class TextareaComponent < FieldComponent
|
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.9
|
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-06-04 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
|