rename_params 1.0.0 → 1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab818ecdac22088f8e4c0fc471f1b14493f3087e
4
- data.tar.gz: d316571c82a95e8697da6e30c2813ab23e049d21
3
+ metadata.gz: b90214f110951409c498a774e7a3e4b59fab5c36
4
+ data.tar.gz: a9eac70722c932f4d4c6d233340575ff2afe5108
5
5
  SHA512:
6
- metadata.gz: fe1cb6c108e84637a6dadba79c4059c49cc0d36c9de3b4229f1c70c6ae90e3e335cbcd4c39174429d2ab8049ead791316f970488c28c31487cf95382d8fb70b3
7
- data.tar.gz: ce58245c80d31fdeac5a6a84d64763ac8a113309c2c9dbec93173a6e18b3db5580648b73490b749df68a225f1651ffcc66a70f6dcbed3bb729bd316d02f17856
6
+ metadata.gz: 6dd0a4c6daf1cd74783714857ff26a7cbafbf84924896682373b6c6a848017223d6158cc584d6ea825465438ee0459a393d542d987d1e2db7a9c2ce2a1bb9eb8
7
+ data.tar.gz: 749be24287ffa76428ebab7a9f46175015d6b10b827b8af25a25a2142214a7f181ebd86d408c9dbca4c723bf97adfe47a66463da3718bed7e9ce4ffe1ba0f1b2
data/lib/rename_params.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'active_support'
2
2
  require 'active_support/core_ext'
3
3
  require 'action_controller'
4
+ require 'rename_params/macros/base'
5
+ require 'rename_params/macros/rename'
6
+ require 'rename_params/macros/move'
4
7
  require 'rename_params/macros'
5
8
  require 'rename_params/params'
6
9
  require 'rename_params/converters/hash_converter'
@@ -4,40 +4,15 @@ module RenameParams
4
4
 
5
5
  module ClassMethods
6
6
  def rename(*args)
7
- current_param = args.shift
8
- options = build_options(*args)
9
-
10
- before_filter options[:filters] do
11
- new_params = RenameParams::Params.new(params, self)
12
- new_params.convert(current_param, options[:convert], options[:namespace])
13
- new_params.rename(current_param, options[:to], options[:namespace])
14
- end
15
- end
16
-
17
- private
18
-
19
- def build_options(args = {})
20
- {
21
- to: args[:to],
22
- convert: args[:convert],
23
- namespace: namespace_options(args),
24
- filters: filter_options(args)
25
- }
26
- end
27
-
28
- def namespace_options(args = {})
29
- args[:namespace].is_a?(Array) ? args[:namespace] : [args[:namespace]].compact
7
+ RenameParams::Macros::Rename.def_rename(self, *args)
30
8
  end
31
9
 
32
- def filter_options(args = {})
33
- {
34
- only: args.delete(:only),
35
- except: args.delete(:except)
36
- }.reject { |_, v| v.nil? }
10
+ def move(*args)
11
+ RenameParams::Macros::Move.def_move(self, *args)
37
12
  end
38
13
  end
39
14
  end
40
15
  end
41
16
 
42
17
  ActionController::API.send(:include, RenameParams::Macros) if defined?(ActionController::API)
