action_args 1.5.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +12 -8
  3. data/CONTRIBUTING.md +5 -4
  4. data/README.md +6 -4
  5. data/Rakefile +12 -10
  6. data/action_args.gemspec +2 -2
  7. data/gemfiles/rails_edge.gemfile +19 -0
  8. data/lib/action_args.rb +2 -14
  9. data/lib/action_args/abstract_controller.rb +25 -36
  10. data/lib/action_args/callbacks.rb +3 -45
  11. data/lib/action_args/params_handler.rb +46 -40
  12. data/lib/action_args/version.rb +1 -1
  13. data/lib/generators/rails/action_args_scaffold_controller_generator.rb +0 -3
  14. data/test/controllers/action_args_controller_test.rb +43 -0
  15. data/test/controllers/hooks_test.rb +27 -0
  16. data/test/controllers/kwargs_controller_test.rb +30 -0
  17. data/test/controllers/kwargs_keyreq_controller_test.rb +30 -0
  18. data/test/controllers/ordinal_controller_test.rb +9 -0
  19. data/test/controllers/strong_parameters_test.rb +35 -0
  20. data/{spec → test}/fake_app.rb +33 -42
  21. data/{spec → test}/kwargs_controllers.rb +0 -0
  22. data/{spec → test}/kwargs_keyreq_controllers.rb +0 -0
  23. data/test/mailers/action_mailer_test.rb +8 -0
  24. data/test/params_handler/params_handler_test.rb +192 -0
  25. data/{spec/spec_helper.rb → test/test_helper.rb} +2 -7
  26. metadata +34 -45
  27. data/.rspec +0 -2
  28. data/gemfiles/rails_32.gemfile +0 -14
  29. data/gemfiles/rails_40.gemfile +0 -15
  30. data/lib/action_args/legacy/callbacks.rb +0 -71
  31. data/lib/generators/action_args/rspec/scaffold/scaffold_generator.rb +0 -18
  32. data/lib/generators/action_args/rspec/scaffold/templates/action_args_controller_spec.rb +0 -130
  33. data/spec/controllers/action_args_controller_spec.rb +0 -42
  34. data/spec/controllers/hooks_spec.rb +0 -31
  35. data/spec/controllers/kwargs_controller_spec.rb +0 -34
  36. data/spec/controllers/kwargs_keyreq_controller_spec.rb +0 -34
  37. data/spec/controllers/ordinal_controller_spec.rb +0 -11
  38. data/spec/controllers/strong_parameters_spec.rb +0 -34
  39. data/spec/mailers/action_mailer_spec.rb +0 -9
  40. data/spec/params_handler/params_handler_spec.rb +0 -218
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class BooksControllerTest < ActionController::TestCase
4
+ sub_test_case 'GET index (having an optional parameter)' do
5
+ setup do
6
+ @books = []
7
+ Book.delete_all
8
+ 100.times {|i| @books << Book.create!(title: 'book'+i.to_s) }
9
+ end
10
+
11
+ test 'without page parameter' do
12
+ get :index
13
+ assert 200, response.code
14
+ assert_equal @books[0..9], assigns(:books)
15
+ end
16
+
17
+ test 'with page parameter' do
18
+ get :index, page: 3
19
+ assert 200, response.code
20
+ assert_equal @books[20..29], assigns(:books)
21
+ end
22
+
23
+ test 'first param is nil and second is not nil' do
24
+ rhg = Book.create! title: 'RHG'
25
+ Book.create! title: 'AWDwR'
26
+ get :index, q: 'RH'
27
+ assert_equal [rhg], assigns(:books)
28
+ end
29
+ end
30
+
31
+ test 'GET show' do
32
+ rhg = Book.create! title: 'RHG'
33
+ get :show, :id => rhg.id
34
+ assert_equal rhg, assigns(:book)
35
+ end
36
+
37
+ test 'POST create' do
38
+ Book.create! title: 'RHG'
39
+ books_count_was = Book.count
40
+ post :create, :book => {title: 'AWDwR', price: 24}
41
+ assert_equal 1, Book.count - books_count_was
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ class BooksControllerTest < ActionController::TestCase
4
+ setup do
5
+ Book.delete_all
6
+ @book = Book.create! title: 'Head First ActionArgs'
7
+ get :show, id: @book.id
8
+ end
9
+
10
+ sub_test_case 'before_action' do
11
+ test 'via Symbol' do
12
+ assert_equal @book, assigns(:book)
13
+ end
14
+
15
+ test 'via String' do
16
+ assert assigns(:string_filter_executed)
17
+ end
18
+
19
+ test 'via Proc' do
20
+ assert assigns(:proc_filter_executed)
21
+ end
22
+ end
23
+
24
+ test 'around_action' do
25
+ assert_not_nil assigns(:elapsed_time)
26
+ end
27
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class KwBooksControllerTest < ActionController::TestCase
4
+ sub_test_case 'GET index (having an optional parameter)' do
5
+ test 'without giving any kw parameter (not even giving :required one)' do
6
+ assert_raises(ActionController::BadRequest) { get :index }
7
+ end
8
+
9
+ test 'without giving any optional kw parameter' do
10
+ get :index, author_name: 'nari'
11
+ assert 200, response.code
12
+ end
13
+
14
+ test 'with kw parameter defaults to non-nil value' do
15
+ get :index, author_name: 'nari', page: 3
16
+ body = eval response.body
17
+ assert_equal 'nari', body[:author_name]
18
+ assert_equal '3', body[:page]
19
+ assert_nil body[:q]
20
+ end
21
+
22
+ test 'with kw parameter defaults to nil' do
23
+ get :index, author_name: 'nari', q: 'Rails'
24
+ body = eval response.body
25
+ assert_equal 'nari', body[:author_name]
26
+ assert_equal '1', body[:page]
27
+ assert_equal 'Rails', body[:q]
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class KwKeyreqBooksControllerTest < ActionController::TestCase
4
+ sub_test_case 'GET index (having an optional parameter)' do
5
+ test 'without giving any kw parameter (not even giving :required one)' do
6
+ assert_raises(ActionController::BadRequest) { get :index }
7
+ end
8
+
9
+ test 'without giving any kw parameter' do
10
+ get :index, author_name: 'nari'
11
+ assert 200, response.code
12
+ end
13
+
14
+ test 'with kw parameter defaults to non-nil value' do
15
+ get :index, author_name: 'nari', page: 3
16
+ body = eval response.body
17
+ assert_equal 'nari', body[:author_name]
18
+ assert_equal '3', body[:page]
19
+ assert_nil body[:q]
20
+ end
21
+
22
+ test 'with kw parameter defaults to nil' do
23
+ get :index, author_name: 'nari', q: 'Rails'
24
+ body = eval response.body
25
+ assert_equal 'nari', body[:author_name]
26
+ assert_equal '1', body[:page]
27
+ assert_equal 'Rails', body[:q]
28
+ end
29
+ end
30
+ end if RUBY_VERSION >= '2.1'
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class AuthorsControllerTest < ActionController::TestCase
4
+ test 'GET show' do
5
+ matz = Author.create! name: 'Matz'
6
+ get :show, :id => matz.id
7
+ assert_equal matz, assigns(:author)
8
+ end
9
+ end
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+
3
+ class StoresControllerTest < ActionController::TestCase
4
+ test 'GET show' do
5
+ tatsu_zine = Store.create! name: 'Tatsu-zine'
6
+ get :show, :id => tatsu_zine.id
7
+
8
+ assert_equal tatsu_zine, assigns(:store)
9
+ end
10
+
11
+ test 'POST create' do
12
+ store_count_was = Store.count
13
+ post :create, :store => {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'}
14
+
15
+ assert_equal 1, Store.count - store_count_was
16
+ end
17
+ end
18
+
19
+ # this controller doesn't permit price of new book do
20
+ class Admin::BooksControllerTest < ActionController::TestCase
21
+ test 'POST create' do
22
+ post :create, :book => {title: 'naruhoUnix', price: 30}
23
+
24
+ assert_nil Book.last.price
25
+ end
26
+ end
27
+
28
+ class Admin::AccountsControllerTest < ActionController::TestCase
29
+ test 'POST create' do
30
+ admin_account_count_was = Admin::Account.count
31
+ post :create, :admin_account => {name: 'amatsuda'}
32
+
33
+ assert_equal 1, Admin::Account.count - admin_account_count_was
34
+ end
35
+ end
@@ -66,21 +66,12 @@ class AuthorsController < ApplicationController
66
66
  end
67
67
  end
68
68
  class BooksController < ApplicationController
69
- if Rails::VERSION::MAJOR >= 4
70
- before_action :set_book, only: :show
71
- before_action -> { @proc_filter_executed = true }, only: :show
72
- before_action '@string_filter_executed = true', only: :show
73
- around_action :benchmark_action
74
- before_action :omg
75
- skip_before_action :omg
76
- else
77
- before_filter :set_book, only: :show
78
- before_filter -> { @proc_filter_executed = true }, only: :show
79
- before_filter '@string_filter_executed = true', only: :show
80
- around_filter :benchmark_action
81
- before_filter :omg
82
- skip_before_filter :omg
83
- end
69
+ before_action :set_book, only: :show
70
+ before_action -> { @proc_filter_executed = true }, only: :show
71
+ before_action '@string_filter_executed = true', only: :show
72
+ around_action :benchmark_action
73
+ before_action :omg
74
+ skip_before_action :omg
84
75
 
85
76
  # optional parameter
86
77
  def index(page = 1, q = nil, limit = 10)
@@ -94,7 +85,7 @@ class BooksController < ApplicationController
94
85
  end
95
86
 
96
87
  def create(book)
97
- book = book.permit :title, :price if Rails::VERSION::MAJOR >= 4
88
+ book = book.permit :title, :price
98
89
  @book = Book.create! book
99
90
  render text: @book.title
100
91
  end
@@ -114,42 +105,40 @@ class BooksController < ApplicationController
114
105
  raise '💣'
115
106
  end
116
107
  end
117
- if Rails::VERSION::MAJOR >= 4
118
- class StoresController < ApplicationController
119
- permits :name, :url
108
+ class StoresController < ApplicationController
109
+ permits :name, :url
120
110
 
121
- def show(id)
122
- @store = Store.find(id)
123
- render text: @store.name
124
- end
111
+ def show(id)
112
+ @store = Store.find(id)
113
+ render text: @store.name
114
+ end
125
115
 
126
- def create(store)
127
- @store = Store.create! store
128
- render text: @store.name
129
- end
116
+ def create(store)
117
+ @store = Store.create! store
118
+ render text: @store.name
130
119
  end
131
- module Admin
132
- class AccountsController < ::ApplicationController
133
- permits :name, model_name: 'Admin::Account'
134
-
135
- def create(admin_account)
136
- @admin_account = Admin::Account.create! admin_account
137
- render text: @admin_account.name
138
- end
120
+ end
121
+ module Admin
122
+ class AccountsController < ::ApplicationController
123
+ permits :name, model_name: 'Admin::Account'
124
+
125
+ def create(admin_account)
126
+ @admin_account = Admin::Account.create! admin_account
127
+ render text: @admin_account.name
139
128
  end
129
+ end
140
130
 
141
- class BooksController < ::ApplicationController
142
- permits :title
131
+ class BooksController < ::ApplicationController
132
+ permits :title
143
133
 
144
- def create(book)
145
- @book = Book.create! book
146
- render text: @book.title
147
- end
134
+ def create(book)
135
+ @book = Book.create! book
136
+ render text: @book.title
148
137
  end
149
138
  end
150
139
  end
151
140
 
152
- require_relative 'kwargs_controllers' if RUBY_VERSION >= '2'
141
+ require_relative 'kwargs_controllers'
153
142
  require_relative 'kwargs_keyreq_controllers' if RUBY_VERSION >= '2.1'
154
143
 
155
144
  # migrations
@@ -161,3 +150,5 @@ class CreateAllTables < ActiveRecord::Migration
161
150
  create_table(:admin_accounts) {|t| t.string :name}
162
151
  end
163
152
  end
153
+
154
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'authors'
File without changes
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class UserMailerTest < ActionMailer::TestCase
4
+ test '#send_email_without_args' do
5
+ #it should not raise NameError: undefined local variable or method `params' for ...
6
+ assert UserMailer.send_email_without_args
7
+ end
8
+ end
@@ -0,0 +1,192 @@
1
+ require 'test_helper'
2
+ using ActionArgs::ParamsHandler
3
+
4
+ class ActionArgs::ParamsHandlerTest < ActiveSupport::TestCase
5
+ sub_test_case 'extract_method_arguments_from_params' do
6
+ setup do
7
+ params = {a: '1', b: '2'}
8
+ @controller = Class.new(ApplicationController).new.tap {|c| c.params = params }
9
+ end
10
+ test 'no parameters' do
11
+ def @controller.m() end
12
+
13
+ assert_equal [], @controller.extract_method_arguments_from_params(:m)
14
+ end
15
+
16
+ test '1 req' do
17
+ def @controller.m(a) end
18
+
19
+ assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
20
+ end
21
+
22
+ test '2 reqs' do
23
+ def @controller.m(a, b) end
24
+
25
+ assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
26
+ end
27
+
28
+ test '1 opt with value' do
29
+ def @controller.m(a = 'a') end
30
+
31
+ assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
32
+ end
33
+
34
+ test '1 opt without value' do
35
+ def @controller.m(x = 'x') end
36
+
37
+ assert_equal [], @controller.extract_method_arguments_from_params(:m)
38
+ end
39
+
40
+ test 'req, opt with value' do
41
+ def @controller.m(a, b = 'b') end
42
+
43
+ assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
44
+ end
45
+
46
+ test 'req, opt without value' do
47
+ def @controller.m(a, x = 'x') end
48
+
49
+ assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
50
+ end
51
+
52
+ test 'opt with value, opt with value' do
53
+ def @controller.m(a = 'a', b = 'b') end
54
+
55
+ assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
56
+ end
57
+
58
+ test 'opt with value, opt without value' do
59
+ def @controller.m(a = 'a', x = 'x') end
60
+
61
+ assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
62
+ end
63
+
64
+ test 'opt without value, opt with value' do
65
+ def @controller.m(x = 'x', a = 'a') end
66
+
67
+ assert_equal [nil, '1'], @controller.extract_method_arguments_from_params(:m)
68
+ end
69
+
70
+ test 'opt without value, opt without value' do
71
+ def @controller.m(x = 'x', y = 'y') end
72
+
73
+ assert_equal [], @controller.extract_method_arguments_from_params(:m)
74
+ end
75
+
76
+ test 'opt with value, req' do
77
+ def @controller.m(a = 'a', b) end
78
+
79
+ assert_equal ['1', '2'], @controller.extract_method_arguments_from_params(:m)
80
+ end
81
+
82
+ test 'opt without value, req' do
83
+ def @controller.m(x = 'x', a) end
84
+
85
+ assert_equal ['1'], @controller.extract_method_arguments_from_params(:m)
86
+ end
87
+
88
+ test 'opt without value, opt with value, req' do
89
+ def @controller.m(x = 'x', b = 'b', a) end
90
+
91
+ assert_equal [nil, '2', '1'], @controller.extract_method_arguments_from_params(:m)
92
+ end
93
+
94
+ test 'opt with value, opt without value, req' do
95
+ def @controller.m(b = 'b', x = 'x', a) end
96
+
97
+ assert_equal ['2', '1'], @controller.extract_method_arguments_from_params(:m)
98
+ end
99
+
100
+ test 'req without a value' do
101
+ def @controller.m(x) end
102
+
103
+ assert_raises(ActionController::BadRequest) { @controller.extract_method_arguments_from_params(:m) }
104
+ end
105
+
106
+ test 'key' do
107
+ def @controller.m(a: nil) end
108
+
109
+ assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
110
+ end
111
+
112
+ test 'key, key without value' do
113
+ def @controller.m(a: nil, x: 'x') end
114
+
115
+ assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
116
+ end
117
+
118
+ if RUBY_VERSION >= '2.1'
119
+ eval <<-KWARGS_KEYREQ_TEST
120
+ test 'keyreq' do
121
+ def @controller.m(a:) end
122
+
123
+ assert_equal [a: '1'], @controller.extract_method_arguments_from_params(:m)
124
+ end
125
+
126
+ test 'keyreq, keyreq without value' do
127
+ def @controller.m(a:, x:) end
128
+
129
+ assert_raises(ActionController::BadRequest) { @controller.extract_method_arguments_from_params(:m) }
130
+ end
131
+ KWARGS_KEYREQ_TEST
132
+ end
133
+ end
134
+
135
+ sub_test_case 'strengthen_params!' do
136
+ setup do
137
+ @params = ActionController::Parameters.new(x: '1', y: '2', foo: {a: 'a', b: 'b'}, bar: {a: 'a', b: 'b'}, baz: {a: 'a', b: 'b'}, hoge: {a: 'a', b: 'b'}, fuga: {a: 'a', b: 'b'})
138
+ end
139
+
140
+ def execute_strengthen_params!(controller)
141
+ c = controller.new
142
+ c.instance_variable_set :@_params, @params
143
+ c.strengthen_params! :a
144
+ end
145
+
146
+ test 'requiring via :req, permitting all scalars' do
147
+ execute_strengthen_params! FooController ||= Class.new(ApplicationController) { permits :a, :b; def a(foo) end }
148
+
149
+ assert @params[:foo].permitted?
150
+ assert_not_nil @params[:foo][:a]
151
+ assert_not_nil @params[:foo][:b]
152
+ end
153
+
154
+ test 'requiring via :req, not permitting all scalars' do
155
+ execute_strengthen_params! BarController ||= Class.new(ApplicationController) { permits :a; def a(bar, x = 'x') end }
156
+
157
+ assert @params[:bar].permitted?
158
+ assert_not_nil @params[:bar][:a]
159
+ assert_nil @params[:bar][:b]
160
+ end
161
+
162
+ test 'requiring via :req, not permitting any scalars' do
163
+ execute_strengthen_params! BazController ||= Class.new(ApplicationController) { def a(baz, aho = 'omg') end }
164
+
165
+ refute @params[:baz].permitted?
166
+ end
167
+
168
+ test 'requiring via :opt, permitting all scalars' do
169
+ execute_strengthen_params! HogeController ||= Class.new(ApplicationController) { permits :a, :b; def a(hoge = {}) end }
170
+
171
+ assert @params[:hoge].permitted?
172
+ assert_not_nil @params[:hoge][:a]
173
+ assert_not_nil @params[:hoge][:b]
174
+ end
175
+
176
+ test 'requiring via :key, permitting all scalars' do
177
+ execute_strengthen_params! FugaController ||= Class.new(ApplicationController) { permits :a, :b; def a(fuga: {}) end }
178
+
179
+ assert @params[:fuga].permitted?
180
+ assert_not_nil @params[:fuga][:a]
181
+ assert_not_nil @params[:fuga][:b]
182
+ end
183
+
184
+ test '"model_name" option' do
185
+ execute_strengthen_params! PiyoController ||= Class.new(ApplicationController) { permits :a, :b, model_name: 'Foo'; def a(foo) end }
186
+
187
+ assert @params[:foo].permitted?
188
+ assert_not_nil @params[:foo][:a]
189
+ assert_not_nil @params[:foo][:b]
190
+ end
191
+ end
192
+ end