action_args 1.4.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +4 -2
- data/README.md +33 -12
- data/action_args.gemspec +1 -0
- data/gemfiles/rails_32.gemfile +1 -1
- data/gemfiles/rails_40.gemfile +2 -1
- data/gemfiles/rails_41.gemfile +0 -1
- data/gemfiles/rails_42.gemfile +13 -0
- data/lib/action_args.rb +1 -0
- data/lib/action_args/callbacks.rb +71 -0
- data/lib/action_args/version.rb +1 -1
- data/lib/generators/rails/templates/controller.rb +11 -9
- data/spec/controllers/hooks_spec.rb +31 -0
- data/spec/fake_app.rb +33 -1
- metadata +27 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1be64c562566fa50677fd49064390945ef09e08
|
4
|
+
data.tar.gz: 579d02790193ebe0bb9bd3fc032ca29785b1f28c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcbe4c44a809e0fde4684c288c56f00b5595ffad2a38b6afd0bf2e14820d8694c949bb00f3b0c37bc3e5c8fb4ae1b88c48625e20fedbf52536b3c98c913ce960
|
7
|
+
data.tar.gz: 81d8cc112ced51eee850a17bd6f58441ecce23b7c8a976b0ab980d053a26b4cf85688b9096174e6ad34c4d7ac902d55e384bb06fec8a4f7582ef9498686fe05d
|
data/.travis.yml
CHANGED
@@ -4,15 +4,17 @@ rvm:
|
|
4
4
|
- 1.9.3
|
5
5
|
- 2.0.0
|
6
6
|
- 2.1
|
7
|
+
- 2.2
|
7
8
|
- ruby-head
|
8
9
|
- jruby
|
9
|
-
- rbx
|
10
|
+
- rbx
|
10
11
|
matrix:
|
11
12
|
allow_failures:
|
12
13
|
- rvm: ruby-head
|
13
14
|
- rvm: jruby
|
14
|
-
- rvm: rbx
|
15
|
+
- rvm: rbx
|
15
16
|
gemfile:
|
16
17
|
- gemfiles/rails_32.gemfile
|
17
18
|
- gemfiles/rails_40.gemfile
|
18
19
|
- gemfiles/rails_41.gemfile
|
20
|
+
- gemfiles/rails_42.gemfile
|
data/README.md
CHANGED
@@ -21,7 +21,7 @@ end
|
|
21
21
|
```
|
22
22
|
|
23
23
|
Hitting "/hoge/fuga?piyo=foo" will call `fuga('foo')` and output 'foo'.
|
24
|
-
This allows you to explicitly state which members of the `params` Hash are used in your controller actions
|
24
|
+
This allows you to explicitly state which members of the `params` Hash are used in your controller actions.
|
25
25
|
|
26
26
|
|
27
27
|
## Method parameter types in Ruby, and how ActionArgs handles parameters
|
@@ -124,6 +124,25 @@ class MembersController < ApplicationController
|
|
124
124
|
end
|
125
125
|
```
|
126
126
|
|
127
|
+
## Filters
|
128
|
+
|
129
|
+
ActionArgs works in filters, in the same way as it works in controller actions.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
class UsersController < ApplicationController
|
133
|
+
before_action :set_user, only: :show
|
134
|
+
|
135
|
+
def show
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
# `params[:id]` will be dynamically assigned to the method parameter `id` here
|
140
|
+
def set_user(id)
|
141
|
+
@user = User.find(id)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
127
146
|
## The Scaffold Generator
|
128
147
|
|
129
148
|
ActionArgs provides a custom scaffold controller generator that overwrites the default scaffold generator.
|
@@ -139,6 +158,7 @@ The following elegant controller code will be generated:
|
|
139
158
|
# coding: utf-8
|
140
159
|
|
141
160
|
class UsersController < ApplicationController
|
161
|
+
before_action :set_user, only: [:show, :edit, :update, :destroy]
|
142
162
|
permits :name, :age, :email
|
143
163
|
|
144
164
|
# GET /users
|
@@ -147,8 +167,7 @@ class UsersController < ApplicationController
|
|
147
167
|
end
|
148
168
|
|
149
169
|
# GET /users/1
|
150
|
-
def show
|
151
|
-
@user = User.find(id)
|
170
|
+
def show
|
152
171
|
end
|
153
172
|
|
154
173
|
# GET /users/new
|
@@ -157,8 +176,7 @@ class UsersController < ApplicationController
|
|
157
176
|
end
|
158
177
|
|
159
178
|
# GET /users/1/edit
|
160
|
-
def edit
|
161
|
-
@user = User.find(id)
|
179
|
+
def edit
|
162
180
|
end
|
163
181
|
|
164
182
|
# POST /users
|
@@ -173,9 +191,7 @@ class UsersController < ApplicationController
|
|
173
191
|
end
|
174
192
|
|
175
193
|
# PUT /users/1
|
176
|
-
def update(
|
177
|
-
@user = User.find(id)
|
178
|
-
|
194
|
+
def update(user)
|
179
195
|
if @user.update_attributes(user)
|
180
196
|
redirect_to @user, notice: 'User was successfully updated.'
|
181
197
|
else
|
@@ -184,12 +200,17 @@ class UsersController < ApplicationController
|
|
184
200
|
end
|
185
201
|
|
186
202
|
# DELETE /users/1
|
187
|
-
def destroy
|
188
|
-
@user = User.find(id)
|
203
|
+
def destroy
|
189
204
|
@user.destroy
|
190
205
|
|
191
206
|
redirect_to users_url
|
192
207
|
end
|
208
|
+
|
209
|
+
private
|
210
|
+
# Use callbacks to share common setup or constraints between actions.
|
211
|
+
def set_user(id)
|
212
|
+
@user = User.find(id)
|
213
|
+
end
|
193
214
|
end
|
194
215
|
```
|
195
216
|
|
@@ -201,9 +222,9 @@ You may notice that
|
|
201
222
|
|
202
223
|
## Supported versions
|
203
224
|
|
204
|
-
* Ruby 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.0 (trunk), JRuby, & Rubinius with 1.9+ mode
|
225
|
+
* Ruby 1.9.2, 1.9.3, 2.0.0, 2.1.x, 2.2.x, 2.3.0 (trunk), JRuby, & Rubinius with 1.9+ mode
|
205
226
|
|
206
|
-
* Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, 4.2 (edge)
|
227
|
+
* Rails 3.0.x, 3.1.x, 3.2.x, 4.0.x, 4.1.x, 4.2.x, 5.0 (edge)
|
207
228
|
|
208
229
|
|
209
230
|
## Installation
|
data/action_args.gemspec
CHANGED
data/gemfiles/rails_32.gemfile
CHANGED
data/gemfiles/rails_40.gemfile
CHANGED
data/gemfiles/rails_41.gemfile
CHANGED
data/lib/action_args.rb
CHANGED
@@ -0,0 +1,71 @@
|
|
1
|
+
module ActiveSupport
|
2
|
+
module Callbacks
|
3
|
+
class Callback
|
4
|
+
if Rails.version > '4.1'
|
5
|
+
# Extending AS::Callbacks::Callback's `make_lambda` not just to call specified
|
6
|
+
# method but to call the method with method parameters taken from `params`.
|
7
|
+
# This would happen only when
|
8
|
+
# * the filter was defined in Symbol form
|
9
|
+
# * the target object is_a ActionController object
|
10
|
+
def make_lambda_with_method_parameters(filter)
|
11
|
+
if Symbol === filter
|
12
|
+
lambda do |target, _, &blk|
|
13
|
+
if ActionController::Base === target
|
14
|
+
meth = target.method filter
|
15
|
+
method_parameters = meth.parameters
|
16
|
+
ActionArgs::ParamsHandler.strengthen_params!(target.class, method_parameters, target.params)
|
17
|
+
values = ActionArgs::ParamsHandler.extract_method_arguments_from_params method_parameters, target.params
|
18
|
+
target.send filter, *values, &blk
|
19
|
+
else
|
20
|
+
target.send filter, &blk
|
21
|
+
end
|
22
|
+
end
|
23
|
+
else
|
24
|
+
make_lambda_without_method_parameters filter
|
25
|
+
end
|
26
|
+
end
|
27
|
+
alias_method_chain :make_lambda, :method_parameters
|
28
|
+
|
29
|
+
elsif Rails.version > '4.0'
|
30
|
+
def apply_with_method_parameters(code)
|
31
|
+
if (Symbol === @filter) && (@klass < ActionController::Base)
|
32
|
+
method_body = <<-FILTER
|
33
|
+
meth = method :#{@filter}
|
34
|
+
method_parameters = meth.parameters
|
35
|
+
ActionArgs::ParamsHandler.strengthen_params!(self.class, method_parameters, params)
|
36
|
+
values = ActionArgs::ParamsHandler.extract_method_arguments_from_params method_parameters, params
|
37
|
+
send :#{@filter}, *values
|
38
|
+
FILTER
|
39
|
+
if @kind == :before
|
40
|
+
@filter = "begin\n#{method_body}\nend"
|
41
|
+
else
|
42
|
+
@filter = method_body.chomp
|
43
|
+
end
|
44
|
+
end
|
45
|
+
apply_without_method_parameters code
|
46
|
+
end
|
47
|
+
alias_method_chain :apply, :method_parameters
|
48
|
+
|
49
|
+
else # Rails 3.2
|
50
|
+
def start_with_method_parameters(key=nil, object=nil)
|
51
|
+
if (Symbol === @filter) && (@klass < ActionController::Base)
|
52
|
+
method_body = <<-FILTER
|
53
|
+
meth = method :#{@filter}
|
54
|
+
method_parameters = meth.parameters
|
55
|
+
ActionArgs::ParamsHandler.strengthen_params!(self.class, method_parameters, params)
|
56
|
+
values = ActionArgs::ParamsHandler.extract_method_arguments_from_params method_parameters, params
|
57
|
+
send :#{@filter}, *values
|
58
|
+
FILTER
|
59
|
+
if @kind == :before
|
60
|
+
@filter = "begin\n#{method_body}\nend"
|
61
|
+
else
|
62
|
+
@filter = method_body.chomp
|
63
|
+
end
|
64
|
+
end
|
65
|
+
start_without_method_parameters key, object
|
66
|
+
end
|
67
|
+
alias_method_chain :start, :method_parameters
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/action_args/version.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
|
3
3
|
class <%= controller_class_name %>Controller < ApplicationController
|
4
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
|
4
5
|
<% if defined? ActionController::StrongParameters -%>
|
5
6
|
permits <%= attributes.map {|a| ":#{a.name}" }.join(', ') %>
|
6
7
|
|
@@ -11,8 +12,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
11
12
|
end
|
12
13
|
|
13
14
|
# GET <%= route_url %>/1
|
14
|
-
def show
|
15
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
15
|
+
def show
|
16
16
|
end
|
17
17
|
|
18
18
|
# GET <%= route_url %>/new
|
@@ -21,8 +21,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
21
21
|
end
|
22
22
|
|
23
23
|
# GET <%= route_url %>/1/edit
|
24
|
-
def edit
|
25
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
24
|
+
def edit
|
26
25
|
end
|
27
26
|
|
28
27
|
# POST <%= route_url %>
|
@@ -37,9 +36,7 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
37
36
|
end
|
38
37
|
|
39
38
|
# PUT <%= route_url %>/1
|
40
|
-
def update(
|
41
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
42
|
-
|
39
|
+
def update(<%= singular_table_name %>)
|
43
40
|
<% if orm_instance.respond_to? :update -%>
|
44
41
|
if @<%= orm_instance.update(singular_table_name) %>
|
45
42
|
<% else -%>
|
@@ -52,10 +49,15 @@ class <%= controller_class_name %>Controller < ApplicationController
|
|
52
49
|
end
|
53
50
|
|
54
51
|
# DELETE <%= route_url %>/1
|
55
|
-
def destroy
|
56
|
-
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
52
|
+
def destroy
|
57
53
|
@<%= orm_instance.destroy %>
|
58
54
|
|
59
55
|
redirect_to <%= index_helper %>_url, notice: <%= "'#{human_name} was successfully destroyed.'" %>
|
60
56
|
end
|
57
|
+
|
58
|
+
private
|
59
|
+
# Use callbacks to share common setup or constraints between actions.
|
60
|
+
def set_<%= singular_table_name %>(id)
|
61
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, 'id') %>
|
62
|
+
end
|
61
63
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BooksController, 'action hooks' do
|
4
|
+
before do
|
5
|
+
Book.delete_all
|
6
|
+
@book = Book.create! title: 'Head First ActionArgs'
|
7
|
+
get :show, id: @book.id
|
8
|
+
end
|
9
|
+
|
10
|
+
describe 'before_action' do
|
11
|
+
context 'via Symbol' do
|
12
|
+
subject { assigns :book }
|
13
|
+
it { should == @book }
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'via String' do
|
17
|
+
subject { assigns :string_filter_executed }
|
18
|
+
it { should be_true }
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'via Proc' do
|
22
|
+
subject { assigns :proc_filter_executed }
|
23
|
+
it { should be_true }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'around_action' do
|
28
|
+
subject { assigns :elapsed_time }
|
29
|
+
it { should be }
|
30
|
+
end
|
31
|
+
end
|
data/spec/fake_app.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
1
3
|
# config
|
2
4
|
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
3
5
|
|
@@ -58,6 +60,22 @@ class AuthorsController < ApplicationController
|
|
58
60
|
end
|
59
61
|
end
|
60
62
|
class BooksController < ApplicationController
|
63
|
+
if Rails::VERSION::MAJOR >= 4
|
64
|
+
before_action :set_book, only: :show
|
65
|
+
before_action -> { @proc_filter_executed = true }, only: :show
|
66
|
+
before_action '@string_filter_executed = true', only: :show
|
67
|
+
around_action :benchmark_action
|
68
|
+
before_action :omg
|
69
|
+
skip_before_action :omg
|
70
|
+
else
|
71
|
+
before_filter :set_book, only: :show
|
72
|
+
before_filter -> { @proc_filter_executed = true }, only: :show
|
73
|
+
before_filter '@string_filter_executed = true', only: :show
|
74
|
+
around_filter :benchmark_action
|
75
|
+
before_filter :omg
|
76
|
+
skip_before_filter :omg
|
77
|
+
end
|
78
|
+
|
61
79
|
# optional parameter
|
62
80
|
def index(page = 1, q = nil, limit = 10)
|
63
81
|
@books = Book.limit(limit.to_i).offset(([page.to_i - 1, 0].max) * 10)
|
@@ -66,7 +84,6 @@ class BooksController < ApplicationController
|
|
66
84
|
end
|
67
85
|
|
68
86
|
def show(id)
|
69
|
-
@book = Book.find(id)
|
70
87
|
render text: @book.title
|
71
88
|
end
|
72
89
|
|
@@ -75,6 +92,21 @@ class BooksController < ApplicationController
|
|
75
92
|
@book = Book.create! book
|
76
93
|
render text: @book.title
|
77
94
|
end
|
95
|
+
|
96
|
+
private
|
97
|
+
def set_book(id)
|
98
|
+
@book = Book.find(id)
|
99
|
+
end
|
100
|
+
|
101
|
+
def benchmark_action
|
102
|
+
start = Time.now
|
103
|
+
yield
|
104
|
+
@elapsed_time = Time.now - start
|
105
|
+
end
|
106
|
+
|
107
|
+
def omg
|
108
|
+
raise '💣'
|
109
|
+
end
|
78
110
|
end
|
79
111
|
if Rails::VERSION::MAJOR >= 4
|
80
112
|
class StoresController < ApplicationController
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_args
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akira Matsuda
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,6 +38,26 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.0'
|
48
|
+
- - "<"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '2.99'
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '2.0'
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '2.99'
|
41
61
|
description: Rails plugin gem that supports Merbish style controller action arguments.
|
42
62
|
email:
|
43
63
|
- ronnie@dio.jp
|
@@ -56,8 +76,10 @@ files:
|
|
56
76
|
- gemfiles/rails_32.gemfile
|
57
77
|
- gemfiles/rails_40.gemfile
|
58
78
|
- gemfiles/rails_41.gemfile
|
79
|
+
- gemfiles/rails_42.gemfile
|
59
80
|
- lib/action_args.rb
|
60
81
|
- lib/action_args/abstract_controller.rb
|
82
|
+
- lib/action_args/callbacks.rb
|
61
83
|
- lib/action_args/params_handler.rb
|
62
84
|
- lib/action_args/version.rb
|
63
85
|
- lib/generators/action_args/rspec/scaffold/scaffold_generator.rb
|
@@ -65,6 +87,7 @@ files:
|
|
65
87
|
- lib/generators/rails/action_args_scaffold_controller_generator.rb
|
66
88
|
- lib/generators/rails/templates/controller.rb
|
67
89
|
- spec/controllers/action_args_controller_spec.rb
|
90
|
+
- spec/controllers/hooks_spec.rb
|
68
91
|
- spec/controllers/kwargs_controller_spec.rb
|
69
92
|
- spec/controllers/kwargs_keyreq_controller_spec.rb
|
70
93
|
- spec/controllers/ordinal_controller_spec.rb
|
@@ -94,12 +117,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
117
|
version: '0'
|
95
118
|
requirements: []
|
96
119
|
rubyforge_project: action_args
|
97
|
-
rubygems_version: 2.
|
120
|
+
rubygems_version: 2.4.5
|
98
121
|
signing_key:
|
99
122
|
specification_version: 4
|
100
123
|
summary: Controller action arguments parameterizer for Rails 3+ & Ruby 1.9+
|
101
124
|
test_files:
|
102
125
|
- spec/controllers/action_args_controller_spec.rb
|
126
|
+
- spec/controllers/hooks_spec.rb
|
103
127
|
- spec/controllers/kwargs_controller_spec.rb
|
104
128
|
- spec/controllers/kwargs_keyreq_controller_spec.rb
|
105
129
|
- spec/controllers/ordinal_controller_spec.rb
|