43
- ActionController::Base.send(:include, RenameParams::Macros) if defined?(ActionController::Base)
18
+ ActionController::Base.send(:include, RenameParams::Macros) if defined?(ActionController::Base)
@@ -0,0 +1,26 @@
1
+ module RenameParams
2
+ module Macros
3
+ class Base
4
+ class << self
5
+
6
+ def namespace_options(args = {})
7
+ args[:namespace] = [] if args[:namespace] == :root
8
+ args[:namespace].is_a?(Array) ? args[:namespace] : [args[:namespace]].compact
9
+ end
10
+
11
+ def move_to_options(key, args = {})
12
+ return unless args[key]
13
+ args[key] = [] if args[key] == :root
14
+ args[key].is_a?(Array) ? args[key] : [args[key]]
15
+ end
16
+
17
+ def filter_options(args = {})
18
+ {
19
+ only: args.delete(:only),
20
+ except: args.delete(:except)
21
+ }.reject { |_, v| v.nil? }
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ module RenameParams
2
+ module Macros
3
+ class Move < Base
4
+ class << self
5
+
6
+ def def_move(klass, *args)
7
+ move_param = args.shift
8
+ options = build_options(*args)
9
+
10
+ klass.before_filter(options[:filters]) do |controller|
11
+ params = RenameParams::Params.new(controller.params, controller)
12
+ params.move(move_param, options[:to], options[:namespace]) if options[:to]
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def build_options(args = {})
19
+ {
20
+ to: move_to_options(:to, args),
21
+ namespace: namespace_options(args),
22
+ filters: filter_options(args)
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,32 @@
1
+ module RenameParams
2
+ module Macros
3
+ class Rename < Base
4
+ class << self
5
+
6
+ def def_rename(klass, *args)
7
+ rename_param = args.shift
8
+ options = build_options(*args)
9
+
10
+ klass.before_filter options[:filters] do |controller|
11
+ params = RenameParams::Params.new(controller.params, controller)
12
+ params.convert(rename_param, options[:convert], options[:namespace])
13
+ params.rename(rename_param, options[:to], options[:namespace])
14
+ params.move(options[:to], options[:move_to], options[:namespace]) if options[:move_to]
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def build_options(args = {})
21
+ {
22
+ to: args[:to],
23
+ convert: args[:convert],
24
+ move_to: move_to_options(:move_to, args),
25
+ namespace: namespace_options(args),
26
+ filters: filter_options(args)
27
+ }
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -22,9 +22,14 @@ module RenameParams
22
22
  set(new_key, delete(key, namespace), namespace) if has_key?(key, namespace)
23
23
  end
24
24
 
25
+ def move(key, target = [], namespace = [])
26
+ set(key, delete(key, namespace), target)
27
+ end
28
+
25
29
  private
26
30
 
27
31
  def set(key, value, namespace = [])
32
+ create_namespace(namespace)
28
33
  namespaced(namespace)[key] = value
29
34
  end
30
35
 
@@ -47,6 +52,14 @@ module RenameParams
47
52
  params
48
53
  end
49
54
 
55
+ def create_namespace(namespace)
56
+ params = @params
57
+ namespace.each do |ns|
58
+ params[ns] ||= {}
59
+ params = params[ns]
60
+ end
61
+ end
62
+
50
63
  def converter_class(converter)
51
64
  if converter.is_a?(Hash)
52
65
  RenameParams::Converters::HashConverter.new(converter)
@@ -1,3 +1,3 @@
1
1
  module RenameParams
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -0,0 +1,77 @@
1
+ describe RenameParams::Macros::Move, type: :controller do
2
+ context 'with :root' do
3
+ controller(ActionController::Base) do
4
+ move :name, to: :root, namespace: :billing_contact
5
+
6
+ def update
7
+ head :ok
8
+ end
9
+ end
10
+
11
+ describe 'move' do
12
+ before { routes.draw { get 'update' => 'anonymous#update' } }
13
+
14
+ it 'moves billing_contact[:name] to root' do
15
+ put :update, { 'billing_contact' => { 'name' => 'Marcelo' } }
16
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => {}, 'name' => 'Marcelo' )
17
+ end
18
+ end
19
+ end
20
+
21
+ context 'with existent nested key' do
22
+ controller(ActionController::Base) do
23
+ move :street, to: :billing_contact, namespace: [:billing_contact, :address]
24
+
25
+ def update
26
+ head :ok
27
+ end
28
+ end
29
+
30
+ describe 'move' do
31
+ before { routes.draw { get 'update' => 'anonymous#update' } }
32
+
33
+ it 'moves billing_contact[:address][:street] to billing_contact[:street]' do
34
+ put :update, { 'billing_contact' => { 'address' => { 'street' => '123 St' } } }
35
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => { 'address' => {}, 'street' => '123 St' } )
36
+ end
37
+ end
38
+ end
39
+
40
+ context 'with non existent nested key' do
41
+ controller(ActionController::Base) do
42
+ move :name, to: :contact, namespace: :billing_contact
43
+
44
+ def update
45
+ head :ok
46
+ end
47
+ end
48
+
49
+ describe 'move' do
50
+ before { routes.draw { get 'update' => 'anonymous#update' } }
51
+
52
+ it 'moves billing_contact[:name] to contact[:name]' do
53
+ put :update, { 'billing_contact' => { 'name' => 'Marcelo' } }
54
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => {}, 'contact' => { 'name' =>'Marcelo' })
55
+ end
56
+ end
57
+ end
58
+
59
+ context 'with non existent deep nested key' do
60
+ controller(ActionController::Base) do
61
+ move :name, to: [:contact, :info], namespace: :billing_contact
62
+
63
+ def update
64
+ head :ok
65
+ end
66
+ end
67
+
68
+ describe 'move' do
69
+ before { routes.draw { get 'update' => 'anonymous#update' } }
70
+
71
+ it 'moves billing_contact[:name] to contact[:info][:name]' do
72
+ put :update, { 'billing_contact' => { 'name' => 'Marcelo' } }
73
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => {}, 'contact' => { 'info' => { 'name' =>'Marcelo' } })
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,299 @@
1
+ describe RenameParams::Macros::Rename, type: :controller do
2
+ context 'when not filtering' do
3
+ let(:default_params) { { 'controller' => 'anonymous', 'action' => 'index' } }
4
+ before { routes.draw { get 'index' => 'anonymous#index' } }
5
+
6
+ describe 'when just renaming params' do
7
+ controller(ActionController::Base) do
8
+ rename :username, to: :login
9
+
10
+ def index
11
+ head :ok
12
+ end
13
+ end
14
+
15
+ it 'renames username to login' do
16
+ get :index, { username: 'aperson' }
17
+ expect(controller.params).to eq default_params.merge('login' => 'aperson')
18
+ end
19
+
20
+ context 'if param is not sent' do
21
+ it 'leaves params as they were' do
22
+ get :index
23
+ expect(controller.params).to eq default_params
24
+ end
25
+ end
26
+ end
27
+
28
+ describe 'when converting values' do
29
+ context 'and using enum converter' do
30
+ controller(ActionController::Base) do
31
+ rename :admin, to: :role, convert: { true: ['admin'], false: [] }
32
+
33
+ def index
34
+ head :ok
35
+ end
36
+ end
37
+
38
+ it 'renames admin to role and converts value' do
39
+ get :index, { admin: 'true' }
40
+ expect(controller.params).to eq default_params.merge('role' => ['admin'])
41
+ end
42
+
43
+ context 'if param is not sent' do
44
+ it 'leaves params as they were' do
45
+ get :index
46
+ expect(controller.params).to eq default_params
47
+ end
48
+ end
49
+ end
50
+
51
+ context 'and using a Proc converter' do
52
+ controller(ActionController::Base) do
53
+ rename :amount_due, to: :amount_due_in_cents, convert: ->(value) { value.to_i * 100 }
54
+
55
+ def index
56
+ head :ok
57
+ end
58
+ end
59
+
60
+ it 'renames amount_due to amount_due_in_cents and converts value' do
61
+ get :index, { amount_due: 100 }
62
+ expect(controller.params).to eq default_params.merge('amount_due_in_cents' => 10000)
63
+ end
64
+
65
+ context 'if param is not sent' do
66
+ it 'leaves params as they were' do
67
+ get :index
68
+ expect(controller.params).to eq default_params
69
+ end
70
+ end
71
+ end
72
+
73
+ context 'and using a method converter' do
74
+ controller(ActionController::Base) do
75
+ rename :amount_due, to: :amount_due_in_cents, convert: :to_cents
76
+
77
+ def index
78
+ head :ok
79
+ end
80
+
81
+ private
82
+
83
+ def to_cents(value)
84
+ value.to_f * 100
85
+ end
86
+ end
87
+
88
+ it 'renames amount_due to amount_due_in_cents and converts value' do
89
+ get :index, { amount_due: 100 }
90
+ expect(controller.params).to eq default_params.merge('amount_due_in_cents' => 10000)
91
+ end
92
+
93
+ context 'if param is not sent' do
94
+ it 'leaves params as they were' do
95
+ get :index
96
+ expect(controller.params).to eq default_params
97
+ end
98
+ end
99
+ end
100
+ end
101
+
102
+ describe 'when nested params' do
103
+ context 'using only one namespace' do
104
+ controller(ActionController::Base) do
105
+ rename :username, to: :login, namespace: :session
106
+
107
+ def index
108
+ head :ok
109
+ end
110
+ end
111
+
112
+ it 'renames username to login' do
113
+ get :index, { 'session' => { 'username' => 'aperson' } }
114
+ expect(controller.params).to eq default_params.merge('session' => { 'login' => 'aperson' })
115
+ end
116
+
117
+ context 'if param is not sent' do
118
+ it 'leaves params as they were' do
119
+ get :index, { 'session' => '' }
120
+ expect(controller.params).to eq default_params.merge('session' => '')
121
+ end
122
+ end
123
+ end
124
+
125
+ context 'using more than one nest levels' do
126
+ controller(ActionController::Base) do
127
+ rename :username, to: :login, namespace: [:session, :credentials]
128
+
129
+ def index
130
+ head :ok
131
+ end
132
+ end
133
+
134
+ it 'renames username to login' do
135
+ get :index, { 'session' => { 'credentials' => { 'username' => 'aperson' } } }
136
+ expect(controller.params).to eq default_params.merge('session' => { 'credentials' => { 'login' => 'aperson' } })
137
+ end
138
+
139
+ context 'if param is not sent' do
140
+ it 'leaves params as they were' do
141
+ get :index, { 'session' => { 'credentials' => '' } }
142
+ expect(controller.params).to eq default_params.merge('session' => { 'credentials' => '' })
143
+ end
144
+ end
145
+
146
+ context 'if namespace is not sent' do
147
+ it 'leaves params as they were' do
148
+ get :index, { 'session' => '' }
149
+ expect(controller.params).to eq default_params.merge('session' => '')
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+
156
+ context 'when filtering' do
157
+ context 'using only' do
158
+ controller(ActionController::Base) do
159
+ rename :username, to: :login, only: :show
160
+
161
+ def index
162
+ head :ok
163
+ end
164
+
165
+ def show
166
+ head :ok
167
+ end
168
+ end
169
+
170
+ describe 'show' do
171
+ before { routes.draw { get 'show' => 'anonymous#show' } }
172
+
173
+ it 'renames username to login' do
174
+ get :show, { 'username' => 'aperson' }
175
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'show', 'login' => 'aperson')
176
+ end
177
+ end
178
+
179
+ describe 'index' do
180
+ before { routes.draw { get 'index' => 'anonymous#index' } }
181
+
182
+ it 'keeps username param' do
183
+ get :index, { 'username' => 'aperson' }
184
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'index', 'username' => 'aperson')
185
+ end
186
+ end
187
+ end
188
+
189
+ context 'using except' do
190
+ controller(ActionController::Base) do
191
+ rename :username, to: :login, except: :show
192
+
193
+ def index
194
+ head :ok
195
+ end
196
+
197
+ def show
198
+ head :ok
199
+ end
200
+ end
201
+
202
+ describe 'show' do
203
+ before { routes.draw { get 'show' => 'anonymous#show' } }
204
+
205
+ it 'keeps username param' do
206
+ get :show, { 'username' => 'aperson' }
207
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'show', 'username' => 'aperson')
208
+ end
209
+ end
210
+
211
+ describe 'index' do
212
+ before { routes.draw { get 'index' => 'anonymous#index' } }
213
+
214
+ it 'renames username to login' do
215
+ get :index, { 'username' => 'aperson' }
216
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'index', 'login' => 'aperson')
217
+ end
218
+ end
219
+ end
220
+ end
221
+
222
+ context 'when using move_to' do
223
+ context 'with :root' do
224
+ controller(ActionController::Base) do
225
+ rename :name, to: :billing_contact_name, namespace: :billing_contact, move_to: :root
226
+
227
+ def update
228
+ head :ok
229
+ end
230
+ end
231
+
232
+ describe 'move_to' do
233
+ before { routes.draw { get 'update' => 'anonymous#update' } }
234
+
235
+ it 'renames billing_contact[:name] to billing_contact_name' do
236
+ put :update, { 'billing_contact' => { 'name' => 'Marcelo' } }
237
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => {}, 'billing_contact_name' => 'Marcelo' )
238
+ end
239
+ end
240
+ end
241
+
242
+ context 'with existent nested key' do
243
+ controller(ActionController::Base) do
244
+ rename :street, to: :address_street, namespace: [:billing_contact, :address], move_to: :billing_contact
245
+
246
+ def update
247
+ head :ok
248
+ end
249
+ end
250
+
251
+ describe 'move_to' do
252
+ before { routes.draw { get 'update' => 'anonymous#update' } }
253
+
254
+ it 'renames billing_contact[:address][:street] to billing_contact[:street_address]' do
255
+ put :update, { 'billing_contact' => { 'address' => { 'street' => '123 St' } } }
256
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => { 'address' => {}, 'address_street' => '123 St' } )
257
+ end
258
+ end
259
+ end
260
+
261
+ context 'with non existent nested key' do
262
+ controller(ActionController::Base) do
263
+ rename :name, to: :name, namespace: :billing_contact, move_to: :contact
264
+
265
+ def update
266
+ head :ok
267
+ end
268
+ end
269
+
270
+ describe 'move_to' do
271
+ before { routes.draw { get 'update' => 'anonymous#update' } }
272
+
273
+ it 'renames billing_contact[:name] to contact[:name]' do
274
+ put :update, { 'billing_contact' => { 'name' => 'Marcelo' } }
275
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => {}, 'contact' => { 'name' =>'Marcelo' })
276
+ end
277
+ end
278
+ end
279
+
280
+ context 'with non existent deep nested key' do
281
+ controller(ActionController::Base) do
282
+ rename :name, to: :name, namespace: :billing_contact, move_to: [:contact, :info]
283
+
284
+ def update
285
+ head :ok
286
+ end
287
+ end
288
+
289
+ describe 'move_to' do
290
+ before { routes.draw { get 'update' => 'anonymous#update' } }
291
+
292
+ it 'renames billing_contact[:name] to contact[:info][:name]' do
293
+ put :update, { 'billing_contact' => { 'name' => 'Marcelo' } }
294
+ expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'update', 'billing_contact' => {}, 'contact' => { 'info' => { 'name' =>'Marcelo' } })
295
+ end
296
+ end
297
+ end
298
+ end
299
+ end
data/spec/params_spec.rb CHANGED
@@ -35,4 +35,22 @@ describe RenameParams::Params do
35
35
  it { expect(params[:b]).to eq 'BB' }
