active_model_serializers 0.10.0 → 0.10.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +17 -5
- data/CHANGELOG.md +112 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +166 -26
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +21 -24
- data/appveyor.yml +9 -3
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +4 -2
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +44 -20
- data/docs/general/serializers.md +100 -11
- data/docs/howto/add_pagination_links.md +15 -16
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +4 -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 +64 -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 +19 -4
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +1 -1
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- 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 +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +76 -37
- 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 +1 -1
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/adapter/json_api.rb +44 -43
- 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 +108 -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 +15 -0
- 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/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/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 +19 -19
- 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 +183 -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 +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- 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 +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -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 +25 -12
- 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 +132 -36
- 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/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 +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +13 -0
- metadata +104 -38
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/include_tree.rb +0 -111
- 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/relationships_test.rb +0 -199
- 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/lint_test.rb
CHANGED
data/test/logger_test.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
3
|
+
module ActiveModelSerializers
|
4
|
+
class LoggerTest < ActiveSupport::TestCase
|
5
|
+
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
6
|
+
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
7
|
+
end
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
def test_logger_can_be_set
|
10
|
+
original_logger = ActiveModelSerializers.logger
|
11
|
+
logger = Logger.new(STDOUT)
|
11
12
|
|
12
|
-
|
13
|
+
ActiveModelSerializers.logger = logger
|
13
14
|
|
14
|
-
|
15
|
-
|
16
|
-
|
15
|
+
assert_equal ActiveModelSerializers.logger, logger
|
16
|
+
ensure
|
17
|
+
ActiveModelSerializers.logger = original_logger
|
18
|
+
end
|
17
19
|
end
|
18
20
|
end
|
@@ -3,7 +3,7 @@ require 'test_helper'
|
|
3
3
|
module ActiveModelSerializers
|
4
4
|
class SerializableResourceTest < ActiveSupport::TestCase
|
5
5
|
def setup
|
6
|
-
@resource = Profile.new(
|
6
|
+
@resource = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
7
7
|
@serializer = ProfileSerializer.new(@resource)
|
8
8
|
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
9
9
|
@serializable_resource = SerializableResource.new(@resource)
|
@@ -32,11 +32,11 @@ module ActiveModelSerializers
|
|
32
32
|
end
|
33
33
|
|
34
34
|
def test_use_adapter_with_adapter_option
|
35
|
-
assert SerializableResource.new(@resource,
|
35
|
+
assert SerializableResource.new(@resource, adapter: 'json').use_adapter?
|
36
36
|
end
|
37
37
|
|
38
38
|
def test_use_adapter_with_adapter_option_as_false
|
39
|
-
refute SerializableResource.new(@resource,
|
39
|
+
refute SerializableResource.new(@resource, adapter: false).use_adapter?
|
40
40
|
end
|
41
41
|
|
42
42
|
class SerializableResourceErrorsTest < Minitest::Test
|
@@ -45,15 +45,13 @@ module ActiveModelSerializers
|
|
45
45
|
resource = ModelWithErrors.new
|
46
46
|
resource.errors.add(:name, 'must be awesome')
|
47
47
|
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
48
|
-
resource,
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
{ :source => { :pointer => '/data/attributes/name' }, :detail => 'must be awesome' }
|
56
|
-
]
|
48
|
+
resource, serializer: ActiveModel::Serializer::ErrorSerializer,
|
49
|
+
adapter: :json_api
|
50
|
+
)
|
51
|
+
expected_response_document = {
|
52
|
+
errors: [
|
53
|
+
{ source: { pointer: '/data/attributes/name' }, detail: 'must be awesome' }
|
54
|
+
]
|
57
55
|
}
|
58
56
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
59
57
|
end
|
@@ -65,16 +63,14 @@ module ActiveModelSerializers
|
|
65
63
|
resource.errors.add(:title, 'must be amazing')
|
66
64
|
resources << ModelWithErrors.new
|
67
65
|
serializable_resource = SerializableResource.new(
|
68
|
-
resources,
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
{ :source => { :pointer => '/data/attributes/title' }, :detail => 'must be amazing' }
|
77
|
-
]
|
66
|
+
resources, serializer: ActiveModel::Serializer::ErrorsSerializer,
|
67
|
+
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
68
|
+
adapter: :json_api
|
69
|
+
)
|
70
|
+
expected_response_document = {
|
71
|
+
errors: [
|
72
|
+
{ source: { pointer: '/data/attributes/title' }, detail: 'must be amazing' }
|
73
|
+
]
|
78
74
|
}
|
79
75
|
assert_equal serializable_resource.as_json(options), expected_response_document
|
80
76
|
end
|
@@ -3,7 +3,8 @@ require 'test_helper'
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
5
|
class AssociationMacrosTest < ActiveSupport::TestCase
|
6
|
-
AuthorSummarySerializer
|
6
|
+
class AuthorSummarySerializer < ActiveModel::Serializer; end
|
7
|
+
|
7
8
|
class AssociationsTestSerializer < Serializer
|
8
9
|
belongs_to :author, serializer: AuthorSummarySerializer
|
9
10
|
has_many :comments
|
@@ -11,7 +12,7 @@ module ActiveModel
|
|
11
12
|
end
|
12
13
|
|
13
14
|
def before_setup
|
14
|
-
@reflections = AssociationsTestSerializer._reflections
|
15
|
+
@reflections = AssociationsTestSerializer._reflections.values
|
15
16
|
end
|
16
17
|
|
17
18
|
def test_has_one_defines_reflection
|
@@ -1,16 +1,19 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
|
3
2
|
module ActiveModel
|
4
3
|
class Serializer
|
5
4
|
class AssociationsTest < ActiveSupport::TestCase
|
5
|
+
class ModelWithoutSerializer < ::Model
|
6
|
+
attributes :id, :name
|
7
|
+
end
|
8
|
+
|
6
9
|
def setup
|
7
10
|
@author = Author.new(name: 'Steve K.')
|
8
11
|
@author.bio = nil
|
9
12
|
@author.roles = []
|
10
|
-
@blog = Blog.new(
|
11
|
-
@post = Post.new(
|
12
|
-
@tag =
|
13
|
-
@comment = Comment.new(
|
13
|
+
@blog = Blog.new(name: 'AMS Blog')
|
14
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
15
|
+
@tag = ModelWithoutSerializer.new(id: 'tagid', name: '#hashtagged')
|
16
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
14
17
|
@post.comments = [@comment]
|
15
18
|
@post.tags = [@tag]
|
16
19
|
@post.blog = @blog
|
@@ -19,7 +22,7 @@ module ActiveModel
|
|
19
22
|
@post.author = @author
|
20
23
|
@author.posts = [@post]
|
21
24
|
|
22
|
-
@post_serializer = PostSerializer.new(@post,
|
25
|
+
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
23
26
|
@author_serializer = AuthorSerializer.new(@author)
|
24
27
|
@comment_serializer = CommentSerializer.new(@comment)
|
25
28
|
end
|
@@ -32,13 +35,13 @@ module ActiveModel
|
|
32
35
|
|
33
36
|
case key
|
34
37
|
when :posts
|
35
|
-
assert_equal
|
38
|
+
assert_equal true, options.fetch(:include_data)
|
36
39
|
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
37
40
|
when :bio
|
38
|
-
assert_equal
|
41
|
+
assert_equal true, options.fetch(:include_data)
|
39
42
|
assert_nil serializer
|
40
43
|
when :roles
|
41
|
-
assert_equal
|
44
|
+
assert_equal true, options.fetch(:include_data)
|
42
45
|
assert_kind_of(ActiveModelSerializers.config.collection_serializer, serializer)
|
43
46
|
else
|
44
47
|
flunk "Unknown association: #{key}"
|
@@ -47,14 +50,18 @@ module ActiveModel
|
|
47
50
|
end
|
48
51
|
|
49
52
|
def test_has_many_with_no_serializer
|
50
|
-
|
53
|
+
post_serializer_class = Class.new(ActiveModel::Serializer) do
|
54
|
+
attributes :id
|
55
|
+
has_many :tags
|
56
|
+
end
|
57
|
+
post_serializer_class.new(@post).associations.each do |association|
|
51
58
|
key = association.key
|
52
59
|
serializer = association.serializer
|
53
60
|
options = association.options
|
54
61
|
|
55
62
|
assert_equal :tags, key
|
56
63
|
assert_nil serializer
|
57
|
-
assert_equal [{ name: '#hashtagged' }].to_json, options[:virtual_value].to_json
|
64
|
+
assert_equal [{ id: 'tagid', name: '#hashtagged' }].to_json, options[:virtual_value].to_json
|
58
65
|
end
|
59
66
|
end
|
60
67
|
|
@@ -63,7 +70,13 @@ module ActiveModel
|
|
63
70
|
.associations
|
64
71
|
.detect { |assoc| assoc.key == :comments }
|
65
72
|
|
66
|
-
|
73
|
+
comment_serializer = association.serializer.first
|
74
|
+
class << comment_serializer
|
75
|
+
def custom_options
|
76
|
+
instance_options
|
77
|
+
end
|
78
|
+
end
|
79
|
+
assert comment_serializer.custom_options.fetch(:custom_options)
|
67
80
|
end
|
68
81
|
|
69
82
|
def test_belongs_to
|
@@ -80,7 +93,7 @@ module ActiveModel
|
|
80
93
|
flunk "Unknown association: #{key}"
|
81
94
|
end
|
82
95
|
|
83
|
-
assert_equal
|
96
|
+
assert_equal true, association.options.fetch(:include_data)
|
84
97
|
end
|
85
98
|
end
|
86
99
|
|
@@ -104,13 +117,13 @@ module ActiveModel
|
|
104
117
|
end
|
105
118
|
|
106
119
|
assert(
|
107
|
-
PostSerializer._reflections.all? do |reflection|
|
108
|
-
inherited_klass._reflections.include?(reflection)
|
120
|
+
PostSerializer._reflections.values.all? do |reflection|
|
121
|
+
inherited_klass._reflections.values.include?(reflection)
|
109
122
|
end
|
110
123
|
)
|
111
124
|
|
112
125
|
assert(
|
113
|
-
inherited_klass._reflections.any? do |reflection|
|
126
|
+
inherited_klass._reflections.values.any? do |reflection|
|
114
127
|
reflection.name == :top_comments
|
115
128
|
end
|
116
129
|
)
|
@@ -143,12 +156,12 @@ module ActiveModel
|
|
143
156
|
)
|
144
157
|
actual = serializable(post, adapter: :attributes, serializer: InlineAssociationTestPostSerializer).as_json
|
145
158
|
expected = {
|
146
|
-
:
|
147
|
-
{ :
|
148
|
-
{ :
|
159
|
+
comments: [
|
160
|
+
{ id: 1, contents: 'first comment' },
|
161
|
+
{ id: 2, contents: 'last comment' }
|
149
162
|
],
|
150
|
-
:
|
151
|
-
{ :
|
163
|
+
last_comments: [
|
164
|
+
{ id: 2, contents: 'last comment' }
|
152
165
|
]
|
153
166
|
}
|
154
167
|
|
@@ -160,18 +173,20 @@ module ActiveModel
|
|
160
173
|
|
161
174
|
class NamespacedResourcesTest < ActiveSupport::TestCase
|
162
175
|
class ResourceNamespace
|
163
|
-
Post
|
164
|
-
|
165
|
-
|
166
|
-
|
176
|
+
class Post < ::Model
|
177
|
+
associations :comments, :author, :description
|
178
|
+
end
|
179
|
+
class Comment < ::Model; end
|
180
|
+
class Author < ::Model; end
|
181
|
+
class Description < ::Model; end
|
167
182
|
class PostSerializer < ActiveModel::Serializer
|
168
183
|
has_many :comments
|
169
184
|
belongs_to :author
|
170
185
|
has_one :description
|
171
186
|
end
|
172
|
-
CommentSerializer
|
173
|
-
AuthorSerializer
|
174
|
-
DescriptionSerializer
|
187
|
+
class CommentSerializer < ActiveModel::Serializer; end
|
188
|
+
class AuthorSerializer < ActiveModel::Serializer; end
|
189
|
+
class DescriptionSerializer < ActiveModel::Serializer; end
|
175
190
|
end
|
176
191
|
|
177
192
|
def setup
|
@@ -201,17 +216,19 @@ module ActiveModel
|
|
201
216
|
end
|
202
217
|
|
203
218
|
class NestedSerializersTest < ActiveSupport::TestCase
|
204
|
-
Post
|
205
|
-
|
206
|
-
|
207
|
-
|
219
|
+
class Post < ::Model
|
220
|
+
associations :comments, :author, :description
|
221
|
+
end
|
222
|
+
class Comment < ::Model; end
|
223
|
+
class Author < ::Model; end
|
224
|
+
class Description < ::Model; end
|
208
225
|
class PostSerializer < ActiveModel::Serializer
|
209
226
|
has_many :comments
|
210
|
-
CommentSerializer
|
227
|
+
class CommentSerializer < ActiveModel::Serializer; end
|
211
228
|
belongs_to :author
|
212
|
-
AuthorSerializer
|
229
|
+
class AuthorSerializer < ActiveModel::Serializer; end
|
213
230
|
has_one :description
|
214
|
-
DescriptionSerializer
|
231
|
+
class DescriptionSerializer < ActiveModel::Serializer; end
|
215
232
|
end
|
216
233
|
|
217
234
|
def setup
|
@@ -241,7 +258,10 @@ module ActiveModel
|
|
241
258
|
|
242
259
|
# rubocop:disable Metrics/AbcSize
|
243
260
|
def test_conditional_associations
|
244
|
-
model =
|
261
|
+
model = Class.new(::Model) do
|
262
|
+
attributes :true, :false
|
263
|
+
associations :association
|
264
|
+
end.new(true: true, false: false)
|
245
265
|
|
246
266
|
scenarios = [
|
247
267
|
{ options: { if: :true }, included: true },
|
@@ -290,6 +310,82 @@ module ActiveModel
|
|
290
310
|
assert_match(/:if should be a Symbol, String or Proc/, exception.message)
|
291
311
|
end
|
292
312
|
end
|
313
|
+
|
314
|
+
class InheritedSerializerTest < ActiveSupport::TestCase
|
315
|
+
class PostSerializer < ActiveModel::Serializer
|
316
|
+
belongs_to :author
|
317
|
+
has_many :comments
|
318
|
+
belongs_to :blog
|
319
|
+
end
|
320
|
+
|
321
|
+
class InheritedPostSerializer < PostSerializer
|
322
|
+
belongs_to :author, polymorphic: true
|
323
|
+
has_many :comments, key: :reviews
|
324
|
+
end
|
325
|
+
|
326
|
+
class AuthorSerializer < ActiveModel::Serializer
|
327
|
+
has_many :posts
|
328
|
+
has_many :roles
|
329
|
+
has_one :bio
|
330
|
+
end
|
331
|
+
|
332
|
+
class InheritedAuthorSerializer < AuthorSerializer
|
333
|
+
has_many :roles, polymorphic: true
|
334
|
+
has_one :bio, polymorphic: true
|
335
|
+
end
|
336
|
+
|
337
|
+
def setup
|
338
|
+
@author = Author.new(name: 'Steve K.')
|
339
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
340
|
+
@post_serializer = PostSerializer.new(@post)
|
341
|
+
@author_serializer = AuthorSerializer.new(@author)
|
342
|
+
@inherited_post_serializer = InheritedPostSerializer.new(@post)
|
343
|
+
@inherited_author_serializer = InheritedAuthorSerializer.new(@author)
|
344
|
+
@author_associations = @author_serializer.associations.to_a
|
345
|
+
@inherited_author_associations = @inherited_author_serializer.associations.to_a
|
346
|
+
@post_associations = @post_serializer.associations.to_a
|
347
|
+
@inherited_post_associations = @inherited_post_serializer.associations.to_a
|
348
|
+
end
|
349
|
+
|
350
|
+
test 'an author serializer must have [posts,roles,bio] associations' do
|
351
|
+
expected = [:posts, :roles, :bio].sort
|
352
|
+
result = @author_serializer.associations.map(&:name).sort
|
353
|
+
assert_equal(result, expected)
|
354
|
+
end
|
355
|
+
|
356
|
+
test 'a post serializer must have [author,comments,blog] associations' do
|
357
|
+
expected = [:author, :comments, :blog].sort
|
358
|
+
result = @post_serializer.associations.map(&:name).sort
|
359
|
+
assert_equal(result, expected)
|
360
|
+
end
|
361
|
+
|
362
|
+
test 'a serializer inheriting from another serializer can redefine has_many and has_one associations' do
|
363
|
+
expected = [:roles, :bio].sort
|
364
|
+
result = (@inherited_author_associations - @author_associations).map(&:name).sort
|
365
|
+
assert_equal(result, expected)
|
366
|
+
end
|
367
|
+
|
368
|
+
test 'a serializer inheriting from another serializer can redefine belongs_to associations' do
|
369
|
+
assert_equal [:author, :comments, :blog], @post_associations.map(&:name)
|
370
|
+
assert_equal [:author, :comments, :blog, :comments], @inherited_post_associations.map(&:name)
|
371
|
+
|
372
|
+
refute @post_associations.detect { |assoc| assoc.name == :author }.options.key?(:polymorphic)
|
373
|
+
assert_equal true, @inherited_post_associations.detect { |assoc| assoc.name == :author }.options.fetch(:polymorphic)
|
374
|
+
|
375
|
+
refute @post_associations.detect { |assoc| assoc.name == :comments }.options.key?(:key)
|
376
|
+
original_comment_assoc, new_comments_assoc = @inherited_post_associations.select { |assoc| assoc.name == :comments }
|
377
|
+
refute original_comment_assoc.options.key?(:key)
|
378
|
+
assert_equal :reviews, new_comments_assoc.options.fetch(:key)
|
379
|
+
|
380
|
+
assert_equal @post_associations.detect { |assoc| assoc.name == :blog }, @inherited_post_associations.detect { |assoc| assoc.name == :blog }
|
381
|
+
end
|
382
|
+
|
383
|
+
test 'a serializer inheriting from another serializer can have an additional association with the same name but with different key' do
|
384
|
+
expected = [:author, :comments, :blog, :reviews].sort
|
385
|
+
result = @inherited_post_serializer.associations.map { |a| a.options.fetch(:key, a.name) }.sort
|
386
|
+
assert_equal(result, expected)
|
387
|
+
end
|
388
|
+
end
|
293
389
|
end
|
294
390
|
end
|
295
391
|
end
|
@@ -22,7 +22,7 @@ module ActiveModel
|
|
22
22
|
inherited_klass = Class.new(AlternateBlogSerializer)
|
23
23
|
blog_serializer = inherited_klass.new(@blog)
|
24
24
|
adapter = ActiveModelSerializers::Adapter::Attributes.new(blog_serializer)
|
25
|
-
assert_equal({ :
|
25
|
+
assert_equal({ id: 1, title: 'AMS Hints' }, adapter.serializable_hash)
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_multiple_calls_with_the_same_attribute
|
@@ -81,7 +81,7 @@ module ActiveModel
|
|
81
81
|
assert_equal('custom', hash[:blog][:id])
|
82
82
|
end
|
83
83
|
|
84
|
-
PostWithVirtualAttribute
|
84
|
+
class PostWithVirtualAttribute < ::Model; attributes :first_name, :last_name end
|
85
85
|
class PostWithVirtualAttributeSerializer < ActiveModel::Serializer
|
86
86
|
attribute :name do
|
87
87
|
"#{object.first_name} #{object.last_name}"
|
@@ -98,7 +98,9 @@ module ActiveModel
|
|
98
98
|
|
99
99
|
# rubocop:disable Metrics/AbcSize
|
100
100
|
def test_conditional_associations
|
101
|
-
model =
|
101
|
+
model = Class.new(::Model) do
|
102
|
+
attributes :true, :false, :attribute
|
103
|
+
end.new(true: true, false: false)
|
102
104
|
|
103
105
|
scenarios = [
|
104
106
|
{ options: { if: :true }, included: true },
|
@@ -4,7 +4,7 @@ module ActiveModel
|
|
4
4
|
class Serializer
|
5
5
|
class AttributesTest < ActiveSupport::TestCase
|
6
6
|
def setup
|
7
|
-
@profile = Profile.new(
|
7
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
8
8
|
@profile_serializer = ProfileSerializer.new(@profile)
|
9
9
|
@comment = Comment.new(id: 1, body: 'ZOMG!!', date: '2015')
|
10
10
|
@serializer_klass = Class.new(CommentSerializer)
|
@@ -69,9 +69,9 @@ class CachingConfigurationTest < ActiveSupport::TestCase
|
|
69
69
|
end
|
70
70
|
|
71
71
|
test 'the non-cached serializer cache_store is nil' do
|
72
|
-
|
73
|
-
|
74
|
-
|
72
|
+
assert_nil @non_cached_serializer._cache
|
73
|
+
assert_nil @non_cached_serializer.cache_store
|
74
|
+
assert_nil @non_cached_serializer._cache
|
75
75
|
end
|
76
76
|
|
77
77
|
test 'the non-cached serializer does not have cache_enabled?' do
|
@@ -136,9 +136,9 @@ class CachingConfigurationTest < ActiveSupport::TestCase
|
|
136
136
|
end
|
137
137
|
|
138
138
|
test 'the non-cached serializer cache_store is nil' do
|
139
|
-
|
140
|
-
|
141
|
-
|
139
|
+
assert_nil @non_cached_serializer._cache
|
140
|
+
assert_nil @non_cached_serializer.cache_store
|
141
|
+
assert_nil @non_cached_serializer._cache
|
142
142
|
end
|
143
143
|
|
144
144
|
test 'the non-cached serializer does not have cache_enabled?' do
|
@@ -5,7 +5,7 @@ module ActiveModel
|
|
5
5
|
class FieldsetTest < ActiveSupport::TestCase
|
6
6
|
def test_fieldset_with_hash
|
7
7
|
fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
|
8
|
-
expected = { :
|
8
|
+
expected = { post: [:id, :title], comment: [:body] }
|
9
9
|
|
10
10
|
assert_equal(expected, fieldset.fields)
|
11
11
|
end
|
@@ -15,7 +15,8 @@ module ActiveModel
|
|
15
15
|
@blog,
|
16
16
|
adapter: :json,
|
17
17
|
serializer: AlternateBlogSerializer,
|
18
|
-
meta: { total: 10 }
|
18
|
+
meta: { total: 10 }
|
19
|
+
).as_json
|
19
20
|
expected = {
|
20
21
|
blog: {
|
21
22
|
id: 1,
|
@@ -65,7 +66,8 @@ module ActiveModel
|
|
65
66
|
@blog,
|
66
67
|
adapter: :attributes,
|
67
68
|
serializer: AlternateBlogSerializer,
|
68
|
-
meta: { total: 10 }
|
69
|
+
meta: { total: 10 }
|
70
|
+
).as_json
|
69
71
|
expected = {
|
70
72
|
id: 1,
|
71
73
|
title: 'AMS Hints'
|
@@ -79,7 +81,8 @@ module ActiveModel
|
|
79
81
|
adapter: :json,
|
80
82
|
serializer: AlternateBlogSerializer,
|
81
83
|
meta: { total: 10 },
|
82
|
-
meta_key: 'haha_meta'
|
84
|
+
meta_key: 'haha_meta'
|
85
|
+
).as_json
|
83
86
|
expected = {
|
84
87
|
blog: {
|
85
88
|
id: 1,
|
@@ -98,7 +101,8 @@ module ActiveModel
|
|
98
101
|
adapter: :json_api,
|
99
102
|
serializer: AlternateBlogSerializer,
|
100
103
|
meta: { total: 10 },
|
101
|
-
meta_key: 'haha_meta'
|
104
|
+
meta_key: 'haha_meta'
|
105
|
+
).as_json
|
102
106
|
expected = {
|
103
107
|
data: {
|
104
108
|
id: '1',
|
@@ -148,7 +152,8 @@ module ActiveModel
|
|
148
152
|
actual = ActiveModelSerializers::SerializableResource.new(
|
149
153
|
[@blog],
|
150
154
|
adapter: :attributes,
|
151
|
-
meta: { total: 10 }
|
155
|
+
meta: { total: 10 }
|
156
|
+
).as_json
|
152
157
|
expected = [{
|
153
158
|
id: 1,
|
154
159
|
name: 'AMS Hints',
|
@@ -170,7 +175,8 @@ module ActiveModel
|
|
170
175
|
[@blog],
|
171
176
|
adapter: :json,
|
172
177
|
meta: { total: 10 },
|
173
|
-
meta_key: 'haha_meta'
|
178
|
+
meta_key: 'haha_meta'
|
179
|
+
).as_json
|
174
180
|
expected = {
|
175
181
|
blogs: [{
|
176
182
|
id: 1,
|
@@ -3,18 +3,29 @@ require 'test_helper'
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
5
|
class OptionsTest < ActiveSupport::TestCase
|
6
|
-
|
7
|
-
|
6
|
+
class ModelWithOptions < ActiveModelSerializers::Model
|
7
|
+
attributes :name, :description
|
8
|
+
end
|
9
|
+
class ModelWithOptionsSerializer < ActiveModel::Serializer
|
10
|
+
attributes :name, :description
|
11
|
+
|
12
|
+
def arguments_passed_in?
|
13
|
+
instance_options[:my_options] == :accessible
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
setup do
|
18
|
+
@model_with_options = ModelWithOptions.new(name: 'Name 1', description: 'Description 1')
|
8
19
|
end
|
9
20
|
|
10
21
|
def test_options_are_accessible
|
11
|
-
|
12
|
-
assert
|
22
|
+
model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options, my_options: :accessible)
|
23
|
+
assert model_with_options_serializer.arguments_passed_in?
|
13
24
|
end
|
14
25
|
|
15
26
|
def test_no_option_is_passed_in
|
16
|
-
|
17
|
-
refute
|
27
|
+
model_with_options_serializer = ModelWithOptionsSerializer.new(@model_with_options)
|
28
|
+
refute model_with_options_serializer.arguments_passed_in?
|
18
29
|
end
|
19
30
|
end
|
20
31
|
end
|
@@ -5,10 +5,10 @@ module ActiveModel
|
|
5
5
|
class ReadAttributeForSerializationTest < ActiveSupport::TestCase
|
6
6
|
# https://github.com/rails-api/active_model_serializers/issues/1653
|
7
7
|
class Parent < ActiveModelSerializers::Model
|
8
|
-
|
8
|
+
attributes :id
|
9
9
|
end
|
10
10
|
class Child < Parent
|
11
|
-
|
11
|
+
attributes :name
|
12
12
|
end
|
13
13
|
class ParentSerializer < ActiveModel::Serializer
|
14
14
|
attributes :$id
|
@@ -30,7 +30,7 @@ module ActiveModel
|
|
30
30
|
|
31
31
|
# https://github.com/rails-api/active_model_serializers/issues/1658
|
32
32
|
class ErrorResponse < ActiveModelSerializers::Model
|
33
|
-
|
33
|
+
attributes :error
|
34
34
|
end
|
35
35
|
class ApplicationSerializer < ActiveModel::Serializer
|
36
36
|
attributes :status
|
@@ -2,10 +2,10 @@ module ActiveModel
|
|
2
2
|
class Serializer
|
3
3
|
class SerializationTest < ActiveSupport::TestCase
|
4
4
|
class Blog < ActiveModelSerializers::Model
|
5
|
-
|
5
|
+
attributes :id, :name, :authors
|
6
6
|
end
|
7
7
|
class Author < ActiveModelSerializers::Model
|
8
|
-
|
8
|
+
attributes :id, :name
|
9
9
|
end
|
10
10
|
class BlogSerializer < ActiveModel::Serializer
|
11
11
|
attributes :id
|