superform 0.4.5 → 0.4.6
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 +43 -7
- data/lib/generators/superform/install/install_generator.rb +9 -0
- data/lib/superform/rails.rb +9 -6
- data/lib/superform/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3cc4c3770a5f974a8d12b494dc0c62674f61d17e566585a145e4d02abcc02047
|
4
|
+
data.tar.gz: 04b1a10d9d7a2349d31117362cfcc1a12b854dfe9e8e666e5fd67534fea1cc3d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dbb5d91b7e49b8357e33fd4f3178ea658b51fa44e46496c4e03fffc1c4801155f8953c80a223cc1d8c4dc2d3d48ea59f51b711f17d64ea2fba31e4344c8949e4
|
7
|
+
data.tar.gz: 70ee4cbbd71fb9e995d9d03a7c5c556ee6e62ddc1beb2d06826a071511d2052a711b0dc453c3e4f25f984092783c3c56b749b68a34ff55104609a7393ec4444e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -54,19 +54,16 @@ Superforms are built out of [Phlex components](https://www.phlex.fun/html/compon
|
|
54
54
|
|
55
55
|
```ruby
|
56
56
|
# ./app/views/forms/application_form.rb
|
57
|
-
class ApplicationForm <
|
58
|
-
class MyInputComponent <
|
57
|
+
class ApplicationForm < Superform::Rails::Form
|
58
|
+
class MyInputComponent < Superform::Rails::Components::InputComponent
|
59
59
|
def template(&)
|
60
60
|
div class: "form-field" do
|
61
61
|
input(**attributes)
|
62
|
-
if field.errors?
|
63
|
-
p(class: "form-field-error") { field.errors.to_sentence }
|
64
|
-
end
|
65
62
|
end
|
66
63
|
end
|
67
64
|
end
|
68
65
|
|
69
|
-
class Field < Field
|
66
|
+
class Field < Superform::Rails::Form::Field
|
70
67
|
def input(**attributes)
|
71
68
|
MyInputComponent.new(self, attributes: attributes)
|
72
69
|
end
|
@@ -107,6 +104,44 @@ Then render it from Erb.
|
|
107
104
|
|
108
105
|
Much better!
|
109
106
|
|
107
|
+
### Namespacing forms
|
108
|
+
|
109
|
+
By default Superform will namespace a form based on the ActiveModel model name param key.
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
class UserForm < Superform::Rails::Form
|
113
|
+
def template
|
114
|
+
render field(:email).input
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
render LoginForm.new(User.new)
|
119
|
+
# This will render inputs with the name `user[email]`
|
120
|
+
|
121
|
+
render LoginForm.new(Admin::User.new)
|
122
|
+
# This will render inputs with the name `admin_user[email]`
|
123
|
+
```
|
124
|
+
|
125
|
+
If you want to customize the form namespace, you can override the `key` method in your form.
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
class UserForm < Superform::Rails::Form
|
129
|
+
def template
|
130
|
+
render field(:email).input
|
131
|
+
end
|
132
|
+
|
133
|
+
def key
|
134
|
+
"user"
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
render UserForm.new(User.new)
|
139
|
+
# This will render inputs with the name `user[email]`
|
140
|
+
|
141
|
+
render UserForm.new(Admin::User.new)
|
142
|
+
# This will also render inputs with the name `user[email]`
|
143
|
+
````
|
144
|
+
|
110
145
|
## Form field guide
|
111
146
|
|
112
147
|
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.
|
@@ -165,6 +200,7 @@ class SignupForm < ApplicationForm
|
|
165
200
|
end
|
166
201
|
```
|
167
202
|
|
203
|
+
|
168
204
|
## Extending Superforms
|
169
205
|
|
170
206
|
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:
|
@@ -247,7 +283,7 @@ Rails ships with a lot of great options to make forms. Many of these inspired Su
|
|
247
283
|
|
248
284
|
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.
|
249
285
|
|
250
|
-
With
|
286
|
+
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.
|
251
287
|
|
252
288
|
### Simple Form
|
253
289
|
|
@@ -1,9 +1,13 @@
|
|
1
|
+
require "bundler"
|
2
|
+
|
1
3
|
class Superform::InstallGenerator < Rails::Generators::Base
|
2
4
|
source_root File.expand_path("templates", __dir__)
|
3
5
|
|
4
6
|
APPLICATION_CONFIGURATION_PATH = Rails.root.join("config/application.rb")
|
5
7
|
|
6
8
|
def install_phlex_rails
|
9
|
+
return if gem_in_bundle? "phlex-rails"
|
10
|
+
|
7
11
|
gem "phlex-rails"
|
8
12
|
generate "phlex:install"
|
9
13
|
end
|
@@ -21,4 +25,9 @@ class Superform::InstallGenerator < Rails::Generators::Base
|
|
21
25
|
def create_application_form
|
22
26
|
template "application_form.rb", Rails.root.join("app/views/forms/application_form.rb")
|
23
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
def gem_in_bundle?(gem_name)
|
31
|
+
Bundler.load.specs.any? { |spec| spec.name == gem_name }
|
32
|
+
end
|
24
33
|
end
|
data/lib/superform/rails.rb
CHANGED
@@ -20,7 +20,6 @@ module Superform
|
|
20
20
|
:field,
|
21
21
|
:collection,
|
22
22
|
:namespace,
|
23
|
-
:key,
|
24
23
|
:assign,
|
25
24
|
:serialize,
|
26
25
|
to: :@namespace
|
@@ -29,8 +28,8 @@ module Superform
|
|
29
28
|
# in your subclass you may have something like this:
|
30
29
|
#
|
31
30
|
# ```ruby
|
32
|
-
# class MyForm
|
33
|
-
# class MyLabel < LabelComponent
|
31
|
+
# class MyForm < Superform::Rails::Form
|
32
|
+
# class MyLabel < Superform::Rails::Components::LabelComponent
|
34
33
|
# def template(&content)
|
35
34
|
# label(form: @field.dom.name, class: "text-bold", &content)
|
36
35
|
# end
|
@@ -38,7 +37,7 @@ module Superform
|
|
38
37
|
#
|
39
38
|
# class Field < Field
|
40
39
|
# def label(**attributes)
|
41
|
-
# MyLabel.new(self,
|
40
|
+
# MyLabel.new(self, **attributes)
|
42
41
|
# end
|
43
42
|
# end
|
44
43
|
# end
|
@@ -80,7 +79,7 @@ module Superform
|
|
80
79
|
@action = action
|
81
80
|
@method = method
|
82
81
|
@attributes = attributes
|
83
|
-
@namespace = Namespace.root(
|
82
|
+
@namespace = Namespace.root(key, object: model, field_class: self.class::Field)
|
84
83
|
end
|
85
84
|
|
86
85
|
def around_template(&)
|
@@ -107,6 +106,10 @@ module Superform
|
|
107
106
|
)
|
108
107
|
end
|
109
108
|
|
109
|
+
def key
|
110
|
+
@model.model_name.param_key
|
111
|
+
end
|
112
|
+
|
110
113
|
protected
|
111
114
|
def authenticity_token_field
|
112
115
|
input(
|
@@ -334,4 +337,4 @@ module Superform
|
|
334
337
|
end
|
335
338
|
end
|
336
339
|
end
|
337
|
-
end
|
340
|
+
end
|
data/lib/superform/version.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.6
|
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-03-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phlex-rails
|