36
36
  end
37
37
  end
38
+
39
+ describe '#move' do
40
+ let(:params) { RenameParams::Params.new(a: 'A', b: 'B', c: { d: 'D' }) }
41
+
42
+ context 'when moving to nested key' do
43
+ before { params.move(:b, [:c]) }
44
+
45
+ it { expect(params[:b]).to be_nil }
46
+ it { expect(params[:c][:b]).to eq 'B' }
47
+ end
48
+
49
+ context 'when moving to root' do
50
+ before { params.move(:d, [], [:c]) }
51
+
52
+ it { expect(params[:d]).to eq 'D' }
53
+ it { expect(params[:c][:d]).to be_nil }
54
+ end
55
+ end
38
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rename_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcelo Casiraghi
@@ -16,28 +16,28 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '3.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: actionpack
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '3.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '3.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -133,11 +133,15 @@ files:
133
133
  - lib/rename_params/converters/method_converter.rb
134
134
  - lib/rename_params/converters/proc_converter.rb
135
135
  - lib/rename_params/macros.rb
136
+ - lib/rename_params/macros/base.rb
137
+ - lib/rename_params/macros/move.rb
138
+ - lib/rename_params/macros/rename.rb
136
139
  - lib/rename_params/params.rb
137
140
  - lib/rename_params/version.rb
