active_model_serializers 0.10.0 → 0.10.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.rubocop.yml +10 -5
- data/.travis.yml +41 -21
- data/CHANGELOG.md +200 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +25 -4
- data/README.md +166 -28
- data/Rakefile +5 -32
- data/active_model_serializers.gemspec +23 -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 +137 -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 +15 -3
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +4 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +10 -5
- data/lib/active_model/serializer/association.rb +64 -10
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
- data/lib/active_model/serializer/collection_serializer.rb +39 -13
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +87 -116
- data/lib/active_model/serializer/error_serializer.rb +13 -7
- data/lib/active_model/serializer/errors_serializer.rb +27 -20
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +2 -0
- data/lib/active_model/serializer/has_many_reflection.rb +5 -3
- data/lib/active_model/serializer/has_one_reflection.rb +3 -4
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +136 -130
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +132 -67
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +308 -82
- data/lib/active_model_serializers/adapter/attributes.rb +5 -66
- data/lib/active_model_serializers/adapter/base.rb +41 -39
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
- data/lib/active_model_serializers/adapter/json_api.rb +84 -65
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +9 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +3 -2
- data/lib/active_model_serializers/deserialization.rb +4 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +111 -28
- data/lib/active_model_serializers/railtie.rb +7 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
- data/lib/active_model_serializers/serializable_resource.rb +10 -7
- data/lib/active_model_serializers/serialization_context.rb +12 -3
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +35 -10
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +6 -4
- data/lib/grape/active_model_serializers.rb +9 -5
- data/lib/grape/formatters/active_model_serializers.rb +21 -2
- data/lib/grape/helpers/active_model_serializers.rb +3 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +16 -5
- data/test/action_controller/explicit_serializer_test.rb +7 -4
- data/test/action_controller/json/include_test.rb +108 -27
- data/test/action_controller/json_api/deserialization_test.rb +3 -1
- data/test/action_controller/json_api/errors_test.rb +10 -9
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +31 -24
- data/test/action_controller/json_api/pagination_test.rb +33 -23
- data/test/action_controller/json_api/transform_test.rb +13 -3
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +234 -0
- data/test/action_controller/serialization_scope_name_test.rb +14 -6
- data/test/action_controller/serialization_test.rb +23 -12
- data/test/active_model_serializers/adapter_for_test.rb +2 -0
- data/test/active_model_serializers/json_pointer_test.rb +17 -13
- data/test/active_model_serializers/logging_test.rb +2 -0
- data/test/active_model_serializers/model_test.rb +139 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +14 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +25 -10
- data/test/active_model_serializers/test/schema_test.rb +5 -2
- data/test/active_model_serializers/test/serializer_test.rb +2 -0
- data/test/active_record_test.rb +2 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +2 -0
- data/test/adapter/json/belongs_to_test.rb +2 -0
- data/test/adapter/json/collection_test.rb +16 -0
- data/test/adapter/json/has_many_test.rb +12 -2
- data/test/adapter/json/transform_test.rb +17 -15
- data/test/adapter/json_api/belongs_to_test.rb +2 -0
- data/test/adapter/json_api/collection_test.rb +6 -3
- data/test/adapter/json_api/errors_test.rb +19 -19
- data/test/adapter/json_api/fields_test.rb +14 -3
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
- data/test/adapter/json_api/has_many_test.rb +51 -20
- data/test/adapter/json_api/has_one_test.rb +2 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +7 -7
- data/test/adapter/json_api/linked_test.rb +35 -12
- data/test/adapter/json_api/links_test.rb +22 -3
- data/test/adapter/json_api/pagination_links_test.rb +55 -13
- data/test/adapter/json_api/parse_test.rb +3 -1
- data/test/adapter/json_api/relationship_test.rb +311 -73
- data/test/adapter/json_api/resource_meta_test.rb +5 -3
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
- data/test/adapter/json_api/transform_test.rb +265 -253
- data/test/adapter/json_api/type_test.rb +170 -36
- data/test/adapter/json_test.rb +10 -7
- data/test/adapter/null_test.rb +3 -2
- data/test/adapter/polymorphic_test.rb +54 -5
- data/test/adapter_test.rb +3 -1
- data/test/array_serializer_test.rb +2 -0
- data/test/benchmark/app.rb +3 -1
- data/test/benchmark/benchmarking_support.rb +3 -1
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +18 -16
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +23 -10
- data/test/benchmark/controllers.rb +18 -17
- data/test/benchmark/fixtures.rb +74 -72
- data/test/cache_test.rb +301 -69
- data/test/collection_serializer_test.rb +33 -14
- data/test/fixtures/active_record.rb +47 -10
- data/test/fixtures/poro.rb +128 -183
- data/test/generators/scaffold_controller_generator_test.rb +2 -0
- data/test/generators/serializer_generator_test.rb +25 -5
- data/test/grape_test.rb +172 -56
- data/test/lint_test.rb +3 -1
- data/test/logger_test.rb +15 -11
- data/test/poro_test.rb +2 -0
- data/test/serializable_resource_test.rb +20 -22
- data/test/serializers/association_macros_test.rb +5 -2
- data/test/serializers/associations_test.rb +274 -49
- data/test/serializers/attribute_test.rb +7 -3
- data/test/serializers/attributes_test.rb +3 -1
- data/test/serializers/caching_configuration_test_isolated.rb +8 -6
- data/test/serializers/configuration_test.rb +2 -0
- data/test/serializers/fieldset_test.rb +3 -1
- data/test/serializers/meta_test.rb +14 -6
- data/test/serializers/options_test.rb +19 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +5 -3
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +3 -1
- data/test/serializers/serialization_test.rb +4 -2
- data/test/serializers/serializer_for_test.rb +14 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/isolated_unit.rb +11 -4
- data/test/support/rails5_shims.rb +10 -2
- data/test/support/rails_app.rb +4 -9
- data/test/support/serialization_testing.rb +33 -5
- data/test/test_helper.rb +15 -0
- metadata +126 -46
- 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
data/test/grape_test.rb
CHANGED
@@ -1,82 +1,198 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
|
-
|
4
|
+
TestHelper.silence_warnings do
|
5
|
+
require 'grape'
|
6
|
+
end
|
3
7
|
require 'grape/active_model_serializers'
|
8
|
+
require 'kaminari'
|
9
|
+
require 'kaminari/hooks'
|
10
|
+
::Kaminari::Hooks.init
|
11
|
+
|
12
|
+
module ActiveModelSerializers
|
13
|
+
class GrapeTest < ActiveSupport::TestCase
|
14
|
+
include Rack::Test::Methods
|
15
|
+
module Models
|
16
|
+
def self.model1
|
17
|
+
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
18
|
+
end
|
4
19
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
def self.model1
|
9
|
-
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
10
|
-
end
|
20
|
+
def self.model2
|
21
|
+
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
|
22
|
+
end
|
11
23
|
|
12
|
-
|
13
|
-
|
14
|
-
|
24
|
+
def self.all
|
25
|
+
@all ||=
|
26
|
+
begin
|
27
|
+
model1.save!
|
28
|
+
model2.save!
|
29
|
+
ARModels::Post.all
|
30
|
+
end
|
31
|
+
end
|
15
32
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
33
|
+
def self.reset_all
|
34
|
+
ARModels::Post.delete_all
|
35
|
+
@all = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.collection_per
|
39
|
+
2
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.collection
|
43
|
+
@collection ||=
|
44
|
+
begin
|
45
|
+
Kaminari.paginate_array(
|
46
|
+
[
|
47
|
+
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
48
|
+
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
49
|
+
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
50
|
+
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
51
|
+
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
52
|
+
]
|
53
|
+
).page(1).per(collection_per)
|
54
|
+
end
|
55
|
+
end
|
23
56
|
end
|
24
|
-
end
|
25
57
|
|
26
|
-
|
27
|
-
|
28
|
-
|
58
|
+
class GrapeTest < Grape::API
|
59
|
+
format :json
|
60
|
+
TestHelper.silence_warnings do
|
61
|
+
include Grape::ActiveModelSerializers
|
62
|
+
end
|
29
63
|
|
30
|
-
|
31
|
-
|
32
|
-
|
64
|
+
def self.resources(*)
|
65
|
+
TestHelper.silence_warnings do
|
66
|
+
super
|
67
|
+
end
|
33
68
|
end
|
34
69
|
|
35
|
-
|
36
|
-
|
37
|
-
|
70
|
+
resources :grape do
|
71
|
+
get '/render' do
|
72
|
+
render Models.model1
|
73
|
+
end
|
74
|
+
|
75
|
+
get '/render_with_json_api' do
|
76
|
+
post = Models.model1
|
77
|
+
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
78
|
+
end
|
79
|
+
|
80
|
+
get '/render_array_with_json_api' do
|
81
|
+
posts = Models.all
|
82
|
+
render posts, adapter: :json_api
|
83
|
+
end
|
84
|
+
|
85
|
+
get '/render_collection_with_json_api' do
|
86
|
+
posts = Models.collection
|
87
|
+
render posts, adapter: :json_api
|
88
|
+
end
|
89
|
+
|
90
|
+
get '/render_with_implicit_formatter' do
|
91
|
+
Models.model1
|
92
|
+
end
|
93
|
+
|
94
|
+
get '/render_array_with_implicit_formatter' do
|
95
|
+
Models.all
|
96
|
+
end
|
97
|
+
|
98
|
+
get '/render_collection_with_implicit_formatter' do
|
99
|
+
Models.collection
|
100
|
+
end
|
38
101
|
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def app
|
105
|
+
Grape::Middleware::Globals.new(GrapeTest.new)
|
106
|
+
end
|
39
107
|
|
40
|
-
|
41
|
-
|
42
|
-
|
108
|
+
extend Minitest::Assertions
|
109
|
+
def self.run_one_method(*)
|
110
|
+
_, stderr = capture_io do
|
111
|
+
super
|
43
112
|
end
|
113
|
+
fail Minitest::Assertion, stderr if stderr !~ /grape/
|
44
114
|
end
|
45
|
-
end
|
46
115
|
|
47
|
-
|
48
|
-
|
49
|
-
|
116
|
+
def test_formatter_returns_json
|
117
|
+
get '/grape/render'
|
118
|
+
|
119
|
+
post = Models.model1
|
120
|
+
serializable_resource = serializable(post)
|
50
121
|
|
51
|
-
|
52
|
-
|
122
|
+
assert last_response.ok?
|
123
|
+
assert_equal serializable_resource.to_json, last_response.body
|
124
|
+
end
|
53
125
|
|
54
|
-
|
55
|
-
|
126
|
+
def test_render_helper_passes_through_options_correctly
|
127
|
+
get '/grape/render_with_json_api'
|
56
128
|
|
57
|
-
|
58
|
-
|
59
|
-
end
|
129
|
+
post = Models.model1
|
130
|
+
serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
|
60
131
|
|
61
|
-
|
62
|
-
|
132
|
+
assert last_response.ok?
|
133
|
+
assert_equal serializable_resource.to_json, last_response.body
|
134
|
+
end
|
63
135
|
|
64
|
-
|
65
|
-
|
136
|
+
def test_formatter_handles_arrays
|
137
|
+
get '/grape/render_array_with_json_api'
|
66
138
|
|
67
|
-
|
68
|
-
|
69
|
-
end
|
139
|
+
posts = Models.all
|
140
|
+
serializable_resource = serializable(posts, adapter: :json_api)
|
70
141
|
|
71
|
-
|
72
|
-
|
142
|
+
assert last_response.ok?
|
143
|
+
assert_equal serializable_resource.to_json, last_response.body
|
144
|
+
ensure
|
145
|
+
Models.reset_all
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_formatter_handles_collections
|
149
|
+
get '/grape/render_collection_with_json_api'
|
150
|
+
assert last_response.ok?
|
73
151
|
|
74
|
-
|
75
|
-
|
152
|
+
representation = JSON.parse(last_response.body)
|
153
|
+
assert representation.include?('data')
|
154
|
+
assert representation['data'].count == Models.collection_per
|
155
|
+
assert representation.include?('links')
|
156
|
+
assert representation['links'].count > 0
|
157
|
+
end
|
76
158
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
159
|
+
def test_implicit_formatter
|
160
|
+
post = Models.model1
|
161
|
+
serializable_resource = serializable(post, adapter: :json_api)
|
162
|
+
|
163
|
+
with_adapter :json_api do
|
164
|
+
get '/grape/render_with_implicit_formatter'
|
165
|
+
end
|
166
|
+
|
167
|
+
assert last_response.ok?
|
168
|
+
assert_equal serializable_resource.to_json, last_response.body
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_implicit_formatter_handles_arrays
|
172
|
+
posts = Models.all
|
173
|
+
serializable_resource = serializable(posts, adapter: :json_api)
|
174
|
+
|
175
|
+
with_adapter :json_api do
|
176
|
+
get '/grape/render_array_with_implicit_formatter'
|
177
|
+
end
|
178
|
+
|
179
|
+
assert last_response.ok?
|
180
|
+
assert_equal serializable_resource.to_json, last_response.body
|
181
|
+
ensure
|
182
|
+
Models.reset_all
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_implicit_formatter_handles_collections
|
186
|
+
with_adapter :json_api do
|
187
|
+
get '/grape/render_collection_with_implicit_formatter'
|
188
|
+
end
|
189
|
+
|
190
|
+
representation = JSON.parse(last_response.body)
|
191
|
+
assert last_response.ok?
|
192
|
+
assert representation.include?('data')
|
193
|
+
assert representation['data'].count == Models.collection_per
|
194
|
+
assert representation.include?('links')
|
195
|
+
assert representation['links'].count > 0
|
196
|
+
end
|
81
197
|
end
|
82
198
|
end
|
data/test/lint_test.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
module ActiveModel
|
@@ -30,7 +32,7 @@ module ActiveModel
|
|
30
32
|
def errors
|
31
33
|
end
|
32
34
|
|
33
|
-
def self.human_attribute_name(
|
35
|
+
def self.human_attribute_name(_, _ = {})
|
34
36
|
end
|
35
37
|
|
36
38
|
def self.lookup_ancestors
|
data/test/logger_test.rb
CHANGED
@@ -1,18 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
module ActiveModelSerializers
|
6
|
+
class LoggerTest < ActiveSupport::TestCase
|
7
|
+
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
8
|
+
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
9
|
+
end
|
7
10
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
+
def test_logger_can_be_set
|
12
|
+
original_logger = ActiveModelSerializers.logger
|
13
|
+
logger = Logger.new(STDOUT)
|
11
14
|
|
12
|
-
|
15
|
+
ActiveModelSerializers.logger = logger
|
13
16
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
assert_equal ActiveModelSerializers.logger, logger
|
18
|
+
ensure
|
19
|
+
ActiveModelSerializers.logger = original_logger
|
20
|
+
end
|
17
21
|
end
|
18
22
|
end
|
data/test/poro_test.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
module ActiveModelSerializers
|
4
6
|
class SerializableResourceTest < ActiveSupport::TestCase
|
5
7
|
def setup
|
6
|
-
@resource = Profile.new(
|
8
|
+
@resource = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
7
9
|
@serializer = ProfileSerializer.new(@resource)
|
8
10
|
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
9
11
|
@serializable_resource = SerializableResource.new(@resource)
|
@@ -32,11 +34,11 @@ module ActiveModelSerializers
|
|
32
34
|
end
|
33
35
|
|
34
36
|
def test_use_adapter_with_adapter_option
|
35
|
-
assert SerializableResource.new(@resource,
|
37
|
+
assert SerializableResource.new(@resource, adapter: 'json').use_adapter?
|
36
38
|
end
|
37
39
|
|
38
40
|
def test_use_adapter_with_adapter_option_as_false
|
39
|
-
refute SerializableResource.new(@resource,
|
41
|
+
refute SerializableResource.new(@resource, adapter: false).use_adapter?
|
40
42
|
end
|
41
43
|
|
42
44
|
class SerializableResourceErrorsTest < Minitest::Test
|
@@ -45,15 +47,13 @@ module ActiveModelSerializers
|
|
45
47
|
resource = ModelWithErrors.new
|
46
48
|
resource.errors.add(:name, 'must be awesome')
|
47
49
|
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
48
|
-
resource,
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
56
|
-
]
|
50
|
+
resource, serializer: ActiveModel::Serializer::ErrorSerializer,
|
51
|
+
adapter: :json_api
|
52
|
+
)
|
53
|
+
expected_response_document = {
|
54
|
+
errors: [
|
55
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'must be awesome' }
|
56
|
+
]
|
57
57
|
}
|
58
58
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
59
59
|
end
|
@@ -65,16 +65,14 @@ module ActiveModelSerializers
|
|
65
65
|
resource.errors.add(:title, 'must be amazing')
|
66
66
|
resources << ModelWithErrors.new
|
67
67
|
serializable_resource = SerializableResource.new(
|
68
|
-
resources,
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
|
77
|
-
]
|
68
|
+
resources, serializer: ActiveModel::Serializer::ErrorsSerializer,
|
69
|
+
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
70
|
+
adapter: :json_api
|
71
|
+
)
|
72
|
+
expected_response_document = {
|
73
|
+
errors: [
|
74
|
+
{ source: { pointer: '/data/attributes/title' }, detail: 'must be amazing' }
|
75
|
+
]
|
78
76
|
}
|
79
77
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
80
78
|
end
|
@@ -1,9 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'test_helper'
|
2
4
|
|
3
5
|
module ActiveModel
|
4
6
|
class Serializer
|
5
7
|
class AssociationMacrosTest < ActiveSupport::TestCase
|
6
|
-
AuthorSummarySerializer
|
8
|
+
class AuthorSummarySerializer < ActiveModel::Serializer; end
|
9
|
+
|
7
10
|
class AssociationsTestSerializer < Serializer
|
8
11
|
belongs_to :author, serializer: AuthorSummarySerializer
|
9
12
|
has_many :comments
|
@@ -11,7 +14,7 @@ module ActiveModel
|
|
11
14
|
end
|
12
15
|
|
13
16
|
def before_setup
|
14
|
-
@reflections = AssociationsTestSerializer._reflections
|
17
|
+
@reflections = AssociationsTestSerializer._reflections.values
|
15
18
|
end
|
16
19
|
|
17
20
|
def test_has_one_defines_reflection
|