superform 0.2.0 → 0.4.0
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 +25 -45
- data/lib/superform/rails.rb +11 -0
- 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: 764fb4731b8ae1977a1e05ce0410302e5f29f3c008c4e0326f9b3f852118cb37
|
4
|
+
data.tar.gz: 6d7887d4b7b3bbf2af8ebfe7e675a155061e4982231d3f52caefd7a5e4c4c85d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edb3acf775edcaacb269b0aff7200d6bfd22d2c5701eb7dd0c96149bbc515acf33b77f586809ac574520a4330c1ed1acbe304e8c54d60a2b219e28b72179321b
|
7
|
+
data.tar.gz: e2b69ed000da6eeddeb3a47f664abfe052bbd85ae0e5649d23131b134d5d68acc9032fd4660260207540724c5fa10b85d6c6b4609484849e7c480a9fbd90bae1
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -10,10 +10,9 @@ Superform aims to be the best way to build forms in Rails applications. Here's w
|
|
10
10
|
|
11
11
|
It's a complete rewrite of Rails form's internals that's inspired by Reactive component design patterns.
|
12
12
|
|
13
|
-
|
13
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/0e4dfe2a1ece26e3a59e/maintainability)](https://codeclimate.com/github/rubymonolith/superform/maintainability) [![Ruby](https://github.com/rubymonolith/superform/actions/workflows/main.yml/badge.svg)](https://github.com/rubymonolith/superform/actions/workflows/main.yml)
|
14
14
|
|
15
|
-
|
16
|
-
> This doesn't actually work yet. There is working source code at https://github.com/rubymonolith/demo/tree/main/app/views/superform that's being extracted into a gem. This repo and README exist to validate some ideas before the gem is finalized and published.
|
15
|
+
## Installation
|
17
16
|
|
18
17
|
Add to the Rails application's Gemfile by executing:
|
19
18
|
|
@@ -45,7 +44,7 @@ Then render it in your templates. Here's what it looks like from an Erb file.
|
|
45
44
|
|
46
45
|
```erb
|
47
46
|
<h1>New post</h1>
|
48
|
-
<%= render Posts::Form.new
|
47
|
+
<%= render Posts::Form.new @post %>
|
49
48
|
```
|
50
49
|
|
51
50
|
## Customization
|
@@ -102,12 +101,12 @@ end
|
|
102
101
|
Then render it from Erb.
|
103
102
|
|
104
103
|
```erb
|
105
|
-
<%= render Users::Form.new
|
104
|
+
<%= render Users::Form.new @user %>
|
106
105
|
```
|
107
106
|
|
108
107
|
Much better!
|
109
108
|
|
110
|
-
|
109
|
+
## Extending Superforms
|
111
110
|
|
112
111
|
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:
|
113
112
|
|
@@ -143,58 +142,39 @@ end
|
|
143
142
|
|
144
143
|
Since Superforms are just Ruby objects, you can organize them however you want. You can keep your view component classes embedded in your Superform file if you prefer for everythign to be in one place, keep the forms in the `app/views/forms/*.rb` folder and the components in `app/views/forms/**/*_component.rb`, use Ruby's `include` and `extend` features to modify different form classes, or put them in a gem and share them with an entire organization or open source community. It's just Ruby code!
|
145
144
|
|
146
|
-
|
147
|
-
|
148
|
-
> **Note**
|
149
|
-
> THese docs are a work in progress. Strong params do work, but not as documented below. Stay tuned for updates.
|
145
|
+
## Automatic strong parameters
|
150
146
|
|
151
|
-
Guess what? Superform
|
147
|
+
Guess what? Superform eliminates then need for Strong Parameters in Rails by assigning the values of the `params` hash _through_ your form via the `assign` method. Here's what it looks like.
|
152
148
|
|
153
149
|
```ruby
|
154
|
-
class
|
155
|
-
|
156
|
-
|
157
|
-
private
|
158
|
-
|
159
|
-
def permitted_params
|
160
|
-
@form.permit params
|
161
|
-
end
|
162
|
-
end
|
163
|
-
```
|
164
|
-
|
165
|
-
To do that though you need to move the form as an inline class into your controller or `app/views` folder, which is pretty easy:
|
150
|
+
class PostsController < ApplicationController
|
151
|
+
include Superform::Rails::StrongParameters
|
166
152
|
|
167
|
-
|
168
|
-
|
169
|
-
# You could also put this in `./app/views/users/form.rb`
|
170
|
-
class Form < ApplicationForm
|
171
|
-
render field(:email).input(type: :email)
|
172
|
-
render field(:name).input
|
153
|
+
def create
|
154
|
+
@post = assign params.require(:post), to: Posts::Form.new(Post.new)
|
173
155
|
|
174
|
-
|
156
|
+
if @post.save
|
157
|
+
# Success path
|
158
|
+
else
|
159
|
+
# Error path
|
160
|
+
end
|
175
161
|
end
|
176
162
|
|
177
|
-
|
178
|
-
|
179
|
-
# Your actions
|
163
|
+
def update
|
164
|
+
@post = Post.find(params[:id])
|
180
165
|
|
181
|
-
|
182
|
-
|
183
|
-
def assign_form
|
184
|
-
@form = Form.new(model: @user)
|
185
|
-
end
|
166
|
+
assign params.require(:post), to: Posts::Form.new(@post)
|
186
167
|
|
187
|
-
|
188
|
-
|
168
|
+
if @post.save
|
169
|
+
# Success path
|
170
|
+
else
|
171
|
+
# Error path
|
172
|
+
end
|
189
173
|
end
|
190
174
|
end
|
191
175
|
```
|
192
176
|
|
193
|
-
|
194
|
-
|
195
|
-
```
|
196
|
-
<%= render @form %>
|
197
|
-
```
|
177
|
+
How does it work? An instance of the form is created, then the hash is assigned to it. If the params include data outside of what a form accepts, it will be ignored.
|
198
178
|
|
199
179
|
## Comparisons
|
200
180
|
|
data/lib/superform/rails.rb
CHANGED
@@ -100,6 +100,17 @@ module Superform
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
+
module StrongParameters
|
104
|
+
protected
|
105
|
+
# Assigns params to the form and returns the model.
|
106
|
+
def assign(params, to:)
|
107
|
+
form = to
|
108
|
+
# TODO: Figure out how to render this in a way that doesn't concat a string; just throw everything away.
|
109
|
+
render_to_string form
|
110
|
+
form.assign params
|
111
|
+
form.model
|
112
|
+
end
|
113
|
+
end
|
103
114
|
|
104
115
|
module Components
|
105
116
|
class FieldComponent < ApplicationComponent
|
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
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brad Gessler
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: phlex-rails
|