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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3cc4c3770a5f974a8d12b494dc0c62674f61d17e566585a145e4d02abcc02047
4
- data.tar.gz: 04b1a10d9d7a2349d31117362cfcc1a12b854dfe9e8e666e5fd67534fea1cc3d
3
+ metadata.gz: 625660f353f992d6002a6dc8c21912cd9f21bd28e63a0b64f9b135d10ce6bb7d
4
+ data.tar.gz: b6c3bf4c93146a80676c8c6f4bc05d5059b6f45d14fa2ff6a7bcb2005671fc6e
5
5
  SHA512:
6
- metadata.gz: dbb5d91b7e49b8357e33fd4f3178ea658b51fa44e46496c4e03fffc1c4801155f8953c80a223cc1d8c4dc2d3d48ea59f51b711f17d64ea2fba31e4344c8949e4
7
- data.tar.gz: 70ee4cbbd71fb9e995d9d03a7c5c556ee6e62ddc1beb2d06826a071511d2052a711b0dc453c3e4f25f984092783c3c56b749b68a34ff55104609a7393ec4444e
6
+ metadata.gz: 3dd0f0dd8f6ceed91419c6dad2234c72adcb12fcd4262d6b1309064ec3c67b5427af434e375b73ec02fd117b0d6f16df27f1c7b80e9091189d080a6e53582c1d
7
+ data.tar.gz: ab30922107e8d7258e6610240703a351edc02d264c8a56087e2ff35205c0e080aab997b06c621d0f22b3c8de0f758a3f3e098cff9b2982f3fee94ee6a4f245f5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- superform (0.4.6)
4
+ superform (0.4.7)
5
5
  phlex-rails (~> 1.0)
6
6
  zeitwerk (~> 2.6)
7
7
 
data/README.md CHANGED
@@ -104,9 +104,60 @@ Then render it from Erb.
104
104
 
105
105
  Much better!
106
106
 
107
- ### Namespacing forms
107
+ ## Namespaces & Collections
108
108
 
109
- By default Superform will namespace a form based on the ActiveModel model name param key.
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
- # This will render inputs with the name `user[email]`
170
+ # Renders input with the name `user[email]`
120
171
 
121
172
  render LoginForm.new(Admin::User.new)
122
- # This will render inputs with the name `admin_user[email]`
173
+ # Renders input with the name `admin_user[email]`
123
174
  ```
124
175
 
125
- If you want to customize the form namespace, you can override the `key` method in your form.
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
- # This will render inputs with the name `user[email]`
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
 
@@ -128,7 +128,11 @@ module Superform
128
128
  end
129
129
 
130
130
  def _method_field_value
131
- @method || @model.persisted? ? "patch" : "post"
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
- { id: dom.id, name: dom.name, value: dom.value, type: type }
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Superform
4
- VERSION = "0.4.6"
4
+ VERSION = "0.4.7"
5
5
  end
data/lib/superform.rb CHANGED
@@ -305,4 +305,4 @@ end
305
305
 
306
306
  def Superform(...)
307
307
  Superform::Namespace.root(...)
308
- end
308
+ end
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.6
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-03-04 00:00:00.000000000 Z
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.3
87
+ rubygems_version: 3.5.6
88
88
  signing_key:
89
89
  specification_version: 4
90
90
  summary: Build forms in Rails