138
141
  - spec/converters/hash_converter_spec.rb
139
142
  - spec/converters/proc_converter_spec.rb
140
- - spec/macros_spec.rb
143
+ - spec/macros/move_spec.rb
144
+ - spec/macros/rename_spec.rb
141
145
  - spec/params_spec.rb
142
146
  - spec/rails_helper.rb
143
147
  - spec/spec_helper.rb
@@ -171,7 +175,8 @@ summary: Simple params renaming for Rails applications
171
175
  test_files:
172
176
  - spec/converters/hash_converter_spec.rb
173
177
  - spec/converters/proc_converter_spec.rb
174
- - spec/macros_spec.rb
178
+ - spec/macros/move_spec.rb
179
+ - spec/macros/rename_spec.rb
175
180
  - spec/params_spec.rb
176
181
  - spec/rails_helper.rb
177
182
  - spec/spec_helper.rb
data/spec/macros_spec.rb DELETED
@@ -1,224 +0,0 @@
1
- describe RenameParams::Macros, type: :controller do
2
- describe '.rename' do
3
-
4
- context 'when not filtering' do
5
- let(:default_params) { { 'controller' => 'anonymous', 'action' => 'index' } }
6
- before { routes.draw { get 'index' => 'anonymous#index' } }
7
-
8
- describe 'when just renaming params' do
9
- controller(ActionController::Base) do
10
- rename :username, to: :login
11
-
12
- def index
13
- head :ok
14
- end
15
- end
16
-
17
- it 'renames username to login' do
18
- get :index, { username: 'aperson' }
19
- expect(controller.params).to eq default_params.merge('login' => 'aperson')
20
- end
21
-
22
- context 'if param is not sent' do
23
- it 'leaves params as they were' do
24
- get :index
25
- expect(controller.params).to eq default_params
26
- end
27
- end
28
- end
29
-
30
- describe 'when converting values' do
31
- context 'and using enum converter' do
32
- controller(ActionController::Base) do
33
- rename :admin, to: :role, convert: { true: ['admin'], false: [] }
34
-
35
- def index
36
- head :ok
37
- end
38
- end
39
-
40
- it 'renames admin to role and converts value' do
41
- get :index, { admin: 'true' }
42
- expect(controller.params).to eq default_params.merge('role' => ['admin'])
43
- end
44
-
45
- context 'if param is not sent' do
46
- it 'leaves params as they were' do
47
- get :index
48
- expect(controller.params).to eq default_params
49
- end
50
- end
51
- end
52
-
53
- context 'and using a Proc converter' do
54
- controller(ActionController::Base) do
55
- rename :amount_due, to: :amount_due_in_cents, convert: ->(value) { value.to_i * 100 }
56
-
57
- def index
58
- head :ok
59
- end
60
- end
61
-
62
- it 'renames amount_due to amount_due_in_cents and converts value' do
63
- get :index, { amount_due: 100 }
64
- expect(controller.params).to eq default_params.merge('amount_due_in_cents' => 10000)
65
- end
66
-
67
- context 'if param is not sent' do
68
- it 'leaves params as they were' do
69
- get :index
70
- expect(controller.params).to eq default_params
71
- end
72
- end
73
- end
74
-
75
- context 'and using a method converter' do
76
- controller(ActionController::Base) do
77
- rename :amount_due, to: :amount_due_in_cents, convert: :to_cents
78
-
79
- def index
80
- head :ok
81
- end
82
-
83
- private
84
-
85
- def to_cents(value)
86
- value.to_f * 100
87
- end
88
- end
89
-
90
- it 'renames amount_due to amount_due_in_cents and converts value' do
91
- get :index, { amount_due: 100 }
92
- expect(controller.params).to eq default_params.merge('amount_due_in_cents' => 10000)
93
- end
94
-
95
- context 'if param is not sent' do
96
- it 'leaves params as they were' do
97
- get :index
98
- expect(controller.params).to eq default_params
99
- end
100
- end
101
- end
102
- end
103
-
104
- describe 'when nested params' do
105
- context 'using only one namespace' do
106
- controller(ActionController::Base) do
107
- rename :username, to: :login, namespace: :session
108
-
109
- def index
110
- head :ok
111
- end
112
- end
113
-
114
- it 'renames username to login' do
115
- get :index, { 'session' => { 'username' => 'aperson' } }
116
- expect(controller.params).to eq default_params.merge('session' => { 'login' => 'aperson' })
117
- end
118
-
119
- context 'if param is not sent' do
120
- it 'leaves params as they were' do
121
- get :index, { 'session' => '' }
122
- expect(controller.params).to eq default_params.merge('session' => '')
123
- end
124
- end
125
- end
126
-
127
- context 'using more than one nest levels' do
128
- controller(ActionController::Base) do
129
- rename :username, to: :login, namespace: [:session, :credentials]
130
-
131
- def index
132
- head :ok
133
- end
134
- end
135
-
136
- it 'renames username to login' do
137
- get :index, { 'session' => { 'credentials' => { 'username' => 'aperson' } } }
138
- expect(controller.params).to eq default_params.merge('session' => { 'credentials' => { 'login' => 'aperson' } })
139
- end
140
-
141
- context 'if param is not sent' do
142
- it 'leaves params as they were' do
143
- get :index, { 'session' => { 'credentials' => '' } }
144
- expect(controller.params).to eq default_params.merge('session' => { 'credentials' => '' })
145
- end
146
- end
147
-
148
- context 'if namespace is not sent' do
149
- it 'leaves params as they were' do
150
- get :index, { 'session' => '' }
151
- expect(controller.params).to eq default_params.merge('session' => '')
152
- end
153
- end
154
- end
155
- end
156
- end
157
-
158
- context 'when filtering' do
159
- context 'using only' do
160
- controller(ActionController::Base) do
161
- rename :username, to: :login, only: :show
162
-
163
- def index
164
- head :ok
165
- end
166
-
167
- def show
168
- head :ok
169
- end
170
- end
171
-
172
- describe 'show' do
173
- before { routes.draw { get 'show' => 'anonymous#show' } }
174
-
175
- it 'renames username to login' do
176
- get :show, { 'username' => 'aperson' }
177
- expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'show', 'login' => 'aperson')
178
- end
179
- end
180
-
181
- describe 'index' do
182
- before { routes.draw { get 'index' => 'anonymous#index' } }
183
-
184
- it 'keeps username param' do
185
- get :index, { 'username' => 'aperson' }
186
- expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'index', 'username' => 'aperson')
187
- end
188
- end
189
- end
190
-
191
- context 'using except' do
192
- controller(ActionController::Base) do
193
- rename :username, to: :login, except: :show
194
-
195
- def index
196
- head :ok
197
- end
198
-
199
- def show
200
- head :ok
201
- end
202
- end
203
-
204
- describe 'show' do
205
- before { routes.draw { get 'show' => 'anonymous#show' } }
206
-
207
- it 'keeps username param' do
208
- get :show, { 'username' => 'aperson' }
209
- expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'show', 'username' => 'aperson')
210
- end
211
- end
212
-
213
- describe 'index' do
214
- before { routes.draw { get 'index' => 'anonymous#index' } }
215
-
216
- it 'renames username to login' do
217
- get :index, { 'username' => 'aperson' }
218
- expect(controller.params).to eq('controller' => 'anonymous', 'action' => 'index', 'login' => 'aperson')
219
- end
220
- end
221
- end
222
- end
223
- end
224
- end