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,370 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
module ActiveModel
|
3
|
-
class Serializer
|
4
|
-
class AssociationsTest < ActiveSupport::TestCase
|
5
|
-
def setup
|
6
|
-
@author = Author.new(name: 'Steve K.')
|
7
|
-
@author.bio = nil
|
8
|
-
@author.roles = []
|
9
|
-
@blog = Blog.new(name: 'AMS Blog')
|
10
|
-
@post = Post.new(title: 'New Post', body: 'Body')
|
11
|
-
@tag = Tag.new(name: '#hashtagged')
|
12
|
-
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
13
|
-
@post.comments = [@comment]
|
14
|
-
@post.tags = [@tag]
|
15
|
-
@post.blog = @blog
|
16
|
-
@comment.post = @post
|
17
|
-
@comment.author = nil
|
18
|
-
@post.author = @author
|
19
|
-
@author.posts = [@post]
|
20
|
-
|
21
|
-
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
22
|
-
@author_serializer = AuthorSerializer.new(@author)
|
23
|
-
@comment_serializer = CommentSerializer.new(@comment)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_has_many_and_has_one
|
27
|
-
@author_serializer.associations.each do |association|
|
28
|
-
key = association.key
|
29
|
-
serializer = association.serializer
|
30
|
-
options = association.options
|
31
|
-
|
32
|
-
case key
|
33
|
-
when :posts
|
34
|
-
assert_equal true, options.fetch(:include_data)
|
35
|
-
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
36
|
-
when :bio
|
37
|
-
assert_equal true, options.fetch(:include_data)
|
38
|
-
assert_nil serializer
|
39
|
-
when :roles
|
40
|
-
assert_equal true, options.fetch(:include_data)
|
41
|
-
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
42
|
-
else
|
43
|
-
flunk "Unknown association: #{key}"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_has_many_with_no_serializer
|
49
|
-
PostWithTagsSerializer.new(@post).associations.each do |association|
|
50
|
-
key = association.key
|
51
|
-
serializer = association.serializer
|
52
|
-
options = association.options
|
53
|
-
|
54
|
-
assert_equal :tags, key
|
55
|
-
assert_nil serializer
|
56
|
-
assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_serializer_options_are_passed_into_associations_serializers
|
61
|
-
association = @post_serializer
|
62
|
-
.associations
|
63
|
-
.detect { |assoc| assoc.key == :comments }
|
64
|
-
|
65
|
-
assert association.serializer.first.custom_options[:custom_options]
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_belongs_to
|
69
|
-
@comment_serializer.associations.each do |association|
|
70
|
-
key = association.key
|
71
|
-
serializer = association.serializer
|
72
|
-
|
73
|
-
case key
|
74
|
-
when :post
|
75
|
-
assert_kind_of(PostSerializer, serializer)
|
76
|
-
when :author
|
77
|
-
assert_nil serializer
|
78
|
-
else
|
79
|
-
flunk "Unknown association: #{key}"
|
80
|
-
end
|
81
|
-
|
82
|
-
assert_equal true, association.options.fetch(:include_data)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_belongs_to_with_custom_method
|
87
|
-
assert(
|
88
|
-
@post_serializer.associations.any? do |association|
|
89
|
-
association.key == :blog
|
90
|
-
end
|
91
|
-
)
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_associations_inheritance
|
95
|
-
inherited_klass = Class.new(PostSerializer)
|
96
|
-
|
97
|
-
assert_equal(PostSerializer._reflections, inherited_klass._reflections)
|
98
|
-
end
|
99
|
-
|
100
|
-
def test_associations_inheritance_with_new_association
|
101
|
-
inherited_klass = Class.new(PostSerializer) do
|
102
|
-
has_many :top_comments, serializer: CommentSerializer
|
103
|
-
end
|
104
|
-
|
105
|
-
assert(
|
106
|
-
PostSerializer._reflections.values.all? do |reflection|
|
107
|
-
inherited_klass._reflections.values.include?(reflection)
|
108
|
-
end
|
109
|
-
)
|
110
|
-
|
111
|
-
assert(
|
112
|
-
inherited_klass._reflections.values.any? do |reflection|
|
113
|
-
reflection.name == :top_comments
|
114
|
-
end
|
115
|
-
)
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_associations_custom_keys
|
119
|
-
serializer = PostWithCustomKeysSerializer.new(@post)
|
120
|
-
|
121
|
-
expected_association_keys = serializer.associations.map(&:key)
|
122
|
-
|
123
|
-
assert expected_association_keys.include? :reviews
|
124
|
-
assert expected_association_keys.include? :writer
|
125
|
-
assert expected_association_keys.include? :site
|
126
|
-
end
|
127
|
-
|
128
|
-
class InlineAssociationTestPostSerializer < ActiveModel::Serializer
|
129
|
-
has_many :comments
|
130
|
-
has_many :comments, key: :last_comments do
|
131
|
-
object.comments.last(1)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_virtual_attribute_block
|
136
|
-
comment1 = ::ARModels::Comment.create!(contents: 'first comment')
|
137
|
-
comment2 = ::ARModels::Comment.create!(contents: 'last comment')
|
138
|
-
post = ::ARModels::Post.create!(
|
139
|
-
title: 'inline association test',
|
140
|
-
body: 'etc',
|
141
|
-
comments: [comment1, comment2]
|
142
|
-
)
|
143
|
-
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
144
|
-
expected = {
|
145
|
-
comments: [
|
146
|
-
{ id: 1, contents: 'first comment' },
|
147
|
-
{ id: 2, contents: 'last comment' }
|
148
|
-
],
|
149
|
-
last_comments: [
|
150
|
-
{ id: 2, contents: 'last comment' }
|
151
|
-
]
|
152
|
-
}
|
153
|
-
|
154
|
-
assert_equal expected, actual
|
155
|
-
ensure
|
156
|
-
::ARModels::Post.delete_all
|
157
|
-
::ARModels::Comment.delete_all
|
158
|
-
end
|
159
|
-
|
160
|
-
class NamespacedResourcesTest < ActiveSupport::TestCase
|
161
|
-
class ResourceNamespace
|
162
|
-
class Post < ::Model; end
|
163
|
-
class Comment < ::Model; end
|
164
|
-
class Author < ::Model; end
|
165
|
-
class Description < ::Model; end
|
166
|
-
class PostSerializer < ActiveModel::Serializer
|
167
|
-
has_many :comments
|
168
|
-
belongs_to :author
|
169
|
-
has_one :description
|
170
|
-
end
|
171
|
-
class CommentSerializer < ActiveModel::Serializer; end
|
172
|
-
class AuthorSerializer < ActiveModel::Serializer; end
|
173
|
-
class DescriptionSerializer < ActiveModel::Serializer; end
|
174
|
-
end
|
175
|
-
|
176
|
-
def setup
|
177
|
-
@comment = ResourceNamespace::Comment.new
|
178
|
-
@author = ResourceNamespace::Author.new
|
179
|
-
@description = ResourceNamespace::Description.new
|
180
|
-
@post = ResourceNamespace::Post.new(comments: [@comment],
|
181
|
-
author: @author,
|
182
|
-
description: @description)
|
183
|
-
@post_serializer = ResourceNamespace::PostSerializer.new(@post)
|
184
|
-
end
|
185
|
-
|
186
|
-
def test_associations_namespaced_resources
|
187
|
-
@post_serializer.associations.each do |association|
|
188
|
-
case association.key
|
189
|
-
when :comments
|
190
|
-
assert_instance_of(ResourceNamespace::CommentSerializer, association.serializer.first)
|
191
|
-
when :author
|
192
|
-
assert_instance_of(ResourceNamespace::AuthorSerializer, association.serializer)
|
193
|
-
when :description
|
194
|
-
assert_instance_of(ResourceNamespace::DescriptionSerializer, association.serializer)
|
195
|
-
else
|
196
|
-
flunk "Unknown association: #{key}"
|
197
|
-
end
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
class NestedSerializersTest < ActiveSupport::TestCase
|
203
|
-
class Post < ::Model; end
|
204
|
-
class Comment < ::Model; end
|
205
|
-
class Author < ::Model; end
|
206
|
-
class Description < ::Model; end
|
207
|
-
class PostSerializer < ActiveModel::Serializer
|
208
|
-
has_many :comments
|
209
|
-
class CommentSerializer < ActiveModel::Serializer; end
|
210
|
-
belongs_to :author
|
211
|
-
class AuthorSerializer < ActiveModel::Serializer; end
|
212
|
-
has_one :description
|
213
|
-
class DescriptionSerializer < ActiveModel::Serializer; end
|
214
|
-
end
|
215
|
-
|
216
|
-
def setup
|
217
|
-
@comment = Comment.new
|
218
|
-
@author = Author.new
|
219
|
-
@description = Description.new
|
220
|
-
@post = Post.new(comments: [@comment],
|
221
|
-
author: @author,
|
222
|
-
description: @description)
|
223
|
-
@post_serializer = PostSerializer.new(@post)
|
224
|
-
end
|
225
|
-
|
226
|
-
def test_associations_namespaced_resources
|
227
|
-
@post_serializer.associations.each do |association|
|
228
|
-
case association.key
|
229
|
-
when :comments
|
230
|
-
assert_instance_of(PostSerializer::CommentSerializer, association.serializer.first)
|
231
|
-
when :author
|
232
|
-
assert_instance_of(PostSerializer::AuthorSerializer, association.serializer)
|
233
|
-
when :description
|
234
|
-
assert_instance_of(PostSerializer::DescriptionSerializer, association.serializer)
|
235
|
-
else
|
236
|
-
flunk "Unknown association: #{key}"
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
|
241
|
-
# rubocop:disable Metrics/AbcSize
|
242
|
-
def test_conditional_associations
|
243
|
-
model = ::Model.new(true: true, false: false)
|
244
|
-
|
245
|
-
scenarios = [
|
246
|
-
{ options: { if: :true }, included: true },
|
247
|
-
{ options: { if: :false }, included: false },
|
248
|
-
{ options: { unless: :false }, included: true },
|
249
|
-
{ options: { unless: :true }, included: false },
|
250
|
-
{ options: { if: 'object.true' }, included: true },
|
251
|
-
{ options: { if: 'object.false' }, included: false },
|
252
|
-
{ options: { unless: 'object.false' }, included: true },
|
253
|
-
{ options: { unless: 'object.true' }, included: false },
|
254
|
-
{ options: { if: -> { object.true } }, included: true },
|
255
|
-
{ options: { if: -> { object.false } }, included: false },
|
256
|
-
{ options: { unless: -> { object.false } }, included: true },
|
257
|
-
{ options: { unless: -> { object.true } }, included: false },
|
258
|
-
{ options: { if: -> (s) { s.object.true } }, included: true },
|
259
|
-
{ options: { if: -> (s) { s.object.false } }, included: false },
|
260
|
-
{ options: { unless: -> (s) { s.object.false } }, included: true },
|
261
|
-
{ options: { unless: -> (s) { s.object.true } }, included: false }
|
262
|
-
]
|
263
|
-
|
264
|
-
scenarios.each do |s|
|
265
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
266
|
-
belongs_to :association, s[:options]
|
267
|
-
|
268
|
-
def true
|
269
|
-
true
|
270
|
-
end
|
271
|
-
|
272
|
-
def false
|
273
|
-
false
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
hash = serializable(model, serializer: serializer).serializable_hash
|
278
|
-
assert_equal(s[:included], hash.key?(:association), "Error with #{s[:options]}")
|
279
|
-
end
|
280
|
-
end
|
281
|
-
|
282
|
-
def test_illegal_conditional_associations
|
283
|
-
exception = assert_raises(TypeError) do
|
284
|
-
Class.new(ActiveModel::Serializer) do
|
285
|
-
belongs_to :x, if: nil
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
assert_match(/:if should be a Symbol, String or Proc/, exception.message)
|
290
|
-
end
|
291
|
-
end
|
292
|
-
|
293
|
-
class InheritedSerializerTest < ActiveSupport::TestCase
|
294
|
-
class PostSerializer < ActiveModel::Serializer
|
295
|
-
belongs_to :author
|
296
|
-
has_many :comments
|
297
|
-
belongs_to :blog
|
298
|
-
end
|
299
|
-
|
300
|
-
class InheritedPostSerializer < PostSerializer
|
301
|
-
belongs_to :author, polymorphic: true
|
302
|
-
has_many :comments, key: :reviews
|
303
|
-
end
|
304
|
-
|
305
|
-
class AuthorSerializer < ActiveModel::Serializer
|
306
|
-
has_many :posts
|
307
|
-
has_many :roles
|
308
|
-
has_one :bio
|
309
|
-
end
|
310
|
-
|
311
|
-
class InheritedAuthorSerializer < AuthorSerializer
|
312
|
-
has_many :roles, polymorphic: true
|
313
|
-
has_one :bio, polymorphic: true
|
314
|
-
end
|
315
|
-
|
316
|
-
def setup
|
317
|
-
@author = Author.new(name: 'Steve K.')
|
318
|
-
@post = Post.new(title: 'New Post', body: 'Body')
|
319
|
-
@post_serializer = PostSerializer.new(@post)
|
320
|
-
@author_serializer = AuthorSerializer.new(@author)
|
321
|
-
@inherited_post_serializer = InheritedPostSerializer.new(@post)
|
322
|
-
@inherited_author_serializer = InheritedAuthorSerializer.new(@author)
|
323
|
-
@author_associations = @author_serializer.associations.to_a
|
324
|
-
@inherited_author_associations = @inherited_author_serializer.associations.to_a
|
325
|
-
@post_associations = @post_serializer.associations.to_a
|
326
|
-
@inherited_post_associations = @inherited_post_serializer.associations.to_a
|
327
|
-
end
|
328
|
-
|
329
|
-
test 'an author serializer must have [posts,roles,bio] associations' do
|
330
|
-
expected = [:posts, :roles, :bio].sort
|
331
|
-
result = @author_serializer.associations.map(&:name).sort
|
332
|
-
assert_equal(result, expected)
|
333
|
-
end
|
334
|
-
|
335
|
-
test 'a post serializer must have [author,comments,blog] associations' do
|
336
|
-
expected = [:author, :comments, :blog].sort
|
337
|
-
result = @post_serializer.associations.map(&:name).sort
|
338
|
-
assert_equal(result, expected)
|
339
|
-
end
|
340
|
-
|
341
|
-
test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
|
342
|
-
expected = [:roles, :bio].sort
|
343
|
-
result = (@inherited_author_associations - @author_associations).map(&:name).sort
|
344
|
-
assert_equal(result, expected)
|
345
|
-
end
|
346
|
-
|
347
|
-
test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
|
348
|
-
assert_equal [:author, :comments, :blog], @post_associations.map(&:name)
|
349
|
-
assert_equal [:author, :comments, :blog, :comments], @inherited_post_associations.map(&:name)
|
350
|
-
|
351
|
-
refute @post_associations.detect { |assoc| assoc.name == :author }.options.key?(:polymorphic)
|
352
|
-
assert_equal true, @inherited_post_associations.detect { |assoc| assoc.name == :author }.options.fetch(:polymorphic)
|
353
|
-
|
354
|
-
refute @post_associations.detect { |assoc| assoc.name == :comments }.options.key?(:key)
|
355
|
-
original_comment_assoc, new_comments_assoc = @inherited_post_associations.select { |assoc| assoc.name == :comments }
|
356
|
-
refute original_comment_assoc.options.key?(:key)
|
357
|
-
assert_equal :reviews, new_comments_assoc.options.fetch(:key)
|
358
|
-
|
359
|
-
assert_equal @post_associations.detect { |assoc| assoc.name == :blog }, @inherited_post_associations.detect { |assoc| assoc.name == :blog }
|
360
|
-
end
|
361
|
-
|
362
|
-
test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
|
363
|
-
expected = [:author, :comments, :blog, :reviews].sort
|
364
|
-
result = @inherited_post_serializer.associations.map { |a| a.options.fetch(:key, a.name) }.sort
|
365
|
-
assert_equal(result, expected)
|
366
|
-
end
|
367
|
-
end
|
368
|
-
end
|
369
|
-
end
|
370
|
-
end
|
@@ -1,151 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class AttributeTest < ActiveSupport::TestCase
|
6
|
-
def setup
|
7
|
-
@blog = Blog.new(id: 1, name: 'AMS Hints', type: 'stuff')
|
8
|
-
@blog_serializer = AlternateBlogSerializer.new(@blog)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_attributes_definition
|
12
|
-
assert_equal([:id, :title],
|
13
|
-
@blog_serializer.class._attributes)
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_json_serializable_hash
|
17
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(@blog_serializer)
|
18
|
-
assert_equal({ blog: { id: 1, title: 'AMS Hints' } }, adapter.serializable_hash)
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_attribute_inheritance_with_key
|
22
|
-
inherited_klass = Class.new(AlternateBlogSerializer)
|
23
|
-
blog_serializer = inherited_klass.new(@blog)
|
24
|
-
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
25
|
-
assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_multiple_calls_with_the_same_attribute
|
29
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
30
|
-
attribute :title
|
31
|
-
attribute :title
|
32
|
-
end
|
33
|
-
|
34
|
-
assert_equal([:title], serializer_class._attributes)
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_id_attribute_override
|
38
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
39
|
-
attribute :name, key: :id
|
40
|
-
end
|
41
|
-
|
42
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
|
43
|
-
assert_equal({ blog: { id: 'AMS Hints' } }, adapter.serializable_hash)
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_object_attribute_override
|
47
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
48
|
-
attribute :name, key: :object
|
49
|
-
end
|
50
|
-
|
51
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(serializer.new(@blog))
|
52
|
-
assert_equal({ blog: { object: 'AMS Hints' } }, adapter.serializable_hash)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_type_attribute
|
56
|
-
attribute_serializer = Class.new(ActiveModel::Serializer) do
|
57
|
-
attribute :id, key: :type
|
58
|
-
end
|
59
|
-
attributes_serializer = Class.new(ActiveModel::Serializer) do
|
60
|
-
attributes :type
|
61
|
-
end
|
62
|
-
|
63
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(attribute_serializer.new(@blog))
|
64
|
-
assert_equal({ blog: { type: 1 } }, adapter.serializable_hash)
|
65
|
-
|
66
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(attributes_serializer.new(@blog))
|
67
|
-
assert_equal({ blog: { type: 'stuff' } }, adapter.serializable_hash)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_id_attribute_override_before
|
71
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
72
|
-
def id
|
73
|
-
'custom'
|
74
|
-
end
|
75
|
-
|
76
|
-
attribute :id
|
77
|
-
end
|
78
|
-
|
79
|
-
hash = ActiveModelSerializers::SerializableResource.new(@blog, adapter: :json, serializer: serializer).serializable_hash
|
80
|
-
|
81
|
-
assert_equal('custom', hash[:blog][:id])
|
82
|
-
end
|
83
|
-
|
84
|
-
class PostWithVirtualAttribute < ::Model; end
|
85
|
-
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
|
86
|
-
attribute :name do
|
87
|
-
"#{object.first_name} #{object.last_name}"
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_virtual_attribute_block
|
92
|
-
post = PostWithVirtualAttribute.new(first_name: 'Lucas', last_name: 'Hosseini')
|
93
|
-
hash = serializable(post).serializable_hash
|
94
|
-
expected = { name: 'Lucas Hosseini' }
|
95
|
-
|
96
|
-
assert_equal(expected, hash)
|
97
|
-
end
|
98
|
-
|
99
|
-
# rubocop:disable Metrics/AbcSize
|
100
|
-
def test_conditional_associations
|
101
|
-
model = ::Model.new(true: true, false: false)
|
102
|
-
|
103
|
-
scenarios = [
|
104
|
-
{ options: { if: :true }, included: true },
|
105
|
-
{ options: { if: :false }, included: false },
|
106
|
-
{ options: { unless: :false }, included: true },
|
107
|
-
{ options: { unless: :true }, included: false },
|
108
|
-
{ options: { if: 'object.true' }, included: true },
|
109
|
-
{ options: { if: 'object.false' }, included: false },
|
110
|
-
{ options: { unless: 'object.false' }, included: true },
|
111
|
-
{ options: { unless: 'object.true' }, included: false },
|
112
|
-
{ options: { if: -> { object.true } }, included: true },
|
113
|
-
{ options: { if: -> { object.false } }, included: false },
|
114
|
-
{ options: { unless: -> { object.false } }, included: true },
|
115
|
-
{ options: { unless: -> { object.true } }, included: false },
|
116
|
-
{ options: { if: -> (s) { s.object.true } }, included: true },
|
117
|
-
{ options: { if: -> (s) { s.object.false } }, included: false },
|
118
|
-
{ options: { unless: -> (s) { s.object.false } }, included: true },
|
119
|
-
{ options: { unless: -> (s) { s.object.true } }, included: false }
|
120
|
-
]
|
121
|
-
|
122
|
-
scenarios.each do |s|
|
123
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
124
|
-
attribute :attribute, s[:options]
|
125
|
-
|
126
|
-
def true
|
127
|
-
true
|
128
|
-
end
|
129
|
-
|
130
|
-
def false
|
131
|
-
false
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
hash = serializable(model, serializer: serializer).serializable_hash
|
136
|
-
assert_equal(s[:included], hash.key?(:attribute), "Error with #{s[:options]}")
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_illegal_conditional_attributes
|
141
|
-
exception = assert_raises(TypeError) do
|
142
|
-
Class.new(ActiveModel::Serializer) do
|
143
|
-
attribute :x, if: nil
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
assert_match(/:if should be a Symbol, String or Proc/, exception.message)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
151
|
-
end
|