superform 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +31 -43
- data/lib/superform/rails.rb +18 -0
- data/lib/superform/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b87df41698b91049bed9e30f976d56b92c5c694762a63803453d29fa47c025a9
|
4
|
+
data.tar.gz: bd9ec4dd8499de8303411c3ee88f7a2888ce285b5592eb32829db6043782a4f9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f945ca1d2a57d3b4ac69d064c72f701cd7d3d524a64dd241c80a23b3fd53cecd572800f2ebbe760321348d0f770b60d60be5acb4a4482e007d9040d21b3898f
|
7
|
+
data.tar.gz: a47df25dcc990f3162b874f85999df79e0acf170643395071aaa2b2c29fde23ff302a055a9ed132927945cc53c30376f645846370f5e323eb89115dc740136ce
|
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,47 @@ 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. Here's what it looks like.
|
152
148
|
|
153
149
|
```ruby
|
154
|
-
class
|
155
|
-
|
150
|
+
class PostsController < ApplicationController
|
151
|
+
include Superform::Rails::StrongParameters
|
156
152
|
|
157
|
-
|
153
|
+
def create
|
154
|
+
@post = assign params.require(:post), to: Post.new
|
158
155
|
|
159
|
-
|
160
|
-
|
156
|
+
if @post.save
|
157
|
+
# Success path
|
158
|
+
else
|
159
|
+
# Error path
|
160
|
+
end
|
161
161
|
end
|
162
|
-
end
|
163
|
-
```
|
164
162
|
|
165
|
-
|
163
|
+
def update
|
164
|
+
@post = Post.find(params[:id])
|
166
165
|
|
167
|
-
|
168
|
-
class UsersController < ApplicationController
|
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
|
166
|
+
assign params.require(:post), to: @post
|
173
167
|
|
174
|
-
|
168
|
+
if @post.save
|
169
|
+
# Success path
|
170
|
+
else
|
171
|
+
# Error path
|
172
|
+
end
|
175
173
|
end
|
176
174
|
|
177
|
-
before_action :assign_form
|
178
|
-
|
179
|
-
# Your actions
|
180
|
-
|
181
175
|
private
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
@form.permit params
|
189
|
-
end
|
176
|
+
# Defaults to `Posts::Form`, but you can override it here if
|
177
|
+
# you uncomment and add your own class. You could also pass the
|
178
|
+
# `form: FormClass` into the `assign` method.
|
179
|
+
#
|
180
|
+
# def form_class
|
181
|
+
# end
|
190
182
|
end
|
191
183
|
```
|
192
184
|
|
193
|
-
|
194
|
-
|
195
|
-
```
|
196
|
-
<%= render @form %>
|
197
|
-
```
|
185
|
+
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
186
|
|
199
187
|
## Comparisons
|
200
188
|
|
data/lib/superform/rails.rb
CHANGED
@@ -100,6 +100,24 @@ module Superform
|
|
100
100
|
end
|
101
101
|
end
|
102
102
|
|
103
|
+
module StrongParameters
|
104
|
+
protected
|
105
|
+
# Assigns params to the form model.
|
106
|
+
def assign(params, to:, form: form_class)
|
107
|
+
to.tap do |model|
|
108
|
+
form.new(model).tap do |form|
|
109
|
+
# TODO: Figure out how to render this in a way that doesn't concat a string; just throw everything away.
|
110
|
+
render_to_string form
|
111
|
+
form.assign params
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# Defaults to the form defined in `./app/views/*/form.rb`.
|
117
|
+
def form_class
|
118
|
+
self.controller_name.camelize.constantize::Form
|
119
|
+
end
|
120
|
+
end
|
103
121
|
|
104
122
|
module Components
|
105
123
|
class FieldComponent < ApplicationComponent
|
data/lib/superform/version.rb
CHANGED