active_model_serializers 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +1 -0
- data/CHANGELOG.md +9 -1
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +15 -15
- data/docs/general/fields.md +31 -0
- data/docs/general/rendering.md +7 -2
- data/docs/general/serializers.md +32 -0
- data/docs/howto/add_pagination_links.md +2 -3
- data/docs/integrations/ember-and-json-api.md +25 -10
- data/lib/action_controller/serialization.rb +4 -3
- data/lib/active_model/serializer.rb +3 -4
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/associations.rb +2 -2
- data/lib/active_model/serializer/caching.rb +11 -8
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +1 -1
- data/lib/active_model_serializers/model.rb +1 -1
- data/lib/active_model_serializers/railtie.rb +1 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +37 -35
- data/lib/generators/rails/serializer_generator.rb +3 -3
- data/lib/grape/active_model_serializers.rb +7 -5
- data/test/action_controller/adapter_selector_test.rb +3 -3
- data/test/action_controller/json_api/errors_test.rb +5 -6
- data/test/action_controller/json_api/linked_test.rb +4 -4
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/serialization_test.rb +1 -1
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/key_transform_test.rb +254 -252
- data/test/active_model_serializers/model_test.rb +6 -4
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +2 -2
- data/test/adapter/json/transform_test.rb +14 -14
- data/test/adapter/json_api/errors_test.rb +9 -9
- data/test/adapter/json_api/has_many_test.rb +18 -18
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +1 -1
- data/test/adapter/json_api/pagination_links_test.rb +6 -6
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +218 -218
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -8
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/bm_caching.rb +1 -1
- data/test/cache_test.rb +29 -1
- data/test/collection_serializer_test.rb +2 -2
- data/test/fixtures/poro.rb +2 -2
- data/test/grape_test.rb +130 -128
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +12 -16
- data/test/serializers/associations_test.rb +10 -10
- data/test/serializers/attribute_test.rb +1 -1
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +3 -1
- data/test/support/isolated_unit.rb +4 -2
- data/test/support/serialization_testing.rb +7 -5
- metadata +3 -3
- data/.rubocop_todo.yml +0 -167
@@ -8,12 +8,11 @@ module ActionController
|
|
8
8
|
get :render_resource_with_errors
|
9
9
|
|
10
10
|
expected_errors_object = {
|
11
|
-
:
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
]
|
11
|
+
errors: [
|
12
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'cannot be nil' },
|
13
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'must be longer' },
|
14
|
+
{ source: { pointer: '/data/attributes/id' }, detail: 'must be a uuid' }
|
15
|
+
]
|
17
16
|
}.to_json
|
18
17
|
assert_equal json_reponse_body.to_json, expected_errors_object
|
19
18
|
end
|
@@ -87,7 +87,7 @@ module ActionController
|
|
87
87
|
setup do
|
88
88
|
@routes = Rails.application.routes.draw do
|
89
89
|
ActiveSupport::Deprecation.silence do
|
90
|
-
match ':action', :
|
90
|
+
match ':action', to: LinkedTestController, via: [:get, :post]
|
91
91
|
end
|
92
92
|
end
|
93
93
|
end
|
@@ -183,17 +183,17 @@ module ActionController
|
|
183
183
|
get '/render_resource_with_missing_nested_has_many_include'
|
184
184
|
response = JSON.parse(@response.body)
|
185
185
|
assert response.key? 'included'
|
186
|
-
refute
|
186
|
+
refute include_type?(response['included'], 'roles')
|
187
187
|
end
|
188
188
|
|
189
189
|
def test_render_collection_with_missing_nested_has_many_include
|
190
190
|
get '/render_collection_with_missing_nested_has_many_include'
|
191
191
|
response = JSON.parse(@response.body)
|
192
192
|
assert response.key? 'included'
|
193
|
-
assert
|
193
|
+
assert include_type?(response['included'], 'roles')
|
194
194
|
end
|
195
195
|
|
196
|
-
def
|
196
|
+
def include_type?(collection, value)
|
197
197
|
collection.detect { |i| i['type'] == value }
|
198
198
|
end
|
199
199
|
end
|
@@ -14,9 +14,9 @@ module ActionController
|
|
14
14
|
class PaginationTestController < ActionController::Base
|
15
15
|
def setup
|
16
16
|
@array = [
|
17
|
-
Profile.new(
|
18
|
-
Profile.new(
|
19
|
-
Profile.new(
|
17
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
18
|
+
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
19
|
+
Profile.new(name: 'Name 3', description: 'Description 3', comments: 'Comments 3')
|
20
20
|
]
|
21
21
|
end
|
22
22
|
|
@@ -48,10 +48,10 @@ module ActionController
|
|
48
48
|
|
49
49
|
def test_render_pagination_links_with_will_paginate
|
50
50
|
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
51
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
52
|
+
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
53
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
54
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
55
55
|
|
56
56
|
get :render_pagination_using_will_paginate, params: { page: { number: 2, size: 1 } }
|
57
57
|
response = JSON.parse(@response.body)
|
@@ -60,8 +60,8 @@ module ActionController
|
|
60
60
|
|
61
61
|
def test_render_only_last_and_next_pagination_links
|
62
62
|
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
63
|
-
|
64
|
-
|
63
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
64
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
65
65
|
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
|
66
66
|
response = JSON.parse(@response.body)
|
67
67
|
assert_equal expected_links, response['links']
|
@@ -69,10 +69,10 @@ module ActionController
|
|
69
69
|
|
70
70
|
def test_render_pagination_links_with_kaminari
|
71
71
|
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
72
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
73
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
74
|
+
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
75
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
76
76
|
get :render_pagination_using_kaminari, params: { page: { number: 2, size: 1 } }
|
77
77
|
response = JSON.parse(@response.body)
|
78
78
|
assert_equal expected_links, response['links']
|
@@ -80,8 +80,8 @@ module ActionController
|
|
80
80
|
|
81
81
|
def test_render_only_prev_and_first_pagination_links
|
82
82
|
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
83
|
-
|
84
|
-
|
83
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
84
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
|
85
85
|
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
|
86
86
|
response = JSON.parse(@response.body)
|
87
87
|
assert_equal expected_links, response['links']
|
@@ -89,8 +89,8 @@ module ActionController
|
|
89
89
|
|
90
90
|
def test_render_only_last_and_next_pagination_links_with_additional_params
|
91
91
|
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
92
|
-
|
93
|
-
|
92
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
93
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
94
94
|
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
|
95
95
|
response = JSON.parse(@response.body)
|
96
96
|
assert_equal expected_links, response['links']
|
@@ -98,8 +98,8 @@ module ActionController
|
|
98
98
|
|
99
99
|
def test_render_only_prev_and_first_pagination_links_with_additional_params
|
100
100
|
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
101
|
-
|
102
|
-
|
101
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
102
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
|
103
103
|
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
|
104
104
|
response = JSON.parse(@response.body)
|
105
105
|
assert_equal expected_links, response['links']
|
@@ -1,20 +1,22 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module ActiveModelSerializers
|
4
|
+
class JsonPointerTest < ActiveSupport::TestCase
|
5
|
+
def test_attribute_pointer
|
6
|
+
attribute_name = 'title'
|
7
|
+
pointer = ActiveModelSerializers::JsonPointer.new(:attribute, attribute_name)
|
8
|
+
assert_equal '/data/attributes/title', pointer
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def test_primary_data_pointer
|
12
|
+
pointer = ActiveModelSerializers::JsonPointer.new(:primary_data)
|
13
|
+
assert_equal '/data', pointer
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def test_unkown_data_pointer
|
17
|
+
assert_raises(TypeError) do
|
18
|
+
ActiveModelSerializers::JsonPointer.new(:unknown)
|
19
|
+
end
|
18
20
|
end
|
19
21
|
end
|
20
22
|
end
|
@@ -1,263 +1,265 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
3
|
+
module ActiveModelSerializers
|
4
|
+
class KeyTransformTest < ActiveSupport::TestCase
|
5
|
+
def test_camel
|
6
|
+
obj = Object.new
|
7
|
+
scenarios = [
|
8
|
+
{
|
9
|
+
value: { :"some-key" => 'value' },
|
10
|
+
expected: { SomeKey: 'value' }
|
11
|
+
},
|
12
|
+
{
|
13
|
+
value: { someKey: 'value' },
|
14
|
+
expected: { SomeKey: 'value' }
|
15
|
+
},
|
16
|
+
{
|
17
|
+
value: { some_key: 'value' },
|
18
|
+
expected: { SomeKey: 'value' }
|
19
|
+
},
|
20
|
+
{
|
21
|
+
value: { 'some-key' => 'value' },
|
22
|
+
expected: { 'SomeKey' => 'value' }
|
23
|
+
},
|
24
|
+
{
|
25
|
+
value: { 'someKey' => 'value' },
|
26
|
+
expected: { 'SomeKey' => 'value' }
|
27
|
+
},
|
28
|
+
{
|
29
|
+
value: { 'some_key' => 'value' },
|
30
|
+
expected: { 'SomeKey' => 'value' }
|
31
|
+
},
|
32
|
+
{
|
33
|
+
value: :"some-value",
|
34
|
+
expected: :SomeValue
|
35
|
+
},
|
36
|
+
{
|
37
|
+
value: :some_value,
|
38
|
+
expected: :SomeValue
|
39
|
+
},
|
40
|
+
{
|
41
|
+
value: :someValue,
|
42
|
+
expected: :SomeValue
|
43
|
+
},
|
44
|
+
{
|
45
|
+
value: 'some-value',
|
46
|
+
expected: 'SomeValue'
|
47
|
+
},
|
48
|
+
{
|
49
|
+
value: 'someValue',
|
50
|
+
expected: 'SomeValue'
|
51
|
+
},
|
52
|
+
{
|
53
|
+
value: 'some_value',
|
54
|
+
expected: 'SomeValue'
|
55
|
+
},
|
56
|
+
{
|
57
|
+
value: obj,
|
58
|
+
expected: obj
|
59
|
+
},
|
60
|
+
{
|
61
|
+
value: nil,
|
62
|
+
expected: nil
|
63
|
+
}
|
64
|
+
]
|
65
|
+
scenarios.each do |s|
|
66
|
+
result = ActiveModelSerializers::KeyTransform.camel(s[:value])
|
67
|
+
assert_equal s[:expected], result
|
68
|
+
end
|
67
69
|
end
|
68
|
-
end
|
69
70
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
71
|
+
def test_camel_lower
|
72
|
+
obj = Object.new
|
73
|
+
scenarios = [
|
74
|
+
{
|
75
|
+
value: { :"some-key" => 'value' },
|
76
|
+
expected: { someKey: 'value' }
|
77
|
+
},
|
78
|
+
{
|
79
|
+
value: { SomeKey: 'value' },
|
80
|
+
expected: { someKey: 'value' }
|
81
|
+
},
|
82
|
+
{
|
83
|
+
value: { some_key: 'value' },
|
84
|
+
expected: { someKey: 'value' }
|
85
|
+
},
|
86
|
+
{
|
87
|
+
value: { 'some-key' => 'value' },
|
88
|
+
expected: { 'someKey' => 'value' }
|
89
|
+
},
|
90
|
+
{
|
91
|
+
value: { 'SomeKey' => 'value' },
|
92
|
+
expected: { 'someKey' => 'value' }
|
93
|
+
},
|
94
|
+
{
|
95
|
+
value: { 'some_key' => 'value' },
|
96
|
+
expected: { 'someKey' => 'value' }
|
97
|
+
},
|
98
|
+
{
|
99
|
+
value: :"some-value",
|
100
|
+
expected: :someValue
|
101
|
+
},
|
102
|
+
{
|
103
|
+
value: :SomeValue,
|
104
|
+
expected: :someValue
|
105
|
+
},
|
106
|
+
{
|
107
|
+
value: :some_value,
|
108
|
+
expected: :someValue
|
109
|
+
},
|
110
|
+
{
|
111
|
+
value: 'some-value',
|
112
|
+
expected: 'someValue'
|
113
|
+
},
|
114
|
+
{
|
115
|
+
value: 'SomeValue',
|
116
|
+
expected: 'someValue'
|
117
|
+
},
|
118
|
+
{
|
119
|
+
value: 'some_value',
|
120
|
+
expected: 'someValue'
|
121
|
+
},
|
122
|
+
{
|
123
|
+
value: obj,
|
124
|
+
expected: obj
|
125
|
+
},
|
126
|
+
{
|
127
|
+
value: nil,
|
128
|
+
expected: nil
|
129
|
+
}
|
130
|
+
]
|
131
|
+
scenarios.each do |s|
|
132
|
+
result = ActiveModelSerializers::KeyTransform.camel_lower(s[:value])
|
133
|
+
assert_equal s[:expected], result
|
134
|
+
end
|
133
135
|
end
|
134
|
-
end
|
135
136
|
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
137
|
+
def test_dash
|
138
|
+
obj = Object.new
|
139
|
+
scenarios = [
|
140
|
+
{
|
141
|
+
value: { some_key: 'value' },
|
142
|
+
expected: { :"some-key" => 'value' }
|
143
|
+
},
|
144
|
+
{
|
145
|
+
value: { 'some_key' => 'value' },
|
146
|
+
expected: { 'some-key' => 'value' }
|
147
|
+
},
|
148
|
+
{
|
149
|
+
value: { SomeKey: 'value' },
|
150
|
+
expected: { :"some-key" => 'value' }
|
151
|
+
},
|
152
|
+
{
|
153
|
+
value: { 'SomeKey' => 'value' },
|
154
|
+
expected: { 'some-key' => 'value' }
|
155
|
+
},
|
156
|
+
{
|
157
|
+
value: { someKey: 'value' },
|
158
|
+
expected: { :"some-key" => 'value' }
|
159
|
+
},
|
160
|
+
{
|
161
|
+
value: { 'someKey' => 'value' },
|
162
|
+
expected: { 'some-key' => 'value' }
|
163
|
+
},
|
164
|
+
{
|
165
|
+
value: :some_value,
|
166
|
+
expected: :"some-value"
|
167
|
+
},
|
168
|
+
{
|
169
|
+
value: :SomeValue,
|
170
|
+
expected: :"some-value"
|
171
|
+
},
|
172
|
+
{
|
173
|
+
value: 'SomeValue',
|
174
|
+
expected: 'some-value'
|
175
|
+
},
|
176
|
+
{
|
177
|
+
value: :someValue,
|
178
|
+
expected: :"some-value"
|
179
|
+
},
|
180
|
+
{
|
181
|
+
value: 'someValue',
|
182
|
+
expected: 'some-value'
|
183
|
+
},
|
184
|
+
{
|
185
|
+
value: obj,
|
186
|
+
expected: obj
|
187
|
+
},
|
188
|
+
{
|
189
|
+
value: nil,
|
190
|
+
expected: nil
|
191
|
+
}
|
192
|
+
]
|
193
|
+
scenarios.each do |s|
|
194
|
+
result = ActiveModelSerializers::KeyTransform.dash(s[:value])
|
195
|
+
assert_equal s[:expected], result
|
196
|
+
end
|
195
197
|
end
|
196
|
-
end
|
197
198
|
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
199
|
+
def test_underscore
|
200
|
+
obj = Object.new
|
201
|
+
scenarios = [
|
202
|
+
{
|
203
|
+
value: { :"some-key" => 'value' },
|
204
|
+
expected: { some_key: 'value' }
|
205
|
+
},
|
206
|
+
{
|
207
|
+
value: { 'some-key' => 'value' },
|
208
|
+
expected: { 'some_key' => 'value' }
|
209
|
+
},
|
210
|
+
{
|
211
|
+
value: { SomeKey: 'value' },
|
212
|
+
expected: { some_key: 'value' }
|
213
|
+
},
|
214
|
+
{
|
215
|
+
value: { 'SomeKey' => 'value' },
|
216
|
+
expected: { 'some_key' => 'value' }
|
217
|
+
},
|
218
|
+
{
|
219
|
+
value: { someKey: 'value' },
|
220
|
+
expected: { some_key: 'value' }
|
221
|
+
},
|
222
|
+
{
|
223
|
+
value: { 'someKey' => 'value' },
|
224
|
+
expected: { 'some_key' => 'value' }
|
225
|
+
},
|
226
|
+
{
|
227
|
+
value: :"some-value",
|
228
|
+
expected: :some_value
|
229
|
+
},
|
230
|
+
{
|
231
|
+
value: :SomeValue,
|
232
|
+
expected: :some_value
|
233
|
+
},
|
234
|
+
{
|
235
|
+
value: :someValue,
|
236
|
+
expected: :some_value
|
237
|
+
},
|
238
|
+
{
|
239
|
+
value: 'some-value',
|
240
|
+
expected: 'some_value'
|
241
|
+
},
|
242
|
+
{
|
243
|
+
value: 'SomeValue',
|
244
|
+
expected: 'some_value'
|
245
|
+
},
|
246
|
+
{
|
247
|
+
value: 'someValue',
|
248
|
+
expected: 'some_value'
|
249
|
+
},
|
250
|
+
{
|
251
|
+
value: obj,
|
252
|
+
expected: obj
|
253
|
+
},
|
254
|
+
{
|
255
|
+
value: nil,
|
256
|
+
expected: nil
|
257
|
+
}
|
258
|
+
]
|
259
|
+
scenarios.each do |s|
|
260
|
+
result = ActiveModelSerializers::KeyTransform.underscore(s[:value])
|
261
|
+
assert_equal s[:expected], result
|
262
|
+
end
|
261
263
|
end
|
262
264
|
end
|
263
265
|
end
|