action_args 2.0.0 → 2.1.0
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 +17 -7
- data/README.md +2 -2
- data/action_args.gemspec +0 -2
- data/gemfiles/rails_50.gemfile +15 -0
- data/lib/action_args/abstract_controller.rb +14 -7
- data/lib/action_args/callbacks.rb +3 -1
- data/lib/action_args/params_handler.rb +12 -12
- data/lib/action_args/version.rb +1 -1
- data/test/controllers/action_args_controller_test.rb +4 -4
- data/test/controllers/hooks_test.rb +1 -1
- data/test/controllers/kwargs_controller_test.rb +3 -3
- data/test/controllers/kwargs_keyreq_controller_test.rb +3 -3
- data/test/controllers/ordinal_controller_test.rb +1 -1
- data/test/controllers/strong_parameters_test.rb +16 -4
- data/test/fake_app.rb +19 -10
- data/test/kwargs_controllers.rb +1 -1
- data/test/kwargs_keyreq_controllers.rb +1 -1
- data/test/test_helper.rb +14 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e8f99c507ba7d9a77264851459c344095c6b3368
|
4
|
+
data.tar.gz: 8ae5a3690c88d10ff55270705ad37558b66a1efc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7b0b63137e252a121d71d202891841548bb43b40daae9289112811b42b4cc1afe4477c3fe73ca81d572e8bd7f4bdfd41d95fcb1aa78219af3feabc407918437
|
7
|
+
data.tar.gz: 16ad6ad19f3530e1773bc9f567d80a8c0c5b6366a9ba8e1c69bf6e3611d4817eaa58ba888d50c05766079fd1ca46fd131914007ae0c4838af0654c4ba2c43bcf
|
data/.travis.yml
CHANGED
@@ -4,21 +4,31 @@ sudo: false
|
|
4
4
|
|
5
5
|
rvm:
|
6
6
|
- 2.0.0
|
7
|
-
- 2.1
|
8
|
-
- 2.2.
|
7
|
+
- 2.1.10
|
8
|
+
- 2.2.5
|
9
|
+
- 2.3.1
|
9
10
|
- ruby-head
|
10
|
-
- jruby-9.
|
11
|
-
- rbx
|
11
|
+
- jruby-9.1.0.0
|
12
|
+
- rbx-3
|
12
13
|
matrix:
|
13
14
|
exclude:
|
15
|
+
- rvm: ruby-head
|
16
|
+
gemfile: gemfiles/rails_41.gemfile
|
17
|
+
- rvm: ruby-head
|
18
|
+
gemfile: gemfiles/rails_42.gemfile
|
19
|
+
- rvm: 2.0.0
|
20
|
+
gemfile: gemfiles/rails_50.gemfile
|
21
|
+
- rvm: 2.1.10
|
22
|
+
gemfile: gemfiles/rails_50.gemfile
|
14
23
|
- rvm: 2.0.0
|
15
24
|
gemfile: gemfiles/rails_edge.gemfile
|
16
|
-
- rvm: 2.1
|
25
|
+
- rvm: 2.1.10
|
17
26
|
gemfile: gemfiles/rails_edge.gemfile
|
18
27
|
allow_failures:
|
19
|
-
- rvm: jruby-9.
|
20
|
-
- rvm: rbx
|
28
|
+
- rvm: jruby-9.1.0.0
|
29
|
+
- rvm: rbx-3
|
21
30
|
gemfile:
|
22
31
|
- gemfiles/rails_41.gemfile
|
23
32
|
- gemfiles/rails_42.gemfile
|
33
|
+
- gemfiles/rails_50.gemfile
|
24
34
|
- gemfiles/rails_edge.gemfile
|
data/README.md
CHANGED
@@ -220,9 +220,9 @@ You may notice that
|
|
220
220
|
|
221
221
|
## Supported versions
|
222
222
|
|
223
|
-
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.0 (trunk), JRuby, & Rubinius with 2.0+ mode
|
223
|
+
* Ruby 2.0.0, 2.1.x, 2.2.x, 2.3.x, 2.4.0 (trunk), JRuby, & Rubinius with 2.0+ mode
|
224
224
|
|
225
|
-
* Rails 4.1.x, 4.2.x, 5.0 (edge)
|
225
|
+
* Rails 4.1.x, 4.2.x, 5.0, 5.1 (edge)
|
226
226
|
|
227
227
|
Please use Version 1.5.4 for Rails 4.0.x.
|
228
228
|
|
data/action_args.gemspec
CHANGED
@@ -12,8 +12,6 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.summary = 'Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+'
|
13
13
|
s.description = 'Rails plugin gem that supports Merbish style controller action arguments.'
|
14
14
|
|
15
|
-
s.rubyforge_project = 'action_args'
|
16
|
-
|
17
15
|
s.files = `git ls-files`.split("\n")
|
18
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
@@ -1,15 +1,19 @@
|
|
1
1
|
require_relative 'params_handler'
|
2
2
|
using ActionArgs::ParamsHandler
|
3
3
|
|
4
|
-
module
|
5
|
-
|
4
|
+
module ActionArgs
|
5
|
+
module AbstractControllerMethods
|
6
6
|
def send_action(method_name, *args)
|
7
|
-
return
|
8
|
-
return
|
7
|
+
return super unless args.empty?
|
8
|
+
return super unless defined?(params)
|
9
9
|
|
10
|
-
|
10
|
+
strengthen_params! method_name
|
11
|
+
values = extract_method_arguments_from_params method_name
|
12
|
+
super method_name, *values
|
11
13
|
end
|
14
|
+
end
|
12
15
|
|
16
|
+
module AbstractControllerClassMethods
|
13
17
|
# You can configure StrongParameters' `permit` attributes using this DSL method.
|
14
18
|
# The `permit` call will be invoked only against parameters having the resource
|
15
19
|
# model name inferred from the controller class name.
|
@@ -22,8 +26,8 @@ module AbstractController
|
|
22
26
|
# end
|
23
27
|
# end
|
24
28
|
#
|
25
|
-
def
|
26
|
-
if attributes.last.is_a?(Hash) && attributes.last.extractable_options? && attributes.last.
|
29
|
+
def permits(*attributes)
|
30
|
+
if attributes.last.is_a?(Hash) && attributes.last.extractable_options? && attributes.last.key?(:model_name)
|
27
31
|
options = attributes.pop
|
28
32
|
@permitting_model_name = options[:model_name]
|
29
33
|
end
|
@@ -31,3 +35,6 @@ module AbstractController
|
|
31
35
|
end
|
32
36
|
end
|
33
37
|
end
|
38
|
+
|
39
|
+
AbstractController::Base.send :prepend, ActionArgs::AbstractControllerMethods
|
40
|
+
AbstractController::Base.singleton_class.send :include, ActionArgs::AbstractControllerClassMethods
|
@@ -12,7 +12,9 @@ module ActionArgs
|
|
12
12
|
if Symbol === filter
|
13
13
|
lambda do |target, _, &blk|
|
14
14
|
if ActionController::Base === target
|
15
|
-
target.
|
15
|
+
target.strengthen_params! filter
|
16
|
+
values = target.extract_method_arguments_from_params filter
|
17
|
+
target.send filter, *values, &blk
|
16
18
|
else
|
17
19
|
target.send filter, &blk
|
18
20
|
end
|
@@ -1,12 +1,6 @@
|
|
1
1
|
module ActionArgs
|
2
2
|
module ParamsHandler
|
3
3
|
refine AbstractController::Base do
|
4
|
-
def send_with_method_parameters_from_params(method_name, &blk)
|
5
|
-
strengthen_params! method_name
|
6
|
-
values = extract_method_arguments_from_params method_name
|
7
|
-
send method_name, *values, &blk
|
8
|
-
end
|
9
|
-
|
10
4
|
# converts the request params Hash into an Array to be passed into the target Method
|
11
5
|
def extract_method_arguments_from_params(method_name)
|
12
6
|
method_parameters = method(method_name).parameters
|
@@ -15,24 +9,29 @@ module ActionArgs
|
|
15
9
|
method_parameters.reverse_each do |type, key|
|
16
10
|
case type
|
17
11
|
when :req
|
18
|
-
missing_required_params << key unless params.
|
12
|
+
missing_required_params << key unless params.key? key
|
19
13
|
next
|
20
14
|
when :keyreq
|
21
|
-
if params.
|
15
|
+
if params.key? key
|
22
16
|
kwargs[key] = params[key]
|
23
17
|
else
|
24
18
|
missing_required_params << key
|
25
19
|
end
|
26
20
|
when :key
|
27
|
-
kwargs[key] = params[key] if params.
|
21
|
+
kwargs[key] = params[key] if params.key? key
|
28
22
|
when :opt
|
29
|
-
break if params.
|
23
|
+
break if params.key? key
|
30
24
|
end
|
31
25
|
# omitting parameters that are :block, :rest, :opt without a param, and :key without a param
|
32
26
|
parameter_names.delete key
|
33
27
|
end
|
34
28
|
if missing_required_params.any?
|
35
|
-
|
29
|
+
message = "Missing required parameters at #{self.class.name}##{method_name}: #{missing_required_params.join(', ')}"
|
30
|
+
if Rails.version > '5'
|
31
|
+
raise ActionController::BadRequest.new message
|
32
|
+
else
|
33
|
+
raise ActionController::BadRequest.new :required, ArgumentError.new(message)
|
34
|
+
end
|
36
35
|
end
|
37
36
|
|
38
37
|
values = parameter_names.map {|k| params[k]}
|
@@ -50,7 +49,8 @@ module ActionArgs
|
|
50
49
|
method_parameters = method(method_name).parameters
|
51
50
|
method_parameters.each do |type, key|
|
52
51
|
if (key == target_model_name) && permitted_attributes
|
53
|
-
params
|
52
|
+
params.require(key) if %i[req keyreq].include?(type)
|
53
|
+
params[key] = params[key].try :permit, *permitted_attributes
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
data/lib/action_args/version.rb
CHANGED
@@ -15,7 +15,7 @@ class BooksControllerTest < ActionController::TestCase
|
|
15
15
|
end
|
16
16
|
|
17
17
|
test 'with page parameter' do
|
18
|
-
get :index, page: 3
|
18
|
+
get :index, params: {page: 3}
|
19
19
|
assert 200, response.code
|
20
20
|
assert_equal @books[20..29], assigns(:books)
|
21
21
|
end
|
@@ -23,21 +23,21 @@ class BooksControllerTest < ActionController::TestCase
|
|
23
23
|
test 'first param is nil and second is not nil' do
|
24
24
|
rhg = Book.create! title: 'RHG'
|
25
25
|
Book.create! title: 'AWDwR'
|
26
|
-
get :index, q: 'RH'
|
26
|
+
get :index, params: {q: 'RH'}
|
27
27
|
assert_equal [rhg], assigns(:books)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
31
|
test 'GET show' do
|
32
32
|
rhg = Book.create! title: 'RHG'
|
33
|
-
get :show, :id
|
33
|
+
get :show, params: {id: rhg.id}
|
34
34
|
assert_equal rhg, assigns(:book)
|
35
35
|
end
|
36
36
|
|
37
37
|
test 'POST create' do
|
38
38
|
Book.create! title: 'RHG'
|
39
39
|
books_count_was = Book.count
|
40
|
-
post :create, :book
|
40
|
+
post :create, params: {book: {title: 'AWDwR', price: 24}}
|
41
41
|
assert_equal 1, Book.count - books_count_was
|
42
42
|
end
|
43
43
|
end
|
@@ -7,12 +7,12 @@ class KwBooksControllerTest < ActionController::TestCase
|
|
7
7
|
end
|
8
8
|
|
9
9
|
test 'without giving any optional kw parameter' do
|
10
|
-
get :index, author_name: 'nari'
|
10
|
+
get :index, params: {author_name: 'nari'}
|
11
11
|
assert 200, response.code
|
12
12
|
end
|
13
13
|
|
14
14
|
test 'with kw parameter defaults to non-nil value' do
|
15
|
-
get :index, author_name: 'nari', page: 3
|
15
|
+
get :index, params: {author_name: 'nari', page: 3}
|
16
16
|
body = eval response.body
|
17
17
|
assert_equal 'nari', body[:author_name]
|
18
18
|
assert_equal '3', body[:page]
|
@@ -20,7 +20,7 @@ class KwBooksControllerTest < ActionController::TestCase
|
|
20
20
|
end
|
21
21
|
|
22
22
|
test 'with kw parameter defaults to nil' do
|
23
|
-
get :index, author_name: 'nari', q: 'Rails'
|
23
|
+
get :index, params: {author_name: 'nari', q: 'Rails'}
|
24
24
|
body = eval response.body
|
25
25
|
assert_equal 'nari', body[:author_name]
|
26
26
|
assert_equal '1', body[:page]
|
@@ -7,12 +7,12 @@ class KwKeyreqBooksControllerTest < ActionController::TestCase
|
|
7
7
|
end
|
8
8
|
|
9
9
|
test 'without giving any kw parameter' do
|
10
|
-
get :index, author_name: 'nari'
|
10
|
+
get :index, params: {author_name: 'nari'}
|
11
11
|
assert 200, response.code
|
12
12
|
end
|
13
13
|
|
14
14
|
test 'with kw parameter defaults to non-nil value' do
|
15
|
-
get :index, author_name: 'nari', page: 3
|
15
|
+
get :index, params: {author_name: 'nari', page: 3}
|
16
16
|
body = eval response.body
|
17
17
|
assert_equal 'nari', body[:author_name]
|
18
18
|
assert_equal '3', body[:page]
|
@@ -20,7 +20,7 @@ class KwKeyreqBooksControllerTest < ActionController::TestCase
|
|
20
20
|
end
|
21
21
|
|
22
22
|
test 'with kw parameter defaults to nil' do
|
23
|
-
get :index, author_name: 'nari', q: 'Rails'
|
23
|
+
get :index, params: {author_name: 'nari', q: 'Rails'}
|
24
24
|
body = eval response.body
|
25
25
|
assert_equal 'nari', body[:author_name]
|
26
26
|
assert_equal '1', body[:page]
|
@@ -3,14 +3,26 @@ require 'test_helper'
|
|
3
3
|
class StoresControllerTest < ActionController::TestCase
|
4
4
|
test 'GET show' do
|
5
5
|
tatsu_zine = Store.create! name: 'Tatsu-zine'
|
6
|
-
get :show, :id
|
6
|
+
get :show, params: {id: tatsu_zine.id}
|
7
7
|
|
8
8
|
assert_equal tatsu_zine, assigns(:store)
|
9
9
|
end
|
10
10
|
|
11
|
+
sub_test_case 'GET new' do
|
12
|
+
test 'without store parameter' do
|
13
|
+
get :new
|
14
|
+
assert 200, response.code
|
15
|
+
end
|
16
|
+
test 'with store parameter' do
|
17
|
+
get :new, params: {store: {name: 'Tatsu-zine'}}
|
18
|
+
assert 200, response.code
|
19
|
+
assert_equal 'Tatsu-zine', assigns(:store).name
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
11
23
|
test 'POST create' do
|
12
24
|
store_count_was = Store.count
|
13
|
-
post :create, :store
|
25
|
+
post :create, params: {store: {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'}}
|
14
26
|
|
15
27
|
assert_equal 1, Store.count - store_count_was
|
16
28
|
end
|
@@ -19,7 +31,7 @@ end
|
|
19
31
|
# this controller doesn't permit price of new book do
|
20
32
|
class Admin::BooksControllerTest < ActionController::TestCase
|
21
33
|
test 'POST create' do
|
22
|
-
post :create, :book
|
34
|
+
post :create, params: {book: {title: 'naruhoUnix', price: 30}}
|
23
35
|
|
24
36
|
assert_nil Book.last.price
|
25
37
|
end
|
@@ -28,7 +40,7 @@ end
|
|
28
40
|
class Admin::AccountsControllerTest < ActionController::TestCase
|
29
41
|
test 'POST create' do
|
30
42
|
admin_account_count_was = Admin::Account.count
|
31
|
-
post :create, :admin_account
|
43
|
+
post :create, params: {admin_account: {name: 'amatsuda'}}
|
32
44
|
|
33
45
|
assert_equal 1, Admin::Account.count - admin_account_count_was
|
34
46
|
end
|
data/test/fake_app.rb
CHANGED
@@ -5,7 +5,7 @@ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':me
|
|
5
5
|
|
6
6
|
module ActionArgsTestApp
|
7
7
|
class Application < Rails::Application
|
8
|
-
config.secret_token = [*'A'..'z'].join
|
8
|
+
config.secret_key_base = config.secret_token = [*'A'..'z'].join
|
9
9
|
config.session_store :cookie_store, :key => '_myapp_session'
|
10
10
|
config.active_support.deprecation = :log
|
11
11
|
config.eager_load = false
|
@@ -62,7 +62,7 @@ end
|
|
62
62
|
class AuthorsController < ApplicationController
|
63
63
|
def show
|
64
64
|
@author = Author.find params[:id]
|
65
|
-
render
|
65
|
+
render plain: @author.name
|
66
66
|
end
|
67
67
|
end
|
68
68
|
class BooksController < ApplicationController
|
@@ -77,17 +77,17 @@ class BooksController < ApplicationController
|
|
77
77
|
def index(page = 1, q = nil, limit = 10)
|
78
78
|
@books = Book.limit(limit.to_i).offset(([page.to_i - 1, 0].max) * 10)
|
79
79
|
@books = @books.where('title like ?', "%#{q}%") unless q.blank?
|
80
|
-
render
|
80
|
+
render plain: 'index', books: @books
|
81
81
|
end
|
82
82
|
|
83
83
|
def show(id)
|
84
|
-
render
|
84
|
+
render plain: @book.title
|
85
85
|
end
|
86
86
|
|
87
87
|
def create(book)
|
88
88
|
book = book.permit :title, :price
|
89
89
|
@book = Book.create! book
|
90
|
-
render
|
90
|
+
render plain: @book.title
|
91
91
|
end
|
92
92
|
|
93
93
|
private
|
@@ -110,12 +110,17 @@ class StoresController < ApplicationController
|
|
110
110
|
|
111
111
|
def show(id)
|
112
112
|
@store = Store.find(id)
|
113
|
-
render
|
113
|
+
render plain: @store.name
|
114
|
+
end
|
115
|
+
|
116
|
+
def new(store = nil)
|
117
|
+
@store = Store.new store
|
118
|
+
render plain: @store.name
|
114
119
|
end
|
115
120
|
|
116
121
|
def create(store)
|
117
122
|
@store = Store.create! store
|
118
|
-
render
|
123
|
+
render plain: @store.name
|
119
124
|
end
|
120
125
|
end
|
121
126
|
module Admin
|
@@ -124,7 +129,7 @@ module Admin
|
|
124
129
|
|
125
130
|
def create(admin_account)
|
126
131
|
@admin_account = Admin::Account.create! admin_account
|
127
|
-
render
|
132
|
+
render plain: @admin_account.name
|
128
133
|
end
|
129
134
|
end
|
130
135
|
|
@@ -133,7 +138,7 @@ module Admin
|
|
133
138
|
|
134
139
|
def create(book)
|
135
140
|
@book = Book.create! book
|
136
|
-
render
|
141
|
+
render plain: @book.title
|
137
142
|
end
|
138
143
|
end
|
139
144
|
end
|
@@ -151,4 +156,8 @@ class CreateAllTables < ActiveRecord::Migration
|
|
151
156
|
end
|
152
157
|
end
|
153
158
|
|
154
|
-
|
159
|
+
if ActiveRecord::Base.connection.respond_to? :data_source_exists?
|
160
|
+
CreateAllTables.up unless ActiveRecord::Base.connection.data_source_exists? 'authors'
|
161
|
+
else
|
162
|
+
CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'
|
163
|
+
end
|
data/test/kwargs_controllers.rb
CHANGED
@@ -3,6 +3,6 @@
|
|
3
3
|
class KwKeyreqBooksController < ApplicationController
|
4
4
|
# keyword arguments
|
5
5
|
def index(author_name:, page: '1', q: nil)
|
6
|
-
render
|
6
|
+
render plain: {author_name: author_name, page: page, q: q}.inspect
|
7
7
|
end
|
8
8
|
end
|
data/test/test_helper.rb
CHANGED
@@ -8,3 +8,17 @@ require 'action_args'
|
|
8
8
|
require 'fake_app'
|
9
9
|
require 'test/unit/rails/test_help'
|
10
10
|
Bundler.require
|
11
|
+
|
12
|
+
if Rails.version < '5'
|
13
|
+
module ActionControllerTestingMonkey
|
14
|
+
def get(path, params: nil, session: nil)
|
15
|
+
super path, params, session
|
16
|
+
end
|
17
|
+
|
18
|
+
def post(path, params: nil, session: nil)
|
19
|
+
super path, params, session
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
ActionController::TestCase.send :prepend, ActionControllerTestingMonkey
|
24
|
+
end
|
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: 2.
|
4
|
+
version: 2.1.0
|
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: 2016-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- action_args.gemspec
|
70
70
|
- gemfiles/rails_41.gemfile
|
71
71
|
- gemfiles/rails_42.gemfile
|
72
|
+
- gemfiles/rails_50.gemfile
|
72
73
|
- gemfiles/rails_edge.gemfile
|
73
74
|
- lib/action_args.rb
|
74
75
|
- lib/action_args/abstract_controller.rb
|
@@ -107,8 +108,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
107
108
|
- !ruby/object:Gem::Version
|
108
109
|
version: '0'
|
109
110
|
requirements: []
|
110
|
-
rubyforge_project:
|
111
|
-
rubygems_version: 2.4
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 2.6.4
|
112
113
|
signing_key:
|
113
114
|
specification_version: 4
|
114
115
|
summary: Controller action arguments parameterizer for Rails 4+ & Ruby 2.0+
|