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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e755c08731146b7a40c49b9eeaee5b267b56476764ce97fdb68ae8392e5cad0a
4
- data.tar.gz: b369765812de40bf81c672ee7f131e4bc8c0a4c234baf210096a41c175f02505
3
+ metadata.gz: b87df41698b91049bed9e30f976d56b92c5c694762a63803453d29fa47c025a9
4
+ data.tar.gz: bd9ec4dd8499de8303411c3ee88f7a2888ce285b5592eb32829db6043782a4f9
5
5
  SHA512:
6
- metadata.gz: 014cdef2d3639b063ab13f6d757f68c2eb4b14d3f5c89b51301deae77302c3e6ad11a38c21cd974a2fc4ae22d5f4db57d6b698ff1042ac495aaa13c6bccdaff1
7
- data.tar.gz: ad6fd81bc9e597d14b63e2915b0c9ba820da4fa71b7db2f129db25ebee8ded0bc836e2353f1799d8565db689f77a0e2dccc73c9a875336a0dedcf5548b8bf169
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
- ## Installation
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
- > **Note**
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 model: @post %>
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 model: @user %>
104
+ <%= render Users::Form.new @user %>
106
105
  ```
107
106
 
108
107
  Much better!
109
108
 
110
- ### Extending Superforms
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
- ### Automatic strong parameters
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 also permits form fields for you in your controller, like this:
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 UserController < ApplicationController
155
- # Your actions
150
+ class PostsController < ApplicationController
151
+ include Superform::Rails::StrongParameters
156
152
 
157
- private
153
+ def create
154
+ @post = assign params.require(:post), to: Post.new
158
155
 
159
- def permitted_params
160
- @form.permit params
156
+ if @post.save
157
+ # Success path
158
+ else
159
+ # Error path
160
+ end
161
161
  end
162
- end
163
- ```
164
162
 
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:
163
+ def update
164
+ @post = Post.find(params[:id])
166
165
 
167
- ```ruby
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
- button(type: :submit) { "Sign up" }
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
- def assign_form
184
- @form = Form.new(model: @user)
185
- end
186
-
187
- def permitted_params
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
- Then render it from your Erb in less lines, like this:
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
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Superform
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler