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