superform 0.4.6 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3cc4c3770a5f974a8d12b494dc0c62674f61d17e566585a145e4d02abcc02047
4
- data.tar.gz: 04b1a10d9d7a2349d31117362cfcc1a12b854dfe9e8e666e5fd67534fea1cc3d
3
+ metadata.gz: 643e70b30f9cc8c74210e4d5312d7c0244e789065f41393d7e51925734fb5a1c
4
+ data.tar.gz: f96d2d84b65d93d1b330946b7b68f16b019ac87a6a4fb016a6edb2b77f66c5a8
5
5
  SHA512:
6
- metadata.gz: dbb5d91b7e49b8357e33fd4f3178ea658b51fa44e46496c4e03fffc1c4801155f8953c80a223cc1d8c4dc2d3d48ea59f51b711f17d64ea2fba31e4344c8949e4
7
- data.tar.gz: 70ee4cbbd71fb9e995d9d03a7c5c556ee6e62ddc1beb2d06826a071511d2052a711b0dc453c3e4f25f984092783c3c56b749b68a34ff55104609a7393ec4444e
6
+ metadata.gz: 13d6666058ca3235fb6a2b858a55c1b7b72a5e337d7a77e3828e01a36689c9965bafa20db1b78f62302672b93b174f33a40f9a3bf69085359afc7a7cb0016f1e
7
+ data.tar.gz: 7813f51fecc29744ba90fe20ffb9314d4c676d5f9512a18827a8e217003481335d1ec8e1d44d70351e545452022b4f0f2af14208d12617f170d8c33e04281b83
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.8)
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,23 +276,52 @@ 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 has_client_provided_value?
288
+ case type.to_sym
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
- case field.value
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
@@ -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.8"
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.8
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-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex-rails