active_model_serializers 0.8.3 → 0.10.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/.rubocop.yml +86 -0
- data/.rubocop_todo.yml +240 -0
- data/.simplecov +111 -0
- data/.travis.yml +33 -22
- data/CHANGELOG.md +358 -6
- data/CONTRIBUTING.md +220 -0
- data/Gemfile +46 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +81 -591
- data/Rakefile +68 -11
- data/active_model_serializers.gemspec +57 -23
- data/appveyor.yml +27 -0
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +35 -0
- data/docs/general/adapters.md +162 -0
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +27 -0
- data/docs/general/getting_started.md +98 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +153 -0
- data/docs/general/serializers.md +207 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +121 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +58 -0
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/jsonapi/schema.md +140 -0
- data/lib/action_controller/serialization.rb +41 -37
- data/lib/active_model/serializable_resource.rb +72 -0
- data/lib/active_model/serializer/adapter/attributes.rb +66 -0
- data/lib/active_model/serializer/adapter/base.rb +58 -0
- data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +111 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
- data/lib/active_model/serializer/adapter/json_api.rb +223 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +91 -0
- data/lib/active_model/serializer/array_serializer.rb +9 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +87 -220
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +28 -0
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +33 -0
- data/lib/active_model/serializer/lint.rb +142 -0
- data/lib/active_model/serializer/reflection.rb +91 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +99 -479
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/logging.rb +119 -0
- data/lib/active_model_serializers/model.rb +39 -0
- data/lib/active_model_serializers/railtie.rb +38 -0
- data/lib/active_model_serializers/serialization_context.rb +10 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +20 -92
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +59 -0
- data/test/action_controller/json_api/linked_test.rb +196 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
- data/test/action_controller/serialization_test.rb +435 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
- data/test/active_model_serializers/serialization_context_test.rb +18 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +38 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +92 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +97 -0
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +394 -0
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +71 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +36 -73
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +229 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +57 -0
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +40 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +166 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +267 -0
- data/test/serializers/attribute_test.rb +123 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/cache_test.rb +209 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +130 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +134 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +77 -0
- data/test/support/rails5_shims.rb +29 -0
- data/test/support/rails_app.rb +25 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +53 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +19 -0
- data/test/test_helper.rb +55 -24
- metadata +358 -42
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,139 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
module Deserialization
|
7
|
+
class ParseTest < Minitest::Test
|
8
|
+
def setup
|
9
|
+
@hash = {
|
10
|
+
'data' => {
|
11
|
+
'type' => 'photos',
|
12
|
+
'id' => 'zorglub',
|
13
|
+
'attributes' => {
|
14
|
+
'title' => 'Ember Hamster',
|
15
|
+
'src' => 'http://example.com/images/productivity.png'
|
16
|
+
},
|
17
|
+
'relationships' => {
|
18
|
+
'author' => {
|
19
|
+
'data' => nil
|
20
|
+
},
|
21
|
+
'photographer' => {
|
22
|
+
'data' => { 'type' => 'people', 'id' => '9' }
|
23
|
+
},
|
24
|
+
'comments' => {
|
25
|
+
'data' => [
|
26
|
+
{ 'type' => 'comments', 'id' => '1' },
|
27
|
+
{ 'type' => 'comments', 'id' => '2' }
|
28
|
+
]
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
32
|
+
}
|
33
|
+
@params = ActionController::Parameters.new(@hash)
|
34
|
+
@expected = {
|
35
|
+
id: 'zorglub',
|
36
|
+
title: 'Ember Hamster',
|
37
|
+
src: 'http://example.com/images/productivity.png',
|
38
|
+
author_id: nil,
|
39
|
+
photographer_id: '9',
|
40
|
+
comment_ids: %w(1 2)
|
41
|
+
}
|
42
|
+
|
43
|
+
@illformed_payloads = [nil,
|
44
|
+
{},
|
45
|
+
{
|
46
|
+
'data' => nil
|
47
|
+
}, {
|
48
|
+
'data' => { 'attributes' => [] }
|
49
|
+
}, {
|
50
|
+
'data' => { 'relationships' => [] }
|
51
|
+
}, {
|
52
|
+
'data' => {
|
53
|
+
'relationships' => { 'rel' => nil }
|
54
|
+
}
|
55
|
+
}, {
|
56
|
+
'data' => {
|
57
|
+
'relationships' => { 'rel' => {} }
|
58
|
+
}
|
59
|
+
}]
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_hash
|
63
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash)
|
64
|
+
assert_equal(@expected, parsed_hash)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_actioncontroller_parameters
|
68
|
+
assert_equal(false, @params.permitted?)
|
69
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@params)
|
70
|
+
assert_equal(@expected, parsed_hash)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_illformed_payloads_safe
|
74
|
+
@illformed_payloads.each do |p|
|
75
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse(p)
|
76
|
+
assert_equal({}, parsed_hash)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_illformed_payloads_unsafe
|
81
|
+
@illformed_payloads.each do |p|
|
82
|
+
assert_raises(InvalidDocument) do
|
83
|
+
ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(p)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_filter_fields_only
|
89
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, only: [:id, :title, :author])
|
90
|
+
expected = {
|
91
|
+
id: 'zorglub',
|
92
|
+
title: 'Ember Hamster',
|
93
|
+
author_id: nil
|
94
|
+
}
|
95
|
+
assert_equal(expected, parsed_hash)
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_filter_fields_except
|
99
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, except: [:id, :title, :author])
|
100
|
+
expected = {
|
101
|
+
src: 'http://example.com/images/productivity.png',
|
102
|
+
photographer_id: '9',
|
103
|
+
comment_ids: %w(1 2)
|
104
|
+
}
|
105
|
+
assert_equal(expected, parsed_hash)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_keys
|
109
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, keys: { author: :user, title: :post_title })
|
110
|
+
expected = {
|
111
|
+
id: 'zorglub',
|
112
|
+
post_title: 'Ember Hamster',
|
113
|
+
src: 'http://example.com/images/productivity.png',
|
114
|
+
user_id: nil,
|
115
|
+
photographer_id: '9',
|
116
|
+
comment_ids: %w(1 2)
|
117
|
+
}
|
118
|
+
assert_equal(expected, parsed_hash)
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_polymorphic
|
122
|
+
parsed_hash = ActiveModel::Serializer::Adapter::JsonApi::Deserialization.parse!(@hash, polymorphic: [:photographer])
|
123
|
+
expected = {
|
124
|
+
id: 'zorglub',
|
125
|
+
title: 'Ember Hamster',
|
126
|
+
src: 'http://example.com/images/productivity.png',
|
127
|
+
author_id: nil,
|
128
|
+
photographer_id: '9',
|
129
|
+
photographer_type: 'people',
|
130
|
+
comment_ids: %w(1 2)
|
131
|
+
}
|
132
|
+
assert_equal(expected, parsed_hash)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class JsonApi
|
7
|
+
class ResourceTypeConfigTest < ActiveSupport::TestCase
|
8
|
+
class ProfileTypeSerializer < ActiveModel::Serializer
|
9
|
+
attributes :name
|
10
|
+
type 'profile'
|
11
|
+
end
|
12
|
+
|
13
|
+
def setup
|
14
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
15
|
+
@author.bio = nil
|
16
|
+
@author.roles = []
|
17
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
18
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
19
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
20
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
21
|
+
@post.comments = [@comment]
|
22
|
+
@post.blog = @blog
|
23
|
+
@anonymous_post.comments = []
|
24
|
+
@anonymous_post.blog = nil
|
25
|
+
@comment.post = @post
|
26
|
+
@comment.author = nil
|
27
|
+
@post.author = @author
|
28
|
+
@anonymous_post.author = nil
|
29
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
30
|
+
@blog.writer = @author
|
31
|
+
@blog.articles = [@post, @anonymous_post]
|
32
|
+
@author.posts = []
|
33
|
+
end
|
34
|
+
|
35
|
+
def with_jsonapi_resource_type type
|
36
|
+
old_type = ActiveModelSerializers.config.jsonapi_resource_type
|
37
|
+
ActiveModelSerializers.config.jsonapi_resource_type = type
|
38
|
+
yield
|
39
|
+
ensure
|
40
|
+
ActiveModelSerializers.config.jsonapi_resource_type = old_type
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_config_plural
|
44
|
+
with_jsonapi_resource_type :plural do
|
45
|
+
hash = serializable(@comment, adapter: :json_api).serializable_hash
|
46
|
+
assert_equal('comments', hash[:data][:type])
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_config_singular
|
51
|
+
with_jsonapi_resource_type :singular do
|
52
|
+
hash = serializable(@comment, adapter: :json_api).serializable_hash
|
53
|
+
assert_equal('comment', hash[:data][:type])
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_explicit_type_value
|
58
|
+
hash = serializable(@author, serializer: ProfileTypeSerializer, adapter: :json_api).serializable_hash
|
59
|
+
assert_equal('profile', hash.fetch(:data).fetch(:type))
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def serializable(resource, options = {})
|
65
|
+
ActiveModel::SerializableResource.new(resource, options)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class JsonApi
|
7
|
+
class TopLevelJsonApiTest < ActiveSupport::TestCase
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@author.bio = nil
|
11
|
+
@author.roles = []
|
12
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
13
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
14
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
15
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
16
|
+
@post.comments = [@comment]
|
17
|
+
@post.blog = @blog
|
18
|
+
@anonymous_post.comments = []
|
19
|
+
@anonymous_post.blog = nil
|
20
|
+
@comment.post = @post
|
21
|
+
@comment.author = nil
|
22
|
+
@post.author = @author
|
23
|
+
@anonymous_post.author = nil
|
24
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
25
|
+
@blog.writer = @author
|
26
|
+
@blog.articles = [@post, @anonymous_post]
|
27
|
+
@author.posts = []
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_toplevel_jsonapi_defaults_to_false
|
31
|
+
assert_equal config.fetch(:jsonapi_include_toplevel_object), false
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_disable_toplevel_jsonapi
|
35
|
+
with_config(jsonapi_include_toplevel_object: false) do
|
36
|
+
hash = serialize(@post)
|
37
|
+
assert_nil(hash[:jsonapi])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_enable_toplevel_jsonapi
|
42
|
+
with_config(jsonapi_include_toplevel_object: true) do
|
43
|
+
hash = serialize(@post)
|
44
|
+
refute_nil(hash[:jsonapi])
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_default_toplevel_jsonapi_version
|
49
|
+
with_config(jsonapi_include_toplevel_object: true) do
|
50
|
+
hash = serialize(@post)
|
51
|
+
assert_equal('1.0', hash[:jsonapi][:version])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_toplevel_jsonapi_no_meta
|
56
|
+
with_config(jsonapi_include_toplevel_object: true) do
|
57
|
+
hash = serialize(@post)
|
58
|
+
assert_nil(hash[:jsonapi][:meta])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_toplevel_jsonapi_meta
|
63
|
+
new_config = {
|
64
|
+
jsonapi_include_toplevel_object: true,
|
65
|
+
jsonapi_toplevel_meta: {
|
66
|
+
'copyright' => 'Copyright 2015 Example Corp.'
|
67
|
+
}
|
68
|
+
}
|
69
|
+
with_config(new_config) do
|
70
|
+
hash = serialize(@post)
|
71
|
+
assert_equal(new_config[:jsonapi_toplevel_meta], hash.fetch(:jsonapi).fetch(:meta))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
private
|
76
|
+
|
77
|
+
def serialize(resource, options = {})
|
78
|
+
serializable(resource, { adapter: :json_api }.merge!(options)).serializable_hash
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class JsonTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
ActionController::Base.cache_store.clear
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
11
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
12
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
13
|
+
@post.comments = [@first_comment, @second_comment]
|
14
|
+
@first_comment.post = @post
|
15
|
+
@second_comment.post = @post
|
16
|
+
@post.author = @author
|
17
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
18
|
+
@post.blog = @blog
|
19
|
+
|
20
|
+
@serializer = PostSerializer.new(@post)
|
21
|
+
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_has_many
|
25
|
+
assert_equal([
|
26
|
+
{ id: 1, body: 'ZOMG A COMMENT' },
|
27
|
+
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
28
|
+
], @adapter.serializable_hash[:post][:comments])
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_custom_keys
|
32
|
+
serializer = PostWithCustomKeysSerializer.new(@post)
|
33
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
34
|
+
|
35
|
+
assert_equal({
|
36
|
+
id: 1,
|
37
|
+
reviews: [{ id: 1, body: 'ZOMG A COMMENT' },
|
38
|
+
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
39
|
+
],
|
40
|
+
writer: { id: 1, name: 'Steve K.' },
|
41
|
+
site: { id: 1, name: 'My Blog!!' }
|
42
|
+
}, adapter.serializable_hash[:post])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class NullTest < ActiveSupport::TestCase
|
7
|
+
def setup
|
8
|
+
profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
serializer = ProfileSerializer.new(profile)
|
10
|
+
|
11
|
+
@adapter = Null.new(serializer)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_serializable_hash
|
15
|
+
assert_equal({}, @adapter.serializable_hash)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_it_returns_empty_json
|
19
|
+
assert_equal('{}', @adapter.to_json)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class AdapterTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
profile = Profile.new
|
8
|
+
@serializer = ProfileSerializer.new(profile)
|
9
|
+
@adapter = ActiveModel::Serializer::Adapter::Base.new(@serializer)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_serializable_hash_is_abstract_method
|
13
|
+
assert_raises(NotImplementedError) do
|
14
|
+
@adapter.serializable_hash(only: [:name])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_serializer
|
19
|
+
assert_equal @serializer, @adapter.serializer
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_create_adapter
|
23
|
+
adapter = ActiveModel::Serializer::Adapter.create(@serializer)
|
24
|
+
assert_equal ActiveModel::Serializer::Adapter::Attributes, adapter.class
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_create_adapter_with_override
|
28
|
+
adapter = ActiveModel::Serializer::Adapter.create(@serializer, { adapter: :json_api })
|
29
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, adapter.class
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_inflected_adapter_class_for_known_adapter
|
33
|
+
ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'API' }
|
34
|
+
klass = ActiveModel::Serializer::Adapter.adapter_class(:json_api)
|
35
|
+
|
36
|
+
ActiveSupport::Inflector.inflections.acronyms.clear
|
37
|
+
|
38
|
+
assert_equal ActiveModel::Serializer::Adapter::JsonApi, klass
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -1,75 +1,38 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
{ :title => "Comment1" }
|
38
|
-
], serializer.as_json)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_array_serializer_with_root
|
42
|
-
comment1 = Comment.new(:title => "Comment1", :id => 1)
|
43
|
-
comment2 = Comment.new(:title => "Comment2", :id => 2)
|
44
|
-
|
45
|
-
array = [ comment1, comment2 ]
|
46
|
-
|
47
|
-
serializer = array.active_model_serializer.new(array, :root => :comments)
|
48
|
-
|
49
|
-
assert_equal({ :comments => [
|
50
|
-
{ :title => "Comment1" },
|
51
|
-
{ :title => "Comment2" }
|
52
|
-
]}, serializer.as_json)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_array_serializer_with_hash
|
56
|
-
hash = {:value => "something"}
|
57
|
-
array = [hash]
|
58
|
-
serializer = array.active_model_serializer.new(array, :root => :items)
|
59
|
-
assert_equal({ :items => [ hash.as_json ]}, serializer.as_json)
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_array_serializer_with_specified_seriailizer
|
63
|
-
post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
|
64
|
-
post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
|
65
|
-
|
66
|
-
array = [ post1, post2 ]
|
67
|
-
|
68
|
-
serializer = array.active_model_serializer.new array, :each_serializer => CustomPostSerializer
|
69
|
-
|
70
|
-
assert_equal([
|
71
|
-
{ :title => "Post1" },
|
72
|
-
{ :title => "Post2" }
|
73
|
-
], serializer.as_json)
|
1
|
+
require 'test_helper'
|
2
|
+
require_relative 'collection_serializer_test'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
class Serializer
|
6
|
+
# Minitest.run_one_method isn't present in minitest 4
|
7
|
+
if $minitest_version > 4 # rubocop:disable Style/GlobalVars
|
8
|
+
class ArraySerializerTest < CollectionSerializerTest
|
9
|
+
extend ActiveSupport::Testing::Stream
|
10
|
+
def self.run_one_method(*)
|
11
|
+
stderr = (capture(:stderr) do
|
12
|
+
super
|
13
|
+
end)
|
14
|
+
if stderr !~ /Calling deprecated ArraySerializer/
|
15
|
+
fail Minitest::Assertion, stderr
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def collection_serializer
|
20
|
+
ArraySerializer
|
21
|
+
end
|
22
|
+
end
|
23
|
+
else
|
24
|
+
class ArraySerializerTest < ActiveSupport::TestCase
|
25
|
+
extend ActiveSupport::Testing::Stream
|
26
|
+
def test_json_key_with_root_warns_when_using_array_serializer
|
27
|
+
stderr = (capture(:stderr) do
|
28
|
+
comment = Comment.new
|
29
|
+
post = Post.new
|
30
|
+
serializer = ArraySerializer.new([comment, post])
|
31
|
+
assert_equal 'comments', serializer.json_key
|
32
|
+
end)
|
33
|
+
assert_match(/Calling deprecated ArraySerializer/, stderr)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
74
37
|
end
|
75
38
|
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class CollectionSerializerTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
@comment = Comment.new
|
8
|
+
@post = Post.new
|
9
|
+
@resource = build_named_collection @comment, @post
|
10
|
+
@serializer = collection_serializer.new(@resource, { some: :options })
|
11
|
+
end
|
12
|
+
|
13
|
+
def collection_serializer
|
14
|
+
CollectionSerializer
|
15
|
+
end
|
16
|
+
|
17
|
+
def build_named_collection(*resource)
|
18
|
+
resource.define_singleton_method(:name) { 'MeResource' }
|
19
|
+
resource
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_has_object_reader_serializer_interface
|
23
|
+
assert_equal @serializer.object, @resource
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_respond_to_each
|
27
|
+
assert_respond_to @serializer, :each
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_each_object_should_be_serialized_with_appropriate_serializer
|
31
|
+
serializers = @serializer.to_a
|
32
|
+
|
33
|
+
assert_kind_of CommentSerializer, serializers.first
|
34
|
+
assert_kind_of Comment, serializers.first.object
|
35
|
+
|
36
|
+
assert_kind_of PostSerializer, serializers.last
|
37
|
+
assert_kind_of Post, serializers.last.object
|
38
|
+
|
39
|
+
assert_equal :options, serializers.last.custom_options[:some]
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_serializer_option_not_passed_to_each_serializer
|
43
|
+
serializers = collection_serializer.new([@post], { serializer: PostSerializer }).to_a
|
44
|
+
|
45
|
+
refute serializers.first.custom_options.key?(:serializer)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_root_default
|
49
|
+
@serializer = collection_serializer.new([@comment, @post])
|
50
|
+
assert_nil @serializer.root
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_root
|
54
|
+
expected = 'custom_root'
|
55
|
+
@serializer = collection_serializer.new([@comment, @post], root: expected)
|
56
|
+
assert_equal expected, @serializer.root
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_root_with_no_serializers
|
60
|
+
expected = 'custom_root'
|
61
|
+
@serializer = collection_serializer.new([], root: expected)
|
62
|
+
assert_equal expected, @serializer.root
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_json_key_with_resource_with_serializer
|
66
|
+
singular_key = @serializer.send(:serializers).first.json_key
|
67
|
+
assert_equal singular_key.pluralize, @serializer.json_key
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_json_key_with_resource_with_name_and_no_serializers
|
71
|
+
serializer = collection_serializer.new(build_named_collection)
|
72
|
+
assert_equal 'me_resources', serializer.json_key
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_json_key_with_resource_with_nil_name_and_no_serializers
|
76
|
+
resource = []
|
77
|
+
resource.define_singleton_method(:name) { nil }
|
78
|
+
serializer = collection_serializer.new(resource)
|
79
|
+
assert_nil serializer.json_key
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_json_key_with_resource_without_name_and_no_serializers
|
83
|
+
serializer = collection_serializer.new([])
|
84
|
+
assert_nil serializer.json_key
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_json_key_with_root
|
88
|
+
expected = 'custom_root'
|
89
|
+
serializer = collection_serializer.new(@resource, root: expected)
|
90
|
+
assert_equal expected, serializer.json_key
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_json_key_with_root_and_no_serializers
|
94
|
+
expected = 'custom_root'
|
95
|
+
serializer = collection_serializer.new(build_named_collection, root: expected)
|
96
|
+
assert_equal expected, serializer.json_key
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
|
4
|
+
ActiveRecord::Schema.define do
|
5
|
+
create_table :posts, force: true do |t|
|
6
|
+
t.string :title
|
7
|
+
t.text :body
|
8
|
+
t.references :author
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
create_table :authors, force: true do |t|
|
12
|
+
t.string :name
|
13
|
+
t.timestamps null: false
|
14
|
+
end
|
15
|
+
create_table :comments, force: true do |t|
|
16
|
+
t.text :contents
|
17
|
+
t.references :author
|
18
|
+
t.references :post
|
19
|
+
t.timestamp null: false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ARModels
|
24
|
+
class Post < ActiveRecord::Base
|
25
|
+
has_many :comments
|
26
|
+
belongs_to :author
|
27
|
+
end
|
28
|
+
|
29
|
+
class Comment < ActiveRecord::Base
|
30
|
+
belongs_to :post
|
31
|
+
belongs_to :author
|
32
|
+
end
|
33
|
+
|
34
|
+
class Author < ActiveRecord::Base
|
35
|
+
has_many :posts
|
36
|
+
end
|
37
|
+
|
38
|
+
class PostSerializer < ActiveModel::Serializer
|
39
|
+
attributes :id, :title, :body
|
40
|
+
|
41
|
+
has_many :comments
|
42
|
+
belongs_to :author
|
43
|
+
end
|
44
|
+
|
45
|
+
class CommentSerializer < ActiveModel::Serializer
|
46
|
+
attributes :id, :contents
|
47
|
+
|
48
|
+
belongs_to :author
|
49
|
+
end
|
50
|
+
|
51
|
+
class AuthorSerializer < ActiveModel::Serializer
|
52
|
+
attributes :id, :name
|
53
|
+
|
54
|
+
has_many :posts
|
55
|
+
end
|
56
|
+
end
|