action_args 2.3.0 → 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +66 -32
- data/Gemfile +1 -0
- data/README.md +83 -60
- data/action_args.gemspec +7 -3
- data/gemfiles/rails_41.gemfile +2 -1
- data/gemfiles/rails_42.gemfile +2 -1
- data/gemfiles/rails_50.gemfile +2 -7
- data/gemfiles/rails_51.gemfile +2 -7
- data/gemfiles/rails_52.gemfile +15 -0
- data/gemfiles/rails_60.gemfile +16 -0
- data/gemfiles/rails_61.gemfile +16 -0
- data/gemfiles/rails_edge.gemfile +2 -5
- data/lib/action_args.rb +1 -0
- data/lib/action_args/abstract_controller.rb +8 -3
- data/lib/action_args/callbacks.rb +18 -9
- data/lib/action_args/params_handler.rb +13 -11
- data/lib/action_args/version.rb +2 -1
- data/lib/generators/rails/action_args_scaffold_controller_generator.rb +2 -1
- data/lib/generators/rails/templates/controller.rb +9 -11
- metadata +12 -33
- data/test/controllers/action_args_controller_test.rb +0 -44
- data/test/controllers/hooks_test.rb +0 -30
- data/test/controllers/kwargs_controller_test.rb +0 -31
- data/test/controllers/kwargs_keyreq_controller_test.rb +0 -31
- data/test/controllers/ordinal_controller_test.rb +0 -10
- data/test/controllers/strong_parameters_test.rb +0 -58
- data/test/fake_app.rb +0 -184
- data/test/kwargs_controllers.rb +0 -9
- data/test/kwargs_keyreq_controllers.rb +0 -9
- data/test/mailers/action_mailer_test.rb +0 -9
- data/test/params_handler/params_handler_test.rb +0 -193
- data/test/test_helper.rb +0 -29
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8be56bb5bd7e6183a1f68f1cfeb7baa5da32287d11a50dddc53248561755ee1c
|
4
|
+
data.tar.gz: 14b0d9da9608f4d0cdc465039d202eb35eabb2abe7471f12cdf28af205d0bd01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4a112b0b811700d98a8f5c65958d1f9566b0401bcb885bcd160f2d6d7fdcbbc47b55b72e8b3258f9f3c6a461e3256e5d4c97409fe2ccc3373fbdb2deb5db55d4
|
7
|
+
data.tar.gz: d10fdf287ee66df67eee06cfd9c515bbaab7567cac69a84dbbdf11732ca86609ecafc7d437e3e2c89dff9b4001f9757edc4887061cc5d0c3d606d68629850ed6
|
data/.travis.yml
CHANGED
@@ -1,44 +1,78 @@
|
|
1
1
|
script: 'bundle exec rake -t test'
|
2
2
|
language: ruby
|
3
|
-
sudo: false
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
-
|
9
|
-
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
4
|
+
cache: bundler
|
5
|
+
|
6
|
+
before_install:
|
7
|
+
- "ruby -e 'exit RUBY_VERSION.to_f >= 2.3' && gem up --system || (gem i rubygems-update -v '<3' && update_rubygems)"
|
8
|
+
- gem up bundler
|
9
|
+
|
10
|
+
# rvm:
|
11
|
+
# - 2.6.7
|
12
|
+
# - 2.5.9
|
13
|
+
# - 2.4.10
|
14
|
+
# - 2.3.8
|
15
|
+
# - 2.2.10
|
16
|
+
# - 2.1.10
|
17
|
+
# - 2.0.0
|
18
|
+
# - ruby-head
|
19
|
+
# gemfile:
|
20
|
+
# - gemfiles/rails_52.gemfile
|
21
|
+
# - gemfiles/rails_51.gemfile
|
22
|
+
# - gemfiles/rails_50.gemfile
|
23
|
+
# - gemfiles/rails_42.gemfile
|
24
|
+
# - gemfiles/rails_41.gemfile
|
25
|
+
# - gemfiles/rails_edge.gemfile
|
26
|
+
|
14
27
|
matrix:
|
15
|
-
|
16
|
-
- rvm:
|
17
|
-
gemfile: gemfiles/
|
18
|
-
- rvm:
|
19
|
-
gemfile: gemfiles/
|
20
|
-
- rvm:
|
21
|
-
gemfile: gemfiles/rails_51.gemfile
|
22
|
-
- rvm: 2.1.10
|
23
|
-
gemfile: gemfiles/rails_51.gemfile
|
24
|
-
- rvm: 2.0.0
|
28
|
+
include:
|
29
|
+
- rvm: 3.0.1
|
30
|
+
gemfile: gemfiles/rails_61.gemfile
|
31
|
+
- rvm: 3.0.1
|
32
|
+
gemfile: gemfiles/rails_60.gemfile
|
33
|
+
- rvm: 3.0.1
|
25
34
|
gemfile: gemfiles/rails_edge.gemfile
|
26
|
-
- rvm: 2.
|
35
|
+
- rvm: 2.7.3
|
36
|
+
gemfile: gemfiles/rails_61.gemfile
|
37
|
+
- rvm: 2.7.3
|
27
38
|
gemfile: gemfiles/rails_edge.gemfile
|
28
|
-
- rvm: 2.
|
39
|
+
- rvm: 2.6.7
|
40
|
+
gemfile: gemfiles/rails_61.gemfile
|
41
|
+
- rvm: 2.6.7
|
42
|
+
gemfile: gemfiles/rails_52.gemfile
|
43
|
+
- rvm: 2.6.7
|
44
|
+
gemfile: gemfiles/rails_51.gemfile
|
45
|
+
- rvm: 2.6.7
|
46
|
+
gemfile: gemfiles/rails_50.gemfile
|
47
|
+
- rvm: 2.5.9
|
48
|
+
gemfile: gemfiles/rails_61.gemfile
|
49
|
+
- rvm: 2.5.9
|
50
|
+
gemfile: gemfiles/rails_52.gemfile
|
51
|
+
- rvm: 2.4.10
|
52
|
+
gemfile: gemfiles/rails_52.gemfile
|
53
|
+
- rvm: 2.3.8
|
54
|
+
gemfile: gemfiles/rails_52.gemfile
|
55
|
+
- rvm: 2.2.10
|
56
|
+
gemfile: gemfiles/rails_42.gemfile
|
57
|
+
- rvm: 2.2.10
|
29
58
|
gemfile: gemfiles/rails_41.gemfile
|
30
|
-
- rvm: 2.
|
59
|
+
- rvm: 2.1.10
|
31
60
|
gemfile: gemfiles/rails_42.gemfile
|
61
|
+
- rvm: 2.0.0
|
62
|
+
gemfile: gemfiles/rails_42.gemfile
|
63
|
+
|
32
64
|
- rvm: ruby-head
|
33
|
-
gemfile: gemfiles/
|
65
|
+
gemfile: gemfiles/rails_61.gemfile
|
34
66
|
- rvm: ruby-head
|
35
|
-
gemfile: gemfiles/
|
67
|
+
gemfile: gemfiles/rails_edge.gemfile
|
68
|
+
|
69
|
+
- rvm: jruby-9.2.17.0
|
70
|
+
gemfile: gemfiles/rails_52.gemfile
|
71
|
+
- rvm: rubinius-3
|
72
|
+
gemfile: gemfiles/rails_52.gemfile
|
73
|
+
|
36
74
|
allow_failures:
|
37
|
-
- rvm:
|
75
|
+
- rvm: ruby-head
|
76
|
+
- rvm: jruby-9.2.17.0
|
38
77
|
- rvm: rubinius-3
|
39
|
-
gemfile:
|
40
|
-
- gemfiles/rails_41.gemfile
|
41
|
-
- gemfiles/rails_42.gemfile
|
42
|
-
- gemfiles/rails_50.gemfile
|
43
|
-
- gemfiles/rails_51.gemfile
|
44
|
-
- gemfiles/rails_edge.gemfile
|
78
|
+
- gemfile: gemfiles/rails_edge.gemfile
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
#
|
1
|
+
# action_args
|
2
2
|
[![Build Status](https://travis-ci.org/asakusarb/action_args.svg?branch=master)](https://travis-ci.org/asakusarb/action_args)
|
3
3
|
|
4
|
-
Controller action arguments parameterizer for Rails
|
4
|
+
Controller action arguments parameterizer for Rails
|
5
5
|
|
6
6
|
|
7
|
-
## What
|
7
|
+
## What Is This?
|
8
8
|
|
9
|
-
|
9
|
+
action_args is a Rails plugin that extends your controller action methods to allow you to specify arguments of interest in the method definition for any action. - in short, this makes your Rails controller Merb-ish.
|
10
10
|
|
11
11
|
|
12
12
|
## The Controllers
|
@@ -14,24 +14,24 @@ ActionArgs is a Rails plugin that extends your controller action methods to allo
|
|
14
14
|
Having the following controller code:
|
15
15
|
|
16
16
|
```ruby
|
17
|
-
class
|
18
|
-
def
|
19
|
-
|
17
|
+
class UsersController < ApplicationController
|
18
|
+
def show(id)
|
19
|
+
@user = User.find id
|
20
20
|
end
|
21
21
|
end
|
22
22
|
```
|
23
23
|
|
24
|
-
|
25
|
-
This allows you to explicitly state which members of the `params` Hash are used in your controller actions
|
24
|
+
When a request visits "/users/777", it calls `UsersController#show` passing 777 as the method parameter.
|
25
|
+
This allows you to explicitly state the most important API for the action -- which members of the `params` Hash are used in your controller actions -- in a perfectly natural Ruby way!
|
26
26
|
|
27
27
|
|
28
|
-
## Method
|
28
|
+
## Method Parameter Types in Ruby, and How action_args Handles Parameters
|
29
29
|
|
30
30
|
### Required Parameters (:req)
|
31
|
-
Method parameters that you specify are required. If a key of the same name does not exist in the params Hash,
|
32
|
-
|
31
|
+
Method parameters that you specify are required. If a key of the same name does not exist in the params Hash, ActionContrller::BadRequest is raised.
|
32
|
+
|
33
|
+
In this `show` action, action_args will require that `id` parameter is provided.
|
33
34
|
|
34
|
-
In this `show` action, ActionArgs will require that `id` parameter is provided.
|
35
35
|
```ruby
|
36
36
|
class UsersController < ApplicationController
|
37
37
|
# the `id` parameter is mandatory
|
@@ -55,7 +55,7 @@ end
|
|
55
55
|
|
56
56
|
### Keyword Argument (:key)
|
57
57
|
If you think this Ruby 2.0 syntax reads better, you can choose this style for defining your action methods.
|
58
|
-
This just works in the same way as
|
58
|
+
This just works in the same way as `:opt` here.
|
59
59
|
|
60
60
|
```ruby
|
61
61
|
class UsersController < ApplicationController
|
@@ -67,8 +67,8 @@ end
|
|
67
67
|
```
|
68
68
|
|
69
69
|
### Required Keyword Argument (:keyreq)
|
70
|
-
|
71
|
-
You can use this syntax instead of
|
70
|
+
`:keyreq` is the required version of `:key`, which was introduced in Ruby 2.1.
|
71
|
+
You can use this syntax instead of `:req`.
|
72
72
|
|
73
73
|
```ruby
|
74
74
|
class CommentsController < ApplicationController
|
@@ -80,9 +80,9 @@ class CommentsController < ApplicationController
|
|
80
80
|
end
|
81
81
|
```
|
82
82
|
|
83
|
-
|
83
|
+
## StrongParameters - permit
|
84
84
|
|
85
|
-
|
85
|
+
action_args plays very nice with Rails 4 StrongParameters.
|
86
86
|
|
87
87
|
1. Inline declaration
|
88
88
|
|
@@ -97,17 +97,17 @@ class UsersController < ApplicationController
|
|
97
97
|
end
|
98
98
|
```
|
99
99
|
|
100
|
-
2. Declarative
|
100
|
+
2. Declarative allow-listing
|
101
101
|
|
102
|
-
|
102
|
+
action_args also provides a declarative `permits` method for controller classes.
|
103
103
|
Use this to keep your `permit` calls DRY in a comprehensible way.
|
104
104
|
|
105
105
|
```ruby
|
106
106
|
class UsersController < ApplicationController
|
107
|
-
#
|
107
|
+
# allow-lists User model's attributes
|
108
108
|
permits :name, :age
|
109
109
|
|
110
|
-
# the given `user` parameter would be automatically permitted by
|
110
|
+
# the given `user` parameter would be automatically permitted by action_args
|
111
111
|
def create(user)
|
112
112
|
@user = User.new(user)
|
113
113
|
end
|
@@ -116,18 +116,19 @@ end
|
|
116
116
|
|
117
117
|
By default, action_args deduces the target model name from the controller name.
|
118
118
|
For example, the `permits` call in `UsersController` expects the model name to be `User`.
|
119
|
-
If this is not the case, you can specify the
|
119
|
+
If this is not the case, you can specify the `:model_name` option:
|
120
120
|
|
121
121
|
```ruby
|
122
122
|
class MembersController < ApplicationController
|
123
|
-
#
|
123
|
+
# allow-lists User model's attributes
|
124
124
|
permits :name, :age, model_name: 'User'
|
125
125
|
end
|
126
126
|
```
|
127
127
|
|
128
|
+
|
128
129
|
## Filters
|
129
130
|
|
130
|
-
|
131
|
+
action_args works in filters, in the same way as it works in controller actions.
|
131
132
|
|
132
133
|
```ruby
|
133
134
|
class UsersController < ApplicationController
|
@@ -136,17 +137,44 @@ class UsersController < ApplicationController
|
|
136
137
|
def show
|
137
138
|
end
|
138
139
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
end
|
140
|
+
# `params[:id]` will be dynamically assigned to the method parameter `id` here
|
141
|
+
private def set_user(id)
|
142
|
+
@user = User.find(id)
|
143
|
+
end
|
144
144
|
end
|
145
145
|
```
|
146
146
|
|
147
|
+
|
148
|
+
## The *_params Convention
|
149
|
+
|
150
|
+
For those who are familiar with the Rails scaffold's default naming style, you can add `_params` suffix to any of the parameter names in the method signatures.
|
151
|
+
It just matches with the params name without `_params`.
|
152
|
+
|
153
|
+
For instance, these two actions both pass `params[:user]` as the method parameter.
|
154
|
+
|
155
|
+
```ruby
|
156
|
+
# without _params
|
157
|
+
def create(user)
|
158
|
+
@user = User.new(user)
|
159
|
+
...
|
160
|
+
end
|
161
|
+
```
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
# with _params
|
165
|
+
def create(user_params)
|
166
|
+
@user = User.new(user_params)
|
167
|
+
...
|
168
|
+
end
|
169
|
+
```
|
170
|
+
|
171
|
+
This naming convention makes your controller code look much more compatible with the Rails' default scaffolded code,
|
172
|
+
and so it may be actually super easy for you to manually migrate from the legacy scaffold controller to the action_args style.
|
173
|
+
|
174
|
+
|
147
175
|
## The Scaffold Generator
|
148
176
|
|
149
|
-
|
177
|
+
action_args provides a custom scaffold controller generator that overwrites the default scaffold generator.
|
150
178
|
Thus, by hitting the scaffold generator command like this:
|
151
179
|
|
152
180
|
```sh
|
@@ -157,7 +185,6 @@ The following elegant controller code will be generated:
|
|
157
185
|
|
158
186
|
```ruby
|
159
187
|
class UsersController < ApplicationController
|
160
|
-
before_action :set_user, only: [:show, :edit, :update, :destroy]
|
161
188
|
permits :name, :age, :email
|
162
189
|
|
163
190
|
# GET /users
|
@@ -166,7 +193,8 @@ class UsersController < ApplicationController
|
|
166
193
|
end
|
167
194
|
|
168
195
|
# GET /users/1
|
169
|
-
def show
|
196
|
+
def show(id)
|
197
|
+
@user = User.find(id)
|
170
198
|
end
|
171
199
|
|
172
200
|
# GET /users/new
|
@@ -175,7 +203,8 @@ class UsersController < ApplicationController
|
|
175
203
|
end
|
176
204
|
|
177
205
|
# GET /users/1/edit
|
178
|
-
def edit
|
206
|
+
def edit(id)
|
207
|
+
@user = User.find(id)
|
179
208
|
end
|
180
209
|
|
181
210
|
# POST /users
|
@@ -185,66 +214,59 @@ class UsersController < ApplicationController
|
|
185
214
|
if @user.save
|
186
215
|
redirect_to @user, notice: 'User was successfully created.'
|
187
216
|
else
|
188
|
-
render
|
217
|
+
render :new
|
189
218
|
end
|
190
219
|
end
|
191
220
|
|
192
221
|
# PUT /users/1
|
193
|
-
def update(user)
|
194
|
-
|
222
|
+
def update(id, user)
|
223
|
+
@user = User.find(id)
|
224
|
+
|
225
|
+
if @user.update(user)
|
195
226
|
redirect_to @user, notice: 'User was successfully updated.'
|
196
227
|
else
|
197
|
-
render
|
228
|
+
render :edit
|
198
229
|
end
|
199
230
|
end
|
200
231
|
|
201
232
|
# DELETE /users/1
|
202
|
-
def destroy
|
233
|
+
def destroy(id)
|
234
|
+
@user = User.find(id)
|
203
235
|
@user.destroy
|
204
236
|
|
205
|
-
redirect_to users_url
|
237
|
+
redirect_to users_url, notice: 'User was successfully destroyed.'
|
206
238
|
end
|
207
|
-
|
208
|
-
private
|
209
|
-
# Use callbacks to share common setup or constraints between actions.
|
210
|
-
def set_user(id)
|
211
|
-
@user = User.find(id)
|
212
|
-
end
|
213
239
|
end
|
214
240
|
```
|
215
241
|
|
216
242
|
You may notice that
|
217
|
-
* There are no
|
243
|
+
* There are no global-ish `params` reference
|
218
244
|
* It's quite easy to comprehend what's the actual input value for each action
|
219
|
-
* You may write the unit test code as if the actions are just normal Ruby methods
|
245
|
+
* You may be able to write the unit test code without mocking `params` as if the actions are just normal Ruby methods
|
220
246
|
|
221
247
|
|
222
|
-
## Supported
|
248
|
+
## Supported Versions
|
223
249
|
|
224
|
-
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.0 (trunk), JRuby, & Rubinius with 2.0+ mode
|
250
|
+
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.x, 2.5.x, 2.6.x, 2.7.x, 3.0.x, 3.1 (trunk), JRuby, & Rubinius with 2.0+ mode
|
225
251
|
|
226
|
-
* Rails 4.1.x, 4.2.x, 5.0, 5.1 (edge)
|
252
|
+
* Rails 4.1.x, 4.2.x, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0 (edge)
|
227
253
|
|
228
|
-
|
254
|
+
For Rails 4.0.x, please use Version 1.5.4.
|
229
255
|
|
230
256
|
|
231
257
|
## Installation
|
232
258
|
|
233
|
-
|
259
|
+
Bundle the gem to your Rails app by putting this line in your Gemfile:
|
260
|
+
|
234
261
|
```ruby
|
235
262
|
gem 'action_args'
|
236
263
|
```
|
237
264
|
|
238
|
-
Then bundle:
|
239
|
-
```sh
|
240
|
-
% bundle
|
241
|
-
```
|
242
|
-
|
243
265
|
## Notes
|
244
266
|
|
245
267
|
### Plain Old Action Methods
|
246
268
|
|
247
|
-
Of course you still can use both
|
269
|
+
Of course you still can use both Merb-like style and plain old Rails style action methods even if this plugin is loaded. `params` parameter is still alive as well. That means, this plugin won't break any existing controller API.
|
248
270
|
|
249
271
|
### Argument Naming Convention
|
250
272
|
|
@@ -266,7 +288,7 @@ class BooksController < ApplicationController
|
|
266
288
|
end
|
267
289
|
```
|
268
290
|
|
269
|
-
### Default
|
291
|
+
### Default Parameter Values
|
270
292
|
|
271
293
|
You are of course able to specify default values for action parameters such as:
|
272
294
|
|
@@ -292,6 +314,7 @@ end
|
|
292
314
|
|
293
315
|
This way, the `page` parameter will be defaulted to 1 as everyone might expect.
|
294
316
|
|
317
|
+
|
295
318
|
## Copyright
|
296
319
|
|
297
320
|
Copyright (c) 2011- Asakusa.rb. See MIT-LICENSE for further details.
|
data/action_args.gemspec
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
2
|
# frozen_string_literal: true
|
3
|
+
|
3
4
|
$:.push File.expand_path("../lib", __FILE__)
|
4
5
|
require 'action_args/version'
|
5
6
|
|
@@ -11,12 +12,15 @@ Gem::Specification.new do |s|
|
|
11
12
|
s.email = ['ronnie@dio.jp']
|
12
13
|
s.homepage = 'http://asakusa.rubyist.net/'
|
13
14
|
s.license = 'MIT'
|
15
|
+
s.metadata = {
|
16
|
+
'source_code_uri' => 'https://github.com/asakusarb/action_args'
|
17
|
+
}
|
14
18
|
s.summary = 'Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+'
|
15
19
|
s.description = 'Rails plugin gem that supports Merbish style controller action arguments.'
|
16
20
|
|
17
|
-
s.files =
|
18
|
-
|
19
|
-
|
21
|
+
s.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
23
|
+
end
|
20
24
|
s.require_paths = ["lib"]
|
21
25
|
|
22
26
|
s.add_development_dependency 'bundler'
|