active_model_serializers 0.10.3 → 0.10.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +154 -2
- data/README.md +153 -15
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer.rb +275 -81
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +2 -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/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +53 -14
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -1
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/concerns/caching.rb +36 -23
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +6 -1
- data/lib/active_model/serializer/has_one_reflection.rb +3 -1
- 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 +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +122 -73
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model_serializers.rb +29 -11
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +4 -2
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api.rb +44 -26
- 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 +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +42 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -9
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +35 -18
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -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 +2 -0
- data/lib/active_model_serializers/model.rb +111 -30
- data/lib/active_model_serializers/model/caching.rb +25 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/generators/rails/resource_override.rb +3 -1
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +55 -0
- metadata +74 -291
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -102
- data/.simplecov +0 -110
- data/.travis.yml +0 -51
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -62
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -125
- data/docs/README.md +0 -42
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -247
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -169
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -14
- data/docs/general/rendering.md +0 -294
- data/docs/general/serializers.md +0 -461
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -137
- data/docs/howto/add_root_key.md +0 -55
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -65
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -144
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema.md +0 -151
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/concerns/associations.rb +0 -102
- data/lib/active_model/serializer/concerns/attributes.rb +0 -82
- data/lib/active_model/serializer/concerns/configuration.rb +0 -59
- data/lib/active_model/serializer/concerns/links.rb +0 -35
- data/lib/active_model/serializer/concerns/meta.rb +0 -29
- data/lib/active_model/serializer/concerns/type.rb +0 -25
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model_serializers/key_transform.rb +0 -74
- data/test/action_controller/adapter_selector_test.rb +0 -53
- data/test/action_controller/explicit_serializer_test.rb +0 -135
- data/test/action_controller/json/include_test.rb +0 -246
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -40
- data/test/action_controller/json_api/fields_test.rb +0 -57
- data/test/action_controller/json_api/linked_test.rb +0 -202
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -181
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -226
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- data/test/action_controller/serialization_test.rb +0 -472
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -22
- data/test/active_model_serializers/key_transform_test.rb +0 -297
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -22
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -143
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/attributes_test.rb +0 -43
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -104
- data/test/adapter/json/has_many_test.rb +0 -45
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -96
- data/test/adapter/json_api/errors_test.rb +0 -76
- data/test/adapter/json_api/fields_test.rb +0 -88
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -165
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -166
- data/test/adapter/json_api/json_api_test.rb +0 -33
- data/test/adapter/json_api/linked_test.rb +0 -413
- data/test/adapter/json_api/links_test.rb +0 -95
- data/test/adapter/json_api/pagination_links_test.rb +0 -193
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -397
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -504
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -46
- data/test/adapter/null_test.rb +0 -22
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -81
- data/test/benchmark/bm_adapter.rb +0 -38
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_lookup_chain.rb +0 -83
- data/test/benchmark/bm_transform.rb +0 -45
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -83
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -579
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -286
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -74
- data/test/grape_test.rb +0 -178
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -20
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -79
- data/test/serializers/association_macros_test.rb +0 -37
- data/test/serializers/associations_test.rb +0 -370
- data/test/serializers/attribute_test.rb +0 -151
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -202
- data/test/serializers/options_test.rb +0 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -136
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -87
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -82
- data/test/support/rails5_shims.rb +0 -53
- data/test/support/rails_app.rb +0 -36
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -71
- data/test/test_helper.rb +0 -58
@@ -1,181 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActionController
|
4
|
-
module Serialization
|
5
|
-
class JsonApi
|
6
|
-
class KeyTransformTest < ActionController::TestCase
|
7
|
-
class KeyTransformTestController < ActionController::Base
|
8
|
-
class Post < ::Model; end
|
9
|
-
class Author < ::Model; end
|
10
|
-
class TopComment < ::Model; end
|
11
|
-
class PostSerializer < ActiveModel::Serializer
|
12
|
-
type 'posts'
|
13
|
-
attributes :title, :body, :publish_at
|
14
|
-
belongs_to :author
|
15
|
-
has_many :top_comments
|
16
|
-
|
17
|
-
link(:post_authors) { 'https://example.com/post_authors' }
|
18
|
-
|
19
|
-
meta do
|
20
|
-
{
|
21
|
-
rating: 5,
|
22
|
-
favorite_count: 10
|
23
|
-
}
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
class AuthorSerializer < ActiveModel::Serializer
|
28
|
-
type 'authors'
|
29
|
-
attributes :first_name, :last_name
|
30
|
-
end
|
31
|
-
|
32
|
-
class TopCommentSerializer < ActiveModel::Serializer
|
33
|
-
type 'top_comments'
|
34
|
-
attributes :body
|
35
|
-
belongs_to :author
|
36
|
-
end
|
37
|
-
|
38
|
-
def setup_post
|
39
|
-
ActionController::Base.cache_store.clear
|
40
|
-
@author = Author.new(id: 1, first_name: 'Bob', last_name: 'Jones')
|
41
|
-
@comment1 = TopComment.new(id: 7, body: 'cool', author: @author)
|
42
|
-
@comment2 = TopComment.new(id: 12, body: 'awesome', author: @author)
|
43
|
-
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
44
|
-
author: @author, top_comments: [@comment1, @comment2],
|
45
|
-
publish_at: '2020-03-16T03:55:25.291Z')
|
46
|
-
@comment1.post = @post
|
47
|
-
@comment2.post = @post
|
48
|
-
end
|
49
|
-
|
50
|
-
def render_resource_with_transform
|
51
|
-
setup_post
|
52
|
-
render json: @post, serializer: PostSerializer, adapter: :json_api,
|
53
|
-
key_transform: :camel
|
54
|
-
end
|
55
|
-
|
56
|
-
def render_resource_with_transform_nil
|
57
|
-
setup_post
|
58
|
-
render json: @post, serializer: PostSerializer, adapter: :json_api,
|
59
|
-
key_transform: nil
|
60
|
-
end
|
61
|
-
|
62
|
-
def render_resource_with_transform_with_global_config
|
63
|
-
old_transform = ActiveModelSerializers.config.key_transform
|
64
|
-
setup_post
|
65
|
-
ActiveModelSerializers.config.key_transform = :camel_lower
|
66
|
-
render json: @post, serializer: PostSerializer, adapter: :json_api
|
67
|
-
ensure
|
68
|
-
ActiveModelSerializers.config.key_transform = old_transform
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
tests KeyTransformTestController
|
73
|
-
|
74
|
-
def test_render_resource_with_transform
|
75
|
-
get :render_resource_with_transform
|
76
|
-
response = JSON.parse(@response.body)
|
77
|
-
expected = {
|
78
|
-
'Data' => {
|
79
|
-
'Id' => '1337',
|
80
|
-
'Type' => 'Posts',
|
81
|
-
'Attributes' => {
|
82
|
-
'Title' => 'Title 1',
|
83
|
-
'Body' => 'Body 1',
|
84
|
-
'PublishAt' => '2020-03-16T03:55:25.291Z'
|
85
|
-
},
|
86
|
-
'Relationships' => {
|
87
|
-
'Author' => {
|
88
|
-
'Data' => {
|
89
|
-
'Id' => '1',
|
90
|
-
'Type' => 'Authors'
|
91
|
-
}
|
92
|
-
},
|
93
|
-
'TopComments' => {
|
94
|
-
'Data' => [
|
95
|
-
{ 'Id' => '7', 'Type' => 'TopComments' },
|
96
|
-
{ 'Id' => '12', 'Type' => 'TopComments' }
|
97
|
-
]
|
98
|
-
}
|
99
|
-
},
|
100
|
-
'Links' => {
|
101
|
-
'PostAuthors' => 'https://example.com/post_authors'
|
102
|
-
},
|
103
|
-
'Meta' => { 'Rating' => 5, 'FavoriteCount' => 10 }
|
104
|
-
}
|
105
|
-
}
|
106
|
-
assert_equal expected, response
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_render_resource_with_transform_nil
|
110
|
-
get :render_resource_with_transform_nil
|
111
|
-
response = JSON.parse(@response.body)
|
112
|
-
expected = {
|
113
|
-
'data' => {
|
114
|
-
'id' => '1337',
|
115
|
-
'type' => 'posts',
|
116
|
-
'attributes' => {
|
117
|
-
'title' => 'Title 1',
|
118
|
-
'body' => 'Body 1',
|
119
|
-
'publish-at' => '2020-03-16T03:55:25.291Z'
|
120
|
-
},
|
121
|
-
'relationships' => {
|
122
|
-
'author' => {
|
123
|
-
'data' => {
|
124
|
-
'id' => '1',
|
125
|
-
'type' => 'authors'
|
126
|
-
}
|
127
|
-
},
|
128
|
-
'top-comments' => {
|
129
|
-
'data' => [
|
130
|
-
{ 'id' => '7', 'type' => 'top-comments' },
|
131
|
-
{ 'id' => '12', 'type' => 'top-comments' }
|
132
|
-
]
|
133
|
-
}
|
134
|
-
},
|
135
|
-
'links' => {
|
136
|
-
'post-authors' => 'https://example.com/post_authors'
|
137
|
-
},
|
138
|
-
'meta' => { 'rating' => 5, 'favorite-count' => 10 }
|
139
|
-
}
|
140
|
-
}
|
141
|
-
assert_equal expected, response
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_render_resource_with_transform_with_global_config
|
145
|
-
get :render_resource_with_transform_with_global_config
|
146
|
-
response = JSON.parse(@response.body)
|
147
|
-
expected = {
|
148
|
-
'data' => {
|
149
|
-
'id' => '1337',
|
150
|
-
'type' => 'posts',
|
151
|
-
'attributes' => {
|
152
|
-
'title' => 'Title 1',
|
153
|
-
'body' => 'Body 1',
|
154
|
-
'publishAt' => '2020-03-16T03:55:25.291Z'
|
155
|
-
},
|
156
|
-
'relationships' => {
|
157
|
-
'author' => {
|
158
|
-
'data' => {
|
159
|
-
'id' => '1',
|
160
|
-
'type' => 'authors'
|
161
|
-
}
|
162
|
-
},
|
163
|
-
'topComments' => {
|
164
|
-
'data' => [
|
165
|
-
{ 'id' => '7', 'type' => 'topComments' },
|
166
|
-
{ 'id' => '12', 'type' => 'topComments' }
|
167
|
-
]
|
168
|
-
}
|
169
|
-
},
|
170
|
-
'links' => {
|
171
|
-
'postAuthors' => 'https://example.com/post_authors'
|
172
|
-
},
|
173
|
-
'meta' => { 'rating' => 5, 'favoriteCount' => 10 }
|
174
|
-
}
|
175
|
-
}
|
176
|
-
assert_equal expected, response
|
177
|
-
end
|
178
|
-
end
|
179
|
-
end
|
180
|
-
end
|
181
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActionController
|
4
|
-
module Serialization
|
5
|
-
class LookupProcTest < ActionController::TestCase
|
6
|
-
module Api
|
7
|
-
module V3
|
8
|
-
class PostCustomSerializer < ActiveModel::Serializer
|
9
|
-
attributes :title, :body
|
10
|
-
|
11
|
-
belongs_to :author
|
12
|
-
end
|
13
|
-
|
14
|
-
class AuthorCustomSerializer < ActiveModel::Serializer
|
15
|
-
attributes :name
|
16
|
-
end
|
17
|
-
|
18
|
-
class LookupProcTestController < ActionController::Base
|
19
|
-
def implicit_namespaced_serializer
|
20
|
-
author = Author.new(name: 'Bob')
|
21
|
-
post = Post.new(title: 'New Post', body: 'Body', author: author)
|
22
|
-
|
23
|
-
render json: post
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
tests Api::V3::LookupProcTestController
|
30
|
-
|
31
|
-
test 'implicitly uses namespaced serializer' do
|
32
|
-
controller_namespace = lambda do |resource_class, _parent_serializer_class, namespace|
|
33
|
-
"#{namespace}::#{resource_class}CustomSerializer" if namespace
|
34
|
-
end
|
35
|
-
|
36
|
-
with_prepended_lookup(controller_namespace) do
|
37
|
-
get :implicit_namespaced_serializer
|
38
|
-
|
39
|
-
assert_serializer Api::V3::PostCustomSerializer
|
40
|
-
|
41
|
-
expected = { 'title' => 'New Post', 'body' => 'Body', 'author' => { 'name' => 'Bob' } }
|
42
|
-
actual = JSON.parse(@response.body)
|
43
|
-
|
44
|
-
assert_equal expected, actual
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,226 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActionController
|
4
|
-
module Serialization
|
5
|
-
class NamespaceLookupTest < ActionController::TestCase
|
6
|
-
class Book < ::Model; end
|
7
|
-
class Page < ::Model; end
|
8
|
-
class Chapter < ::Model; end
|
9
|
-
class Writer < ::Model; end
|
10
|
-
|
11
|
-
module Api
|
12
|
-
module V2
|
13
|
-
class BookSerializer < ActiveModel::Serializer
|
14
|
-
attributes :title
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
module VHeader
|
19
|
-
class BookSerializer < ActiveModel::Serializer
|
20
|
-
attributes :title, :body
|
21
|
-
|
22
|
-
def body
|
23
|
-
'header'
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
module V3
|
29
|
-
class BookSerializer < ActiveModel::Serializer
|
30
|
-
attributes :title, :body
|
31
|
-
|
32
|
-
belongs_to :writer
|
33
|
-
has_many :chapters
|
34
|
-
end
|
35
|
-
|
36
|
-
class ChapterSerializer < ActiveModel::Serializer
|
37
|
-
attribute :title do
|
38
|
-
"Chapter - #{object.title}"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
class WriterSerializer < ActiveModel::Serializer
|
43
|
-
attributes :name
|
44
|
-
end
|
45
|
-
|
46
|
-
class LookupTestController < ActionController::Base
|
47
|
-
before_action only: [:namespace_set_in_before_filter] do
|
48
|
-
self.namespace_for_serializer = Api::V2
|
49
|
-
end
|
50
|
-
|
51
|
-
def implicit_namespaced_serializer
|
52
|
-
writer = Writer.new(name: 'Bob')
|
53
|
-
book = Book.new(title: 'New Post', body: 'Body', writer: writer, chapters: [])
|
54
|
-
|
55
|
-
render json: book
|
56
|
-
end
|
57
|
-
|
58
|
-
def implicit_namespaced_collection_serializer
|
59
|
-
chapter1 = Chapter.new(title: 'Oh')
|
60
|
-
chapter2 = Chapter.new(title: 'Oh my')
|
61
|
-
|
62
|
-
render json: [chapter1, chapter2]
|
63
|
-
end
|
64
|
-
|
65
|
-
def implicit_has_many_namespaced_serializer
|
66
|
-
chapter1 = Chapter.new(title: 'Odd World')
|
67
|
-
chapter2 = Chapter.new(title: 'New World')
|
68
|
-
book = Book.new(title: 'New Post', body: 'Body', chapters: [chapter1, chapter2])
|
69
|
-
|
70
|
-
render json: book
|
71
|
-
end
|
72
|
-
|
73
|
-
def explicit_namespace_as_module
|
74
|
-
book = Book.new(title: 'New Post', body: 'Body')
|
75
|
-
|
76
|
-
render json: book, namespace: Api::V2
|
77
|
-
end
|
78
|
-
|
79
|
-
def explicit_namespace_as_string
|
80
|
-
book = Book.new(title: 'New Post', body: 'Body')
|
81
|
-
|
82
|
-
# because this is a string, ruby can't auto-lookup the constant, so otherwise
|
83
|
-
# the looku things we mean ::Api::V2
|
84
|
-
render json: book, namespace: 'ActionController::Serialization::NamespaceLookupTest::Api::V2'
|
85
|
-
end
|
86
|
-
|
87
|
-
def explicit_namespace_as_symbol
|
88
|
-
book = Book.new(title: 'New Post', body: 'Body')
|
89
|
-
|
90
|
-
# because this is a string, ruby can't auto-lookup the constant, so otherwise
|
91
|
-
# the looku things we mean ::Api::V2
|
92
|
-
render json: book, namespace: :'ActionController::Serialization::NamespaceLookupTest::Api::V2'
|
93
|
-
end
|
94
|
-
|
95
|
-
def invalid_namespace
|
96
|
-
book = Book.new(title: 'New Post', body: 'Body')
|
97
|
-
|
98
|
-
render json: book, namespace: :api_v2
|
99
|
-
end
|
100
|
-
|
101
|
-
def namespace_set_in_before_filter
|
102
|
-
book = Book.new(title: 'New Post', body: 'Body')
|
103
|
-
render json: book
|
104
|
-
end
|
105
|
-
|
106
|
-
def namespace_set_by_request_headers
|
107
|
-
book = Book.new(title: 'New Post', body: 'Body')
|
108
|
-
version_from_header = request.headers['X-API_VERSION']
|
109
|
-
namespace = "ActionController::Serialization::NamespaceLookupTest::#{version_from_header}"
|
110
|
-
|
111
|
-
render json: book, namespace: namespace
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
tests Api::V3::LookupTestController
|
118
|
-
|
119
|
-
setup do
|
120
|
-
@test_namespace = self.class.parent
|
121
|
-
end
|
122
|
-
|
123
|
-
test 'uses request headers to determine the namespace' do
|
124
|
-
request.env['X-API_VERSION'] = 'Api::VHeader'
|
125
|
-
get :namespace_set_by_request_headers
|
126
|
-
|
127
|
-
assert_serializer Api::VHeader::BookSerializer
|
128
|
-
end
|
129
|
-
|
130
|
-
test 'implicitly uses namespaced serializer' do
|
131
|
-
get :implicit_namespaced_serializer
|
132
|
-
|
133
|
-
assert_serializer Api::V3::BookSerializer
|
134
|
-
|
135
|
-
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' }, 'chapters' => [] }
|
136
|
-
actual = JSON.parse(@response.body)
|
137
|
-
|
138
|
-
assert_equal expected, actual
|
139
|
-
end
|
140
|
-
|
141
|
-
test 'implicitly uses namespaced serializer for collection' do
|
142
|
-
get :implicit_namespaced_collection_serializer
|
143
|
-
|
144
|
-
assert_serializer 'ActiveModel::Serializer::CollectionSerializer'
|
145
|
-
|
146
|
-
expected = [{ 'title' => 'Chapter - Oh' }, { 'title' => 'Chapter - Oh my' }]
|
147
|
-
actual = JSON.parse(@response.body)
|
148
|
-
|
149
|
-
assert_equal expected, actual
|
150
|
-
end
|
151
|
-
|
152
|
-
test 'implicitly uses namespaced serializer for has_many' do
|
153
|
-
get :implicit_has_many_namespaced_serializer
|
154
|
-
|
155
|
-
assert_serializer Api::V3::BookSerializer
|
156
|
-
|
157
|
-
expected = {
|
158
|
-
'title' => 'New Post',
|
159
|
-
'body' => 'Body', 'writer' => nil,
|
160
|
-
'chapters' => [
|
161
|
-
{ 'title' => 'Chapter - Odd World' },
|
162
|
-
{ 'title' => 'Chapter - New World' }
|
163
|
-
]
|
164
|
-
}
|
165
|
-
actual = JSON.parse(@response.body)
|
166
|
-
|
167
|
-
assert_equal expected, actual
|
168
|
-
end
|
169
|
-
|
170
|
-
test 'explicit namespace as module' do
|
171
|
-
get :explicit_namespace_as_module
|
172
|
-
|
173
|
-
assert_serializer Api::V2::BookSerializer
|
174
|
-
|
175
|
-
expected = { 'title' => 'New Post' }
|
176
|
-
actual = JSON.parse(@response.body)
|
177
|
-
|
178
|
-
assert_equal expected, actual
|
179
|
-
end
|
180
|
-
|
181
|
-
test 'explicit namespace as string' do
|
182
|
-
get :explicit_namespace_as_string
|
183
|
-
|
184
|
-
assert_serializer Api::V2::BookSerializer
|
185
|
-
|
186
|
-
expected = { 'title' => 'New Post' }
|
187
|
-
actual = JSON.parse(@response.body)
|
188
|
-
|
189
|
-
assert_equal expected, actual
|
190
|
-
end
|
191
|
-
|
192
|
-
test 'explicit namespace as symbol' do
|
193
|
-
get :explicit_namespace_as_symbol
|
194
|
-
|
195
|
-
assert_serializer Api::V2::BookSerializer
|
196
|
-
|
197
|
-
expected = { 'title' => 'New Post' }
|
198
|
-
actual = JSON.parse(@response.body)
|
199
|
-
|
200
|
-
assert_equal expected, actual
|
201
|
-
end
|
202
|
-
|
203
|
-
test 'invalid namespace' do
|
204
|
-
get :invalid_namespace
|
205
|
-
|
206
|
-
assert_serializer ActiveModel::Serializer::Null
|
207
|
-
|
208
|
-
expected = { 'title' => 'New Post', 'body' => 'Body' }
|
209
|
-
actual = JSON.parse(@response.body)
|
210
|
-
|
211
|
-
assert_equal expected, actual
|
212
|
-
end
|
213
|
-
|
214
|
-
test 'namespace set in before filter' do
|
215
|
-
get :namespace_set_in_before_filter
|
216
|
-
|
217
|
-
assert_serializer Api::V2::BookSerializer
|
218
|
-
|
219
|
-
expected = { 'title' => 'New Post' }
|
220
|
-
actual = JSON.parse(@response.body)
|
221
|
-
|
222
|
-
assert_equal expected, actual
|
223
|
-
end
|
224
|
-
end
|
225
|
-
end
|
226
|
-
end
|