action_args 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +2 -2
- data/README.md +207 -0
- data/action_args.gemspec +0 -2
- data/gemfiles/rails_32.gemfile +8 -0
- data/gemfiles/rails_40.gemfile +8 -0
- data/lib/action_args/abstract_controller.rb +28 -22
- data/lib/action_args/version.rb +1 -1
- metadata +4 -20
- data/README.rdoc +0 -193
data/.travis.yml
CHANGED
data/README.md
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
# ActionArgs
|
2
|
+
|
3
|
+
Controller action arguments parameterizer for Rails 3+
|
4
|
+
|
5
|
+
|
6
|
+
## What is this?
|
7
|
+
|
8
|
+
ActionArgs is a Rails plugin that extends your controller action methods to look and act like simple general Ruby methods with meaningful parameters, or in short, Merbish.
|
9
|
+
|
10
|
+
|
11
|
+
## The Controllers
|
12
|
+
|
13
|
+
Having the following controller code:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class HogeController < ApplicationController
|
17
|
+
def fuga(piyo)
|
18
|
+
render :text => piyo
|
19
|
+
end
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
Hitting "/hoge/fuga?piyo=foo" will call fuga('foo') and output 'foo'.
|
24
|
+
So, you do never need to touch the ugly `params` Hash in order to fetch the request parameters.
|
25
|
+
|
26
|
+
|
27
|
+
## StrongParameters
|
28
|
+
|
29
|
+
ActionArgs plays very nice with Rails 4 StrongParameters.
|
30
|
+
|
31
|
+
In this `show` action, ActionArgs `require`s the `id` parameter.
|
32
|
+
Hence, raises an error if the `id` value has not been specified, in the same way as usual Ruby methods do.
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class UsersController < ApplicationController
|
36
|
+
# the `id` parameter is mandatory
|
37
|
+
def show(id)
|
38
|
+
@user = User.find id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
If you don't want ActionArgs to check the existence of some action parameters, you can make them optional by defining their default values.
|
44
|
+
Again, it just acts in the same way as usual Ruby methods do.
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
class UsersController < ApplicationController
|
48
|
+
# the `page` parameter is optional
|
49
|
+
def index(page = nil)
|
50
|
+
@users = User.page(page).per(50)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
Hashes in the action method arguments simply respond to the StrongParameters' `permit` method.
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class UsersController < ApplicationController
|
59
|
+
def create(user)
|
60
|
+
@user = User.new(user.permit(:name, :age))
|
61
|
+
...
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
Moreover, ActionArgs provides declarative `permits` method for controller classes,
|
67
|
+
so that you can DRY up your `permit` calls in the most comprehensible way.
|
68
|
+
The `permits` method assumes the model class from the controller name, and
|
69
|
+
`permit`s the action arguments containing attributes for that model.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class UsersController < ApplicationController
|
73
|
+
# white-lists User model's attributes
|
74
|
+
permits :name, :age
|
75
|
+
|
76
|
+
# the given `user` parameter would be automatically permitted by ActionArgs
|
77
|
+
def create(user)
|
78
|
+
@user = User.new(user)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
|
84
|
+
## The Scaffold Generator
|
85
|
+
|
86
|
+
ActionArgs provides a custom scaffold controller generator that overwrites the default scaffold generator.
|
87
|
+
Thus, by hitting the scaffold generator command like this:
|
88
|
+
|
89
|
+
```sh
|
90
|
+
% rails g scaffold user name age:integer email
|
91
|
+
```
|
92
|
+
|
93
|
+
The following beautiful controller code will be generated:
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
# coding: utf-8
|
97
|
+
|
98
|
+
class UsersController < ApplicationController
|
99
|
+
permits :name, :age, :email
|
100
|
+
|
101
|
+
# GET /users
|
102
|
+
def index
|
103
|
+
@users = User.all
|
104
|
+
end
|
105
|
+
|
106
|
+
# GET /users/1
|
107
|
+
def show(id)
|
108
|
+
@user = User.find(id)
|
109
|
+
end
|
110
|
+
|
111
|
+
# GET /users/new
|
112
|
+
def new
|
113
|
+
@user = User.new
|
114
|
+
end
|
115
|
+
|
116
|
+
# GET /users/1/edit
|
117
|
+
def edit(id)
|
118
|
+
@user = User.find(id)
|
119
|
+
end
|
120
|
+
|
121
|
+
# POST /users
|
122
|
+
def create(user)
|
123
|
+
@user = User.new(user)
|
124
|
+
|
125
|
+
if @user.save
|
126
|
+
redirect_to @user, notice: 'User was successfully created.'
|
127
|
+
else
|
128
|
+
render action: 'new'
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# PUT /users/1
|
133
|
+
def update(id, user)
|
134
|
+
@user = User.find(id)
|
135
|
+
|
136
|
+
if @user.update_attributes(user)
|
137
|
+
redirect_to @user, notice: 'User was successfully updated.'
|
138
|
+
else
|
139
|
+
render action: 'edit'
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
# DELETE /users/1
|
144
|
+
def destroy(id)
|
145
|
+
@user = User.find(id)
|
146
|
+
@user.destroy
|
147
|
+
|
148
|
+
redirect_to users_url
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
153
|
+
You may notice that
|
154
|
+
* There are no globalish `params` referrence
|
155
|
+
* It's quite easy to comprehend what's the actual input value for each action
|
156
|
+
* You may write the unit test code as if the actions are just normal Ruby methods
|
157
|
+
|
158
|
+
|
159
|
+
## Supported versions
|
160
|
+
|
161
|
+
* Ruby 1.9.2, 1.9.3, 2.0.0 (trunk), JRuby & Rubinius with 1.9 mode
|
162
|
+
|
163
|
+
* Rails 3.0.x, 3.1.x, 3.2.x, 4.0 (edge)
|
164
|
+
|
165
|
+
|
166
|
+
## Installation
|
167
|
+
|
168
|
+
Put this line in your Gemfile:
|
169
|
+
```ruby
|
170
|
+
gem 'action_args'
|
171
|
+
```
|
172
|
+
|
173
|
+
Then bundle:
|
174
|
+
```sh
|
175
|
+
% bundle
|
176
|
+
```
|
177
|
+
|
178
|
+
## Notes
|
179
|
+
|
180
|
+
### Plain Old Action Methods
|
181
|
+
|
182
|
+
Of courese you still can use both Merbish 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.
|
183
|
+
|
184
|
+
### Argument Naming Convention
|
185
|
+
|
186
|
+
Each action method parameter name corresponds to `params` key name. For example, the following beautifully written nested show action works perfectly (this might not be a very good example of effective querying, but that's another story).
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
Rails.application.routes.draw do
|
190
|
+
resources :authors do
|
191
|
+
resources :books
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
class BooksController < ApplicationController
|
196
|
+
# GET /authors/:author_id/books/:id
|
197
|
+
def show(author_id, id)
|
198
|
+
@book = Author.find(author_id).books.find(id)
|
199
|
+
end
|
200
|
+
...
|
201
|
+
end
|
202
|
+
```
|
203
|
+
|
204
|
+
|
205
|
+
## Copyright
|
206
|
+
|
207
|
+
Copyright (c) 2011 Asakusa.rb. See MIT-LICENSE for further details.
|
data/action_args.gemspec
CHANGED
@@ -18,6 +18,4 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
-
|
22
|
-
s.add_development_dependency 'sqlite3', ['>= 0']
|
23
21
|
end
|
data/gemfiles/rails_32.gemfile
CHANGED
data/gemfiles/rails_40.gemfile
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
module AbstractController
|
2
2
|
class Base
|
3
|
-
|
4
|
-
|
3
|
+
if defined? ActionController::StrongParameters
|
4
|
+
def send_action(method_name, *args)
|
5
|
+
return send method_name, *args unless args.empty?
|
5
6
|
|
6
|
-
values = if defined? ActionController::StrongParameters
|
7
7
|
target_model_name = self.class.name.sub(/Controller$/, '').singularize.underscore.to_sym
|
8
8
|
permitted_attributes = self.class.instance_variable_get '@permitted_attributes'
|
9
|
-
method(method_name).parameters.reject {|type, _| type == :block }.map do |type, key|
|
9
|
+
values = method(method_name).parameters.reject {|type, _| type == :block }.map do |type, key|
|
10
10
|
params.require key if type == :req
|
11
11
|
if (key == target_model_name) && permitted_attributes
|
12
12
|
params[key].try :permit, *permitted_attributes
|
@@ -14,26 +14,32 @@ module AbstractController
|
|
14
14
|
params[key]
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
18
|
-
method(method_name).parameters.reject {|type, _| type == :block }.map(&:last).map {|key| params[key]}
|
17
|
+
send method_name, *values
|
19
18
|
end
|
20
|
-
send method_name, *values
|
21
|
-
end
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
20
|
+
# You can configure StrongParameters' `permit` attributes using this DSL method.
|
21
|
+
# The `permit` call will be invoked only against parameters having the resource
|
22
|
+
# model name inferred from the controller class name.
|
23
|
+
#
|
24
|
+
# class UsersController < ApplicationController
|
25
|
+
# permits :name, :age
|
26
|
+
#
|
27
|
+
# def create(user)
|
28
|
+
# @user = User.new(user)
|
29
|
+
# end
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
def self.permits(*attributes)
|
33
|
+
@permitted_attributes = attributes
|
34
|
+
end
|
35
|
+
# no StrongParameters
|
36
|
+
else
|
37
|
+
def send_action(method_name, *args)
|
38
|
+
return send method_name, *args unless args.empty?
|
39
|
+
|
40
|
+
values = method(method_name).parameters.reject {|type, _| type == :block }.map {|_, key| params[key]}
|
41
|
+
send method_name, *values
|
42
|
+
end
|
37
43
|
end
|
38
44
|
end
|
39
45
|
end
|
data/lib/action_args/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_args
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,24 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-11-
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: sqlite3
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
|
-
requirements:
|
19
|
-
- - ! '>='
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
version: '0'
|
22
|
-
type: :development
|
23
|
-
prerelease: false
|
24
|
-
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ! '>='
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '0'
|
12
|
+
date: 2012-11-30 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
30
14
|
description: Rails plugin gem that supports Merbish style controller action arguments.
|
31
15
|
email:
|
32
16
|
- ronnie@dio.jp
|
@@ -38,7 +22,7 @@ files:
|
|
38
22
|
- .travis.yml
|
39
23
|
- Gemfile
|
40
24
|
- MIT-LICENSE
|
41
|
-
- README.
|
25
|
+
- README.md
|
42
26
|
- Rakefile
|
43
27
|
- action_args.gemspec
|
44
28
|
- gemfiles/rails_32.gemfile
|
data/README.rdoc
DELETED
@@ -1,193 +0,0 @@
|
|
1
|
-
= ActionArgs
|
2
|
-
|
3
|
-
Controller action arguments parameterizer for Rails 3+
|
4
|
-
|
5
|
-
|
6
|
-
== What is this?
|
7
|
-
|
8
|
-
ActionArgs is a Rails plugin that extends your controller action methods to look and act like simple general Ruby methods with meaningful parameters, or in short, Merbish.
|
9
|
-
|
10
|
-
|
11
|
-
== The Controllers
|
12
|
-
|
13
|
-
Having the following controller code:
|
14
|
-
|
15
|
-
class HogeController < ApplicationController
|
16
|
-
def fuga(piyo)
|
17
|
-
render :text => piyo
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
Hitting "/hoge/fuga?piyo=foo" will call fuga('foo') and output 'foo'.
|
22
|
-
So, you do never need to touch the ugly +params+ Hash in order to fetch the request parameters.
|
23
|
-
|
24
|
-
|
25
|
-
== StrongParameters
|
26
|
-
|
27
|
-
ActionArgs plays very nice with Rails 4 StrongParameters.
|
28
|
-
|
29
|
-
In this `show` action, ActionArgs `require`s the `id` parameter.
|
30
|
-
Hence, raises an error if the `id` value has not been specified, in the same way as usual Ruby methods do.
|
31
|
-
|
32
|
-
class UsersController < ApplicationController
|
33
|
-
# the `id` parameter is mandatory
|
34
|
-
def show(id)
|
35
|
-
@user = User.find id
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
If you don't want ActionArgs to check the existence of some action parameters, you can make them optional by defining their default values.
|
40
|
-
Again, it just acts in the same way as usual Ruby methods do.
|
41
|
-
|
42
|
-
class UsersController < ApplicationController
|
43
|
-
# the `page` parameter is optional
|
44
|
-
def index(page = nil)
|
45
|
-
@users = User.page(page).per(50)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
Hashes in the action method arguments simply respond to the StrongParameters' `permit` method.
|
50
|
-
|
51
|
-
class UsersController < ApplicationController
|
52
|
-
def create(user)
|
53
|
-
@user = User.new(user.permit(:name, :age))
|
54
|
-
...
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
Moreover, ActionArgs provides declarative `permits` method for controller classes,
|
59
|
-
so that you can DRY up your `permit` calls in the most comprehensible way.
|
60
|
-
The `permits` method assumes the model class from the controller name, and
|
61
|
-
`permit`s the action arguments containing attributes for that model.
|
62
|
-
|
63
|
-
class UsersController < ApplicationController
|
64
|
-
# white-lists User model's attributes
|
65
|
-
permits :name, :age
|
66
|
-
|
67
|
-
# the given `user` parameter would be automatically permitted by ActionArgs
|
68
|
-
def create(user)
|
69
|
-
@user = User.new(user)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
|
74
|
-
== The Scaffold Generator
|
75
|
-
|
76
|
-
ActionArgs provides a custom scaffold controller generator that overwrites the default scaffold generator.
|
77
|
-
Thus, by hitting the following command:
|
78
|
-
|
79
|
-
% rails g scaffold user name age:integer email
|
80
|
-
|
81
|
-
The following beautiful controller code will be generated:
|
82
|
-
|
83
|
-
# coding: utf-8
|
84
|
-
|
85
|
-
class UsersController < ApplicationController
|
86
|
-
permits :name, :age, :email
|
87
|
-
|
88
|
-
# GET /users
|
89
|
-
def index
|
90
|
-
@users = User.all
|
91
|
-
end
|
92
|
-
|
93
|
-
# GET /users/1
|
94
|
-
def show(id)
|
95
|
-
@user = User.find(id)
|
96
|
-
end
|
97
|
-
|
98
|
-
# GET /users/new
|
99
|
-
def new
|
100
|
-
@user = User.new
|
101
|
-
end
|
102
|
-
|
103
|
-
# GET /users/1/edit
|
104
|
-
def edit(id)
|
105
|
-
@user = User.find(id)
|
106
|
-
end
|
107
|
-
|
108
|
-
# POST /users
|
109
|
-
def create(user)
|
110
|
-
@user = User.new(user)
|
111
|
-
|
112
|
-
if @user.save
|
113
|
-
redirect_to @user, notice: 'User was successfully created.'
|
114
|
-
else
|
115
|
-
render action: 'new'
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
# PUT /users/1
|
120
|
-
def update(id, user)
|
121
|
-
@user = User.find(id)
|
122
|
-
|
123
|
-
if @user.update_attributes(user)
|
124
|
-
redirect_to @user, notice: 'User was successfully updated.'
|
125
|
-
else
|
126
|
-
render action: 'edit'
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
# DELETE /users/1
|
131
|
-
def destroy(id)
|
132
|
-
@user = User.find(id)
|
133
|
-
@user.destroy
|
134
|
-
|
135
|
-
redirect_to users_url
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
You may notice that
|
140
|
-
* There are no globalish +params+ referrence
|
141
|
-
* It's quite easy to comprehend what's the actual input value for each action
|
142
|
-
* You may write the unit test code as if the actions are just normal Ruby methods
|
143
|
-
|
144
|
-
|
145
|
-
== Supported versions
|
146
|
-
|
147
|
-
* Ruby 1.9.2, 1.9.3, 2.0.0 (trunk)
|
148
|
-
|
149
|
-
* Rails 3.0.x, 3.1.x, 3.2.x, 4.0 (edge)
|
150
|
-
|
151
|
-
|
152
|
-
== Installation
|
153
|
-
|
154
|
-
Put this line in your Gemfile:
|
155
|
-
gem 'action_args'
|
156
|
-
|
157
|
-
Then bundle:
|
158
|
-
% bundle
|
159
|
-
|
160
|
-
|
161
|
-
== Notes
|
162
|
-
|
163
|
-
=== Plain Old Action Methods
|
164
|
-
|
165
|
-
Of courese you still can use both Merbish 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.
|
166
|
-
|
167
|
-
=== Argument Naming Convention
|
168
|
-
|
169
|
-
Each action method parameter name corresponds to +params+ key name. For example, the following beautifully written nested show action works perfectly (this might not be a very good example of effective querying, but that's another story).
|
170
|
-
|
171
|
-
Rails.application.routes.draw do
|
172
|
-
resources :authors do
|
173
|
-
resources :books
|
174
|
-
end
|
175
|
-
end
|
176
|
-
|
177
|
-
class BooksController < ApplicationController
|
178
|
-
# GET /authors/:author_id/books/:id
|
179
|
-
def show(author_id, id)
|
180
|
-
@book = Author.find(author_id).books.find(id)
|
181
|
-
end
|
182
|
-
...
|
183
|
-
end
|
184
|
-
|
185
|
-
|
186
|
-
== Todo
|
187
|
-
|
188
|
-
* other Ruby implementations
|
189
|
-
|
190
|
-
|
191
|
-
== Copyright
|
192
|
-
|
193
|
-
Copyright (c) 2011 Asakusa.rb. See MIT-LICENSE for further details.
|