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
@@ -1,31 +0,0 @@
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
@@ -1,34 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe KwBooksController do
4
- describe 'GET index (having an optional parameter)' do
5
- context 'without giving any kw parameter (not even giving :required one)' do
6
- if ActionPack::VERSION::MAJOR >= 4
7
- it { expect { get :index }.to raise_error ActionController::BadRequest }
8
- else
9
- it { expect { get :index }.to raise_error ArgumentError }
10
- end
11
- end
12
-
13
- context 'without giving any optional kw parameter' do
14
- before { get :index, author_name: 'nari' }
15
- its(:response) { should be_success }
16
- end
17
-
18
- context 'with kw parameter defaults to non-nil value' do
19
- before { get :index, author_name: 'nari', page: 3 }
20
- subject { eval response.body }
21
- its([:author_name]) { should == 'nari' }
22
- its([:page]) { should == '3' }
23
- its([:q]) { should == nil }
24
- end
25
-
26
- context 'with kw parameter defaults to nil' do
27
- before { get :index, author_name: 'nari', q: 'Rails' }
28
- subject { eval response.body }
29
- its([:author_name]) { should == 'nari' }
30
- its([:page]) { should == '1' }
31
- its([:q]) { should == 'Rails' }
32
- end
33
- end
34
- end if RUBY_VERSION >= '2'
@@ -1,34 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe KwKeyreqBooksController do
4
- describe 'GET index (having an optional parameter)' do
5
- context 'without giving any kw parameter (not even giving :required one)' do
6
- if ActionPack::VERSION::MAJOR >= 4
7
- it { expect { get :index }.to raise_error ActionController::BadRequest }
8
- else
9
- it { expect { get :index }.to raise_error ArgumentError }
10
- end
11
- end
12
-
13
- context 'without giving any kw parameter' do
14
- before { get :index, author_name: 'nari' }
15
- its(:response) { should be_success }
16
- end
17
-
18
- context 'with kw parameter defaults to non-nil value' do
19
- before { get :index, author_name: 'nari', page: 3 }
20
- subject { eval response.body }
21
- its([:author_name]) { should == 'nari' }
22
- its([:page]) { should == '3' }
23
- its([:q]) { should == nil }
24
- end
25
-
26
- context 'with kw parameter defaults to nil' do
27
- before { get :index, author_name: 'nari', q: 'Rails' }
28
- subject { eval response.body }
29
- its([:author_name]) { should == 'nari' }
30
- its([:page]) { should == '1' }
31
- its([:q]) { should == 'Rails' }
32
- end
33
- end
34
- end if RUBY_VERSION >= '2.1'
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe AuthorsController do
4
- describe 'GET show' do
5
- let(:matz) { Author.create! name: 'Matz' }
6
- it 'assigns the requested author as @author' do
7
- get :show, :id => matz.id
8
- assigns(:author).should eq(matz)
9
- end
10
- end
11
- end
@@ -1,34 +0,0 @@
1
- require 'spec_helper'
2
-
3
- if Rails::VERSION::MAJOR >= 4
4
- describe StoresController do
5
- describe 'GET show' do
6
- let(:tatsu_zine) { Store.create! name: 'Tatsu-zine' }
7
- before { get :show, :id => tatsu_zine.id }
8
- subject { assigns :store }
9
- it { should == tatsu_zine }
10
- end
11
-
12
- describe 'POST create' do
13
- it { expect { post :create, :store => {name: 'Tatsu-zine', url: 'http://tatsu-zine.com'} }.to change(Store, :count).by(1) }
14
- end
15
- end
16
-
17
- describe Admin::BooksController do
18
- context "this controller doesn't permit price of new book" do
19
- describe 'POST create' do
20
- before { post :create, :book => {title: 'naruhoUnix', price: 30} }
21
-
22
- it 'should not save price of the book' do
23
- expect(Book.last.price).to be_nil
24
- end
25
- end
26
- end
27
- end
28
-
29
- describe Admin::AccountsController do
30
- describe 'POST create' do
31
- it { expect { post :create, :admin_account => {name: 'amatsuda'} }.to change(Admin::Account, :count).by(1) }
32
- end
33
- end
34
- end
@@ -1,9 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe UserMailer do
4
- describe '#send_email_without_args' do
5
- it "should not raise NameError: undefined local variable or method `params' for ..." do
6
- expect{ UserMailer.send_email_without_args }.to_not raise_error
7
- end
8
- end
9
- end
@@ -1,218 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe ActionArgs::ParamsHandler do
4
- # ActionArgs::ParamsHandler.extract_method_arguments_from_params(method_parameters, params)
5
- describe 'extract_method_arguments_from_params' do
6
- let(:params) { {a: '1', b: '2'} }
7
- subject { ActionArgs::ParamsHandler.extract_method_arguments_from_params method(:m).parameters, params }
8
- context 'no parameters' do
9
- before do
10
- def m() end
11
- end
12
- it { should == [] }
13
- end
14
-
15
- context '1 req' do
16
- before do
17
- def m(a) end
18
- end
19
- it { should == ['1'] }
20
- end
21
-
22
- context '2 reqs' do
23
- before do
24
- def m(a, b) end
25
- end
26
- it { should == ['1', '2'] }
27
- end
28
-
29
- context '1 opt with value' do
30
- before do
31
- def m(a = 'a') end
32
- end
33
- it { should == ['1'] }
34
- end
35
-
36
- context '1 opt without value' do
37
- before do
38
- def m(x = 'x') end
39
- end
40
- it { should == [] }
41
- end
42
-
43
- context 'req, opt with value' do
44
- before do
45
- def m(a, b = 'b') end
46
- end
47
- it { should == ['1', '2'] }
48
- end
49
-
50
- context 'req, opt without value' do
51
- before do
52
- def m(a, x = 'x') end
53
- end
54
- it { should == ['1'] }
55
- end
56
-
57
- context 'opt with value, opt with value' do
58
- before do
59
- def m(a = 'a', b = 'b') end
60
- end
61
- it { should == ['1', '2'] }
62
- end
63
-
64
- context 'opt with value, opt without value' do
65
- before do
66
- def m(a = 'a', x = 'x') end
67
- end
68
- it { should == ['1'] }
69
- end
70
-
71
- context 'opt without value, opt with value' do
72
- before do
73
- def m(x = 'x', a = 'a') end
74
- end
75
- it { should == [nil, '1'] }
76
- end
77
-
78
- context 'opt without value, opt without value' do
79
- before do
80
- def m(x = 'x', y = 'y') end
81
- end
82
- it { should == [] }
83
- end
84
-
85
- context 'opt with value, req' do
86
- before do
87
- def m(a = 'a', b) end
88
- end
89
- it { should == ['1', '2'] }
90
- end
91
-
92
- context 'opt without value, req' do
93
- before do
94
- def m(x = 'x', a) end
95
- end
96
- it { should == ['1'] }
97
- end
98
-
99
- context 'opt without value, opt with value, req' do
100
- before do
101
- def m(x = 'x', b = 'b', a) end
102
- end
103
- it { should == [nil, '2', '1'] }
104
- end
105
-
106
- context 'opt with value, opt without value, req' do
107
- before do
108
- def m(b = 'b', x = 'x', a) end
109
- end
110
- it { should == ['2', '1'] }
111
- end
112
-
113
- if Rails::VERSION::MAJOR >= 4
114
- context 'req without a value' do
115
- before do
116
- def m(x) end
117
- end
118
- it { expect { subject }.to raise_error ActionController::BadRequest }
119
- end
120
- end
121
-
122
- if RUBY_VERSION >= '2'
123
- eval <<-KWARGS_TEST
124
- context 'key' do
125
- before do
126
- def m(a: nil) end
127
- end
128
- it { should == [a: '1'] }
129
- end
130
-
131
- context 'key, key without value' do
132
- before do
133
- def m(a: nil, x: 'x') end
134
- end
135
- it { should == [a: '1'] }
136
- end
137
- KWARGS_TEST
138
- end
139
-
140
- if RUBY_VERSION >= '2.1'
141
- eval <<-KWARGS_KEYREQ_TEST
142
- context 'keyreq' do
143
- before do
144
- def m(a:) end
145
- end
146
- it { should == [a: '1'] }
147
- end
148
-
149
- if Rails::VERSION::MAJOR >= 4
150
- context 'keyreq, keyreq without value' do
151
- before do
152
- def m(a:, x:) end
153
- end
154
- it { expect { subject }.to raise_error ::ActionController::BadRequest }
155
- end
156
- end
157
- KWARGS_KEYREQ_TEST
158
- end
159
- end
160
-
161
- if defined? ActionController::StrongParameters
162
- # strengthen_params!(controller_class, method_parameters, params)
163
- describe 'strengthen_params!' do
164
- before { ActionArgs::ParamsHandler.strengthen_params! controller, controller.new.method(:a).parameters, params }
165
- let(: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'}) }
166
-
167
- context 'requiring via :req, permitting all scalars' do
168
- let(:controller) { FooController ||= Class.new(ApplicationController) { permits :a, :b; def a(foo) end } }
169
- subject { params[:foo] }
170
- it { should be_permitted }
171
- its([:a]) { should be }
172
- its([:b]) { should be }
173
- end
174
-
175
- context 'requiring via :req, not permitting all scalars' do
176
- let(:controller) { BarController ||= Class.new(ApplicationController) { permits :a; def a(bar, x = 'x') end } }
177
- subject { params[:bar] }
178
- it { should be_permitted }
179
- its([:a]) { should be }
180
- its([:b]) { should_not be }
181
- end
182
-
183
- context 'requiring via :req, not permitting any scalars' do
184
- let(:controller) { BazController ||= Class.new(ApplicationController) { def a(baz, aho = 'omg') end } }
185
- subject { params[:baz] }
186
- it { should_not be_permitted }
187
- end
188
-
189
- context 'requiring via :opt, permitting all scalars' do
190
- let(:controller) { HogeController ||= Class.new(ApplicationController) { permits :a, :b; def a(hoge = {}) end } }
191
- subject { params[:hoge] }
192
- it { should be_permitted }
193
- its([:a]) { should be }
194
- its([:b]) { should be }
195
- end
196
-
197
- if RUBY_VERSION >= '2'
198
- eval <<-KWARGS_TEST
199
- context 'requiring via :key, permitting all scalars' do
200
- let(:controller) { FugaController ||= Class.new(ApplicationController) { permits :a, :b; def a(fuga: {}) end } }
201
- subject { params[:fuga] }
202
- it { should be_permitted }
203
- its([:a]) { should be }
204
- its([:b]) { should be }
205
- end
206
- KWARGS_TEST
207
- end
208
-
209
- describe '"model_name" option' do
210
- let(:controller) { PiyoController ||= Class.new(ApplicationController) { permits :a, :b, model_name: 'Foo'; def a(foo) end } }
211
- subject { params[:foo] }
212
- it { should be_permitted }
213
- its([:a]) { should be }
214
- its([:b]) { should be }
215
- end
216
- end
217
- end
218
- end