active_model_serializers 0.10.0 → 0.10.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +6 -5
- data/.travis.yml +30 -21
- data/CHANGELOG.md +172 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +23 -4
- data/README.md +166 -28
- data/Rakefile +3 -32
- data/active_model_serializers.gemspec +22 -25
- data/appveyor.yml +10 -6
- data/bin/rubocop +38 -0
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +35 -11
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +86 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +1 -1
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +63 -25
- data/docs/general/serializers.md +125 -14
- data/docs/howto/add_pagination_links.md +16 -17
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +11 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +67 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +62 -10
- data/lib/active_model/serializer/belongs_to_reflection.rb +4 -3
- data/lib/active_model/serializer/collection_serializer.rb +39 -13
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +82 -115
- 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/has_many_reflection.rb +3 -3
- data/lib/active_model/serializer/has_one_reflection.rb +1 -4
- data/lib/active_model/serializer/lazy_association.rb +95 -0
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +127 -67
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +297 -79
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +39 -39
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +2 -2
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +47 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +75 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +39 -10
- data/lib/active_model_serializers/adapter/json_api.rb +71 -57
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +109 -28
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/active_model_serializers.rb +16 -1
- data/lib/generators/rails/resource_override.rb +1 -1
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/lib/tasks/rubocop.rake +53 -0
- data/test/action_controller/adapter_selector_test.rb +14 -5
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/deserialization_test.rb +1 -1
- data/test/action_controller/json_api/errors_test.rb +8 -9
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +31 -23
- data/test/action_controller/json_api/transform_test.rb +11 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +12 -6
- data/test/action_controller/serialization_test.rb +12 -9
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/model_test.rb +137 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/active_model_serializers/test/schema_test.rb +3 -2
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/has_many_test.rb +10 -2
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +12 -3
- data/test/adapter/json_api/has_many_test.rb +49 -20
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +213 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +53 -13
- data/test/adapter/json_api/parse_test.rb +1 -1
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +263 -253
- data/test/adapter/json_api/type_test.rb +168 -36
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +52 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +21 -10
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +235 -69
- data/test/collection_serializer_test.rb +31 -14
- data/test/fixtures/active_record.rb +45 -10
- data/test/fixtures/poro.rb +124 -181
- data/test/generators/serializer_generator_test.rb +23 -5
- data/test/grape_test.rb +170 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +222 -49
- data/test/serializers/attribute_test.rb +5 -3
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/caching_configuration_test_isolated.rb +6 -6
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/options_test.rb +17 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
- data/test/serializers/reflection_test.rb +427 -0
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serialization_test.rb +2 -2
- data/test/serializers/serializer_for_test.rb +12 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/isolated_unit.rb +9 -4
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +31 -5
- data/test/test_helper.rb +13 -0
- metadata +130 -71
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/adapter/json_api/resource_identifier_test.rb +0 -85
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,25 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Type
|
4
|
-
extend ActiveSupport::Concern
|
5
|
-
|
6
|
-
included do
|
7
|
-
with_options instance_writer: false, instance_reader: true do |serializer|
|
8
|
-
serializer.class_attribute :_type # @api private
|
9
|
-
end
|
10
|
-
|
11
|
-
extend ActiveSupport::Autoload
|
12
|
-
end
|
13
|
-
|
14
|
-
module ClassMethods
|
15
|
-
# Set the JSON API type of a serializer.
|
16
|
-
# @example
|
17
|
-
# class AdminAuthorSerializer < ActiveModel::Serializer
|
18
|
-
# type 'authors'
|
19
|
-
def type(type)
|
20
|
-
self._type = type && type.to_s
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,70 +0,0 @@
|
|
1
|
-
require 'active_support/core_ext/hash/keys'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
module KeyTransform
|
5
|
-
module_function
|
6
|
-
|
7
|
-
# Transforms values to UpperCamelCase or PascalCase.
|
8
|
-
#
|
9
|
-
# @example:
|
10
|
-
# "some_key" => "SomeKey",
|
11
|
-
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
|
12
|
-
def camel(value)
|
13
|
-
case value
|
14
|
-
when Hash then value.deep_transform_keys! { |key| camel(key) }
|
15
|
-
when Symbol then camel(value.to_s).to_sym
|
16
|
-
when String then value.underscore.camelize
|
17
|
-
else value
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# Transforms values to camelCase.
|
22
|
-
#
|
23
|
-
# @example:
|
24
|
-
# "some_key" => "someKey",
|
25
|
-
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L66-L76 ActiveSupport::Inflector.camelize}
|
26
|
-
def camel_lower(value)
|
27
|
-
case value
|
28
|
-
when Hash then value.deep_transform_keys! { |key| camel_lower(key) }
|
29
|
-
when Symbol then camel_lower(value.to_s).to_sym
|
30
|
-
when String then value.underscore.camelize(:lower)
|
31
|
-
else value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
# Transforms values to dashed-case.
|
36
|
-
# This is the default case for the JsonApi adapter.
|
37
|
-
#
|
38
|
-
# @example:
|
39
|
-
# "some_key" => "some-key",
|
40
|
-
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L185-L187 ActiveSupport::Inflector.dasherize}
|
41
|
-
def dash(value)
|
42
|
-
case value
|
43
|
-
when Hash then value.deep_transform_keys! { |key| dash(key) }
|
44
|
-
when Symbol then dash(value.to_s).to_sym
|
45
|
-
when String then value.underscore.dasherize
|
46
|
-
else value
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Transforms values to underscore_case.
|
51
|
-
# This is the default case for deserialization in the JsonApi adapter.
|
52
|
-
#
|
53
|
-
# @example:
|
54
|
-
# "some-key" => "some_key",
|
55
|
-
# @see {https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb#L89-L98 ActiveSupport::Inflector.underscore}
|
56
|
-
def underscore(value)
|
57
|
-
case value
|
58
|
-
when Hash then value.deep_transform_keys! { |key| underscore(key) }
|
59
|
-
when Symbol then underscore(value.to_s).to_sym
|
60
|
-
when String then value.underscore
|
61
|
-
else value
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
# Returns the value unaltered
|
66
|
-
def unaltered(value)
|
67
|
-
value
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
@@ -1,263 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
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
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
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
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
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
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
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
|
261
|
-
end
|
262
|
-
end
|
263
|
-
end
|
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
module Adapter
|
5
|
-
class JsonApi
|
6
|
-
class HasManyEmbedIdsTest < ActiveSupport::TestCase
|
7
|
-
def setup
|
8
|
-
@author = Author.new(name: 'Steve K.')
|
9
|
-
@author.bio = nil
|
10
|
-
@author.roles = nil
|
11
|
-
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
12
|
-
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
13
|
-
@author.posts = [@first_post, @second_post]
|
14
|
-
@first_post.author = @author
|
15
|
-
@second_post.author = @author
|
16
|
-
@first_post.comments = []
|
17
|
-
@second_post.comments = []
|
18
|
-
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
19
|
-
@first_post.blog = @blog
|
20
|
-
@second_post.blog = nil
|
21
|
-
|
22
|
-
@serializer = AuthorSerializer.new(@author)
|
23
|
-
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_includes_comment_ids
|
27
|
-
expected = {
|
28
|
-
data: [
|
29
|
-
{ type: 'posts', id: '1' },
|
30
|
-
{ type: 'posts', id: '2' }
|
31
|
-
]
|
32
|
-
}
|
33
|
-
|
34
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_no_includes_linked_comments
|
38
|
-
assert_nil @adapter.serializable_hash[:linked]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,199 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
module Adapter
|
6
|
-
class JsonApi
|
7
|
-
class RelationshipTest < ActiveSupport::TestCase
|
8
|
-
RelationshipAuthor = Class.new(::Model)
|
9
|
-
class RelationshipAuthorSerializer < ActiveModel::Serializer
|
10
|
-
has_one :bio do
|
11
|
-
link :self, '//example.com/link_author/relationships/bio'
|
12
|
-
end
|
13
|
-
|
14
|
-
has_one :profile do
|
15
|
-
link :related do
|
16
|
-
"//example.com/profiles/#{object.profile.id}"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
has_many :locations do
|
21
|
-
link :related do
|
22
|
-
ids = object.locations.map(&:id).join(',')
|
23
|
-
href "//example.com/locations/#{ids}"
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
has_many :posts do
|
28
|
-
link :related do
|
29
|
-
ids = object.posts.map(&:id).join(',')
|
30
|
-
href "//example.com/posts/#{ids}"
|
31
|
-
meta ids: ids
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
has_many :comments do
|
36
|
-
link :self do
|
37
|
-
meta ids: [1]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
has_many :roles do |serializer|
|
42
|
-
meta count: object.posts.count
|
43
|
-
serializer.cached_roles
|
44
|
-
end
|
45
|
-
|
46
|
-
has_one :blog do
|
47
|
-
link :self, '//example.com/link_author/relationships/blog'
|
48
|
-
include_data false
|
49
|
-
end
|
50
|
-
|
51
|
-
belongs_to :reviewer do
|
52
|
-
meta name: 'Dan Brown'
|
53
|
-
include_data true
|
54
|
-
end
|
55
|
-
|
56
|
-
has_many :likes do
|
57
|
-
link :related do
|
58
|
-
ids = object.likes.map(&:id).join(',')
|
59
|
-
href "//example.com/likes/#{ids}"
|
60
|
-
meta ids: ids
|
61
|
-
end
|
62
|
-
meta liked: object.likes.any?
|
63
|
-
end
|
64
|
-
|
65
|
-
def cached_roles
|
66
|
-
[
|
67
|
-
Role.new(id: 'from-serializer-method')
|
68
|
-
]
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
def setup
|
73
|
-
@post = Post.new(id: 1337, comments: [], author: nil)
|
74
|
-
@blog = Blog.new(id: 1337, name: 'extra')
|
75
|
-
@bio = Bio.new(id: 1337)
|
76
|
-
@like = Like.new(id: 1337)
|
77
|
-
@role = Role.new(id: 'from-record')
|
78
|
-
@profile = Profile.new(id: 1337)
|
79
|
-
@location = Location.new(id: 1337)
|
80
|
-
@reviewer = Author.new(id: 1337)
|
81
|
-
@comment = Comment.new(id: 1337)
|
82
|
-
@author = RelationshipAuthor.new(
|
83
|
-
id: 1337,
|
84
|
-
posts: [@post],
|
85
|
-
blog: @blog,
|
86
|
-
reviewer: @reviewer,
|
87
|
-
bio: @bio,
|
88
|
-
likes: [@like],
|
89
|
-
roles: [@role],
|
90
|
-
locations: [@location],
|
91
|
-
profile: @profile,
|
92
|
-
comments: [@comment]
|
93
|
-
)
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_relationship_simple_link
|
97
|
-
expected = {
|
98
|
-
data: {
|
99
|
-
id: '1337',
|
100
|
-
type: 'bios'
|
101
|
-
},
|
102
|
-
links: {
|
103
|
-
self: '//example.com/link_author/relationships/bio'
|
104
|
-
}
|
105
|
-
}
|
106
|
-
assert_relationship(:bio, expected)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_relationship_block_link
|
110
|
-
expected = {
|
111
|
-
data: { id: '1337', type: 'profiles' },
|
112
|
-
links: { related: '//example.com/profiles/1337' }
|
113
|
-
}
|
114
|
-
assert_relationship(:profile, expected)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_relationship_block_link_href
|
118
|
-
expected = {
|
119
|
-
data: [{ id: '1337', type: 'locations' }],
|
120
|
-
links: {
|
121
|
-
related: { href: '//example.com/locations/1337' }
|
122
|
-
}
|
123
|
-
}
|
124
|
-
assert_relationship(:locations, expected)
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_relationship_block_link_href_and_meta
|
128
|
-
expected = {
|
129
|
-
data: [{ id: '1337', type: 'posts' }],
|
130
|
-
links: {
|
131
|
-
related: {
|
132
|
-
href: '//example.com/posts/1337',
|
133
|
-
meta: { ids: '1337' }
|
134
|
-
}
|
135
|
-
}
|
136
|
-
}
|
137
|
-
assert_relationship(:posts, expected)
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_relationship_block_link_meta
|
141
|
-
expected = {
|
142
|
-
data: [{ id: '1337', type: 'comments' }],
|
143
|
-
links: {
|
144
|
-
self: {
|
145
|
-
meta: { ids: [1] }
|
146
|
-
}
|
147
|
-
}
|
148
|
-
}
|
149
|
-
assert_relationship(:comments, expected)
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_relationship_meta
|
153
|
-
expected = {
|
154
|
-
data: [{ id: 'from-serializer-method', type: 'roles' }],
|
155
|
-
meta: { count: 1 }
|
156
|
-
}
|
157
|
-
assert_relationship(:roles, expected)
|
158
|
-
end
|
159
|
-
|
160
|
-
def test_relationship_not_including_data
|
161
|
-
expected = {
|
162
|
-
links: { self: '//example.com/link_author/relationships/blog' }
|
163
|
-
}
|
164
|
-
assert_relationship(:blog, expected)
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_relationship_including_data_explicit
|
168
|
-
expected = {
|
169
|
-
data: { id: '1337', type: 'authors' },
|
170
|
-
meta: { name: 'Dan Brown' }
|
171
|
-
}
|
172
|
-
assert_relationship(:reviewer, expected)
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_relationship_with_everything
|
176
|
-
expected = {
|
177
|
-
data: [{ id: '1337', type: 'likes' }],
|
178
|
-
links: {
|
179
|
-
related: {
|
180
|
-
href: '//example.com/likes/1337',
|
181
|
-
meta: { ids: '1337' }
|
182
|
-
}
|
183
|
-
},
|
184
|
-
meta: { liked: true }
|
185
|
-
}
|
186
|
-
assert_relationship(:likes, expected)
|
187
|
-
end
|
188
|
-
|
189
|
-
private
|
190
|
-
|
191
|
-
def assert_relationship(relationship_name, expected)
|
192
|
-
hash = serializable(@author, adapter: :json_api).serializable_hash
|
193
|
-
assert_equal(expected, hash[:data][:relationships][relationship_name])
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
@@ -1,85 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
module Adapter
|
5
|
-
class JsonApi
|
6
|
-
class ResourceIdentifierTest < ActiveSupport::TestCase
|
7
|
-
class WithDefinedTypeSerializer < ActiveModel::Serializer
|
8
|
-
type 'with_defined_type'
|
9
|
-
end
|
10
|
-
|
11
|
-
class WithDefinedIdSerializer < ActiveModel::Serializer
|
12
|
-
def id
|
13
|
-
'special_id'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class FragmentedSerializer < ActiveModel::Serializer; end
|
18
|
-
|
19
|
-
setup do
|
20
|
-
@model = Author.new(id: 1, name: 'Steve K.')
|
21
|
-
ActionController::Base.cache_store.clear
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_defined_type
|
25
|
-
test_type(WithDefinedTypeSerializer, 'with-defined-type')
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_singular_type
|
29
|
-
test_type_inflection(AuthorSerializer, 'author', :singular)
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_plural_type
|
33
|
-
test_type_inflection(AuthorSerializer, 'authors', :plural)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_id_defined_on_object
|
37
|
-
test_id(AuthorSerializer, @model.id.to_s)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_id_defined_on_serializer
|
41
|
-
test_id(WithDefinedIdSerializer, 'special_id')
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_id_defined_on_fragmented
|
45
|
-
FragmentedSerializer.fragmented(WithDefinedIdSerializer.new(@model))
|
46
|
-
test_id(FragmentedSerializer, 'special_id')
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def test_type_inflection(serializer_class, expected_type, inflection)
|
52
|
-
original_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
53
|
-
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
54
|
-
test_type(serializer_class, expected_type)
|
55
|
-
ensure
|
56
|
-
ActiveModelSerializers.config.jsonapi_resource_type = original_inflection
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_type(serializer_class, expected_type)
|
60
|
-
serializer = serializer_class.new(@model)
|
61
|
-
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
62
|
-
expected = {
|
63
|
-
id: @model.id.to_s,
|
64
|
-
type: expected_type
|
65
|
-
}
|
66
|
-
|
67
|
-
assert_equal(expected, resource_identifier.as_json)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_id(serializer_class, id)
|
71
|
-
serializer = serializer_class.new(@model)
|
72
|
-
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
73
|
-
inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
74
|
-
type = @model.class.model_name.send(inflection)
|
75
|
-
expected = {
|
76
|
-
id: id,
|
77
|
-
type: type
|
78
|
-
}
|
79
|
-
|
80
|
-
assert_equal(expected, resource_identifier.as_json)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|