active_model_serializers 0.9.12 → 0.10.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +638 -82
- data/MIT-LICENSE +3 -2
- data/README.md +194 -846
- data/lib/action_controller/serialization.rb +34 -74
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +53 -38
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +99 -0
- data/lib/active_model/serializer/concerns/caching.rb +305 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- 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 +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +361 -263
- data/lib/active_model_serializers/adapter/attributes.rb +36 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +94 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +62 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers.rb +58 -27
- data/lib/generators/rails/USAGE +6 -0
- data/lib/{active_model/serializer/generators → generators/rails}/resource_override.rb +1 -4
- data/lib/{active_model/serializer/generators/serializer → generators/rails}/serializer_generator.rb +4 -5
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +60 -0
- metadata +248 -155
- data/CONTRIBUTING.md +0 -20
- data/DESIGN.textile +0 -586
- data/lib/action_controller/serialization_test_case.rb +0 -82
- data/lib/active_model/array_serializer.rb +0 -70
- data/lib/active_model/default_serializer.rb +0 -30
- data/lib/active_model/serializable/utils.rb +0 -18
- data/lib/active_model/serializable.rb +0 -61
- data/lib/active_model/serializer/association/has_many.rb +0 -41
- data/lib/active_model/serializer/association/has_one.rb +0 -27
- data/lib/active_model/serializer/config.rb +0 -33
- data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -16
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/lib/active_model/serializer/railtie.rb +0 -24
- data/lib/active_model/serializer_support.rb +0 -7
- data/test/benchmark/app.rb +0 -60
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -41
- data/test/benchmark/setup.rb +0 -75
- data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
- data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
- data/test/fixtures/active_record.rb +0 -96
- data/test/fixtures/poro.rb +0 -255
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
- data/test/integration/action_controller/serialization_test.rb +0 -287
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
- data/test/integration/active_record/active_record_test.rb +0 -94
- data/test/integration/generators/resource_generator_test.rb +0 -26
- data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
- data/test/integration/generators/serializer_generator_test.rb +0 -41
- data/test/test_app.rb +0 -18
- data/test/test_helper.rb +0 -31
- data/test/tmp/app/assets/javascripts/accounts.js +0 -2
- data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
- data/test/tmp/app/controllers/accounts_controller.rb +0 -3
- data/test/tmp/app/helpers/accounts_helper.rb +0 -3
- data/test/tmp/app/serializers/account_serializer.rb +0 -4
- data/test/tmp/config/routes.rb +0 -2
- data/test/unit/active_model/array_serializer/except_test.rb +0 -18
- data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
- data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
- data/test/unit/active_model/array_serializer/only_test.rb +0 -18
- data/test/unit/active_model/array_serializer/options_test.rb +0 -16
- data/test/unit/active_model/array_serializer/root_test.rb +0 -102
- data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
- data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
- data/test/unit/active_model/default_serializer_test.rb +0 -13
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
- data/test/unit/active_model/serializer/associations_test.rb +0 -49
- data/test/unit/active_model/serializer/attributes_test.rb +0 -57
- data/test/unit/active_model/serializer/config_test.rb +0 -91
- data/test/unit/active_model/serializer/filter_test.rb +0 -69
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- data/test/unit/active_model/serializer/has_many_test.rb +0 -265
- data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
- data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
- data/test/unit/active_model/serializer/has_one_test.rb +0 -253
- data/test/unit/active_model/serializer/key_format_test.rb +0 -25
- data/test/unit/active_model/serializer/meta_test.rb +0 -39
- data/test/unit/active_model/serializer/options_test.rb +0 -42
- data/test/unit/active_model/serializer/root_test.rb +0 -117
- data/test/unit/active_model/serializer/scope_test.rb +0 -49
- data/test/unit/active_model/serializer/url_helpers_test.rb +0 -36
- data/test/unit/active_model/serilizable_test.rb +0 -50
- /data/lib/{active_model/serializer/generators/serializer/templates/serializer.rb → generators/rails/templates/serializer.rb.erb} +0 -0
@@ -1,91 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class Config
|
6
|
-
class Test < Minitest::Test
|
7
|
-
def test_config_const_is_an_instance_of_config
|
8
|
-
assert_kind_of Config, CONFIG
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_config_instance
|
12
|
-
config = Config.new
|
13
|
-
config.setting1 = 'value1'
|
14
|
-
|
15
|
-
assert_equal 'value1', config.setting1
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_each_config
|
19
|
-
config = Config.new
|
20
|
-
config.setting1 = 'value1'
|
21
|
-
config.setting2 = 'value2'
|
22
|
-
|
23
|
-
actual = {}
|
24
|
-
|
25
|
-
config.each do |k, v|
|
26
|
-
actual[k] = v
|
27
|
-
end
|
28
|
-
|
29
|
-
assert_equal({ 'setting1' => 'value1', 'setting2' => 'value2' }, actual)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
class ConfigTest < Minitest::Test
|
34
|
-
def test_setup
|
35
|
-
Serializer.setup do |config|
|
36
|
-
config.a = 'v1'
|
37
|
-
config.b = 'v2'
|
38
|
-
end
|
39
|
-
|
40
|
-
assert_equal 'v1', CONFIG.a
|
41
|
-
assert_equal 'v2', CONFIG.b
|
42
|
-
ensure
|
43
|
-
CONFIG.clear
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_config_accessors
|
47
|
-
Serializer.setup do |config|
|
48
|
-
config.foo = 'v1'
|
49
|
-
config.bar = 'v2'
|
50
|
-
end
|
51
|
-
|
52
|
-
assert_equal 'v1', CONFIG.foo
|
53
|
-
assert_equal 'v2', CONFIG.bar
|
54
|
-
ensure
|
55
|
-
CONFIG.clear
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_acessor_when_nil
|
59
|
-
assert_nil CONFIG.foo
|
60
|
-
CONFIG.foo = 1
|
61
|
-
assert_equal 1, CONFIG.foo
|
62
|
-
assert_nil CONFIG.bar
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class ApplyConfigTest < Minitest::Test
|
67
|
-
def test_apply_config_to_associations
|
68
|
-
CONFIG.embed = :ids
|
69
|
-
CONFIG.embed_in_root = true
|
70
|
-
CONFIG.key_format = :lower_camel
|
71
|
-
|
72
|
-
association = PostSerializer._associations[:comments]
|
73
|
-
old_association = association.dup
|
74
|
-
|
75
|
-
association.send :initialize, association.name, association.options
|
76
|
-
|
77
|
-
assert association.embed_ids?
|
78
|
-
assert !association.embed_objects?
|
79
|
-
assert association.embed_in_root
|
80
|
-
assert_equal :lower_camel, association.key_format
|
81
|
-
assert_equal 'post', PostSerializer.root_name
|
82
|
-
CONFIG.plural_default_root = true
|
83
|
-
assert_equal 'posts', PostSerializer.root_name
|
84
|
-
ensure
|
85
|
-
PostSerializer._associations[:comments] = old_association
|
86
|
-
CONFIG.clear
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class FilterOptionsTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_only_option
|
11
|
-
@profile_serializer = ProfileSerializer.new(@profile, only: :name)
|
12
|
-
assert_equal({
|
13
|
-
'profile' => { name: 'Name 1' }
|
14
|
-
}, @profile_serializer.as_json)
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_except_option
|
18
|
-
@profile_serializer = ProfileSerializer.new(@profile, except: :comments)
|
19
|
-
assert_equal({
|
20
|
-
'profile' => { name: 'Name 1', description: 'Description 1' }
|
21
|
-
}, @profile_serializer.as_json)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
class FilterAttributesTest < Minitest::Test
|
26
|
-
def setup
|
27
|
-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
28
|
-
@profile_serializer = ProfileSerializer.new(@profile)
|
29
|
-
@profile_serializer.instance_eval do
|
30
|
-
def filter(keys)
|
31
|
-
keys - [:description]
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_filtered_attributes_serialization
|
37
|
-
assert_equal({
|
38
|
-
'profile' => { name: 'Name 1' }
|
39
|
-
}, @profile_serializer.as_json)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
class FilterAssociationsTest < Minitest::Test
|
44
|
-
def setup
|
45
|
-
@association = PostSerializer._associations[:comments]
|
46
|
-
@old_association = @association.dup
|
47
|
-
@association.embed = :ids
|
48
|
-
@association.embed_in_root = true
|
49
|
-
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
50
|
-
@post_serializer = PostSerializer.new(@post)
|
51
|
-
@post_serializer.instance_eval do
|
52
|
-
def filter(keys)
|
53
|
-
keys - [:body, :comments]
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def teardown
|
59
|
-
PostSerializer._associations[:comments] = @old_association
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_filtered_associations_serialization
|
63
|
-
assert_equal({
|
64
|
-
'post' => { title: 'Title 1' }
|
65
|
-
}, @post_serializer.as_json)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,189 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasManyPolymorphicTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@association = MailSerializer._associations[:attachments]
|
8
|
-
@old_association = @association.dup
|
9
|
-
|
10
|
-
@mail = Mail.new({ body: 'Body 1' })
|
11
|
-
@mail_serializer = MailSerializer.new(@mail)
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
MailSerializer._associations[:attachments] = @old_association
|
16
|
-
end
|
17
|
-
|
18
|
-
def model_name(object)
|
19
|
-
object.class.to_s.demodulize.underscore.to_sym
|
20
|
-
end
|
21
|
-
|
22
|
-
def test_associations_definition
|
23
|
-
assert_equal 1, MailSerializer._associations.length
|
24
|
-
assert_kind_of Association::HasMany, @association
|
25
|
-
assert_equal true, @association.polymorphic
|
26
|
-
assert_equal 'attachments', @association.name
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash
|
30
|
-
@association.embed = :ids
|
31
|
-
|
32
|
-
assert_equal({
|
33
|
-
body: 'Body 1',
|
34
|
-
'attachment_ids' => @mail.attachments.map do |c|
|
35
|
-
{ id: c.object_id, type: model_name(c) }
|
36
|
-
end
|
37
|
-
}, @mail_serializer.serializable_hash)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_associations_embedding_ids_serialization_using_as_json
|
41
|
-
@association.embed = :ids
|
42
|
-
|
43
|
-
assert_equal({
|
44
|
-
'mail' => {
|
45
|
-
:body => 'Body 1',
|
46
|
-
'attachment_ids' => @mail.attachments.map do |c|
|
47
|
-
{ id: c.object_id, type: model_name(c) }
|
48
|
-
end
|
49
|
-
}
|
50
|
-
}, @mail_serializer.as_json)
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
54
|
-
@association.embed = :ids
|
55
|
-
@association.key = 'key'
|
56
|
-
|
57
|
-
assert_equal({
|
58
|
-
body: 'Body 1',
|
59
|
-
'key' => @mail.attachments.map do |c|
|
60
|
-
{ id: c.object_id, type: model_name(c) }
|
61
|
-
end
|
62
|
-
}, @mail_serializer.serializable_hash)
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash
|
66
|
-
@association.embed = :objects
|
67
|
-
|
68
|
-
assert_equal({
|
69
|
-
body: 'Body 1',
|
70
|
-
:attachments => [
|
71
|
-
{ type: :image, image: { url: 'U1' }},
|
72
|
-
{ type: :video, video: { html: 'H1' }}
|
73
|
-
]
|
74
|
-
}, @mail_serializer.serializable_hash)
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_associations_embedding_objects_serialization_using_as_json
|
78
|
-
@association.embed = :objects
|
79
|
-
|
80
|
-
assert_equal({
|
81
|
-
'mail' => {
|
82
|
-
body: 'Body 1',
|
83
|
-
attachments: [
|
84
|
-
{ type: :image, image: { url: 'U1' }},
|
85
|
-
{ type: :video, video: { html: 'H1' }}
|
86
|
-
]
|
87
|
-
}
|
88
|
-
}, @mail_serializer.as_json)
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_associations_embedding_nil_objects_serialization_using_as_json
|
92
|
-
@association.embed = :objects
|
93
|
-
@mail.instance_eval do
|
94
|
-
def attachments
|
95
|
-
[nil]
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
assert_equal({
|
100
|
-
'mail' => {
|
101
|
-
:body => 'Body 1',
|
102
|
-
:attachments => [nil]
|
103
|
-
}
|
104
|
-
}, @mail_serializer.as_json)
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
108
|
-
@association.embed = :objects
|
109
|
-
@association.embedded_key = 'root'
|
110
|
-
|
111
|
-
assert_equal({
|
112
|
-
body: 'Body 1',
|
113
|
-
'root' => [
|
114
|
-
{ type: :image, image: { url: 'U1' }},
|
115
|
-
{ type: :video, video: { html: 'H1' }}
|
116
|
-
]
|
117
|
-
}, @mail_serializer.serializable_hash)
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
121
|
-
@association.embed = :ids
|
122
|
-
@association.embed_in_root = true
|
123
|
-
|
124
|
-
assert_equal({
|
125
|
-
body: 'Body 1',
|
126
|
-
'attachment_ids' => @mail.attachments.map do |c|
|
127
|
-
{ id: c.object_id, type: model_name(c) }
|
128
|
-
end
|
129
|
-
}, @mail_serializer.serializable_hash)
|
130
|
-
end
|
131
|
-
|
132
|
-
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
133
|
-
@association.embed = :ids
|
134
|
-
@association.embed_in_root = true
|
135
|
-
|
136
|
-
assert_equal({
|
137
|
-
'mail' => {
|
138
|
-
body: 'Body 1',
|
139
|
-
'attachment_ids' => @mail.attachments.map do |c|
|
140
|
-
{ id: c.object_id, type: model_name(c) }
|
141
|
-
end,
|
142
|
-
},
|
143
|
-
'attachments' => [
|
144
|
-
{ type: :image, image: { url: 'U1' }},
|
145
|
-
{ type: :video, video: { html: 'H1' }}
|
146
|
-
]
|
147
|
-
}, @mail_serializer.as_json)
|
148
|
-
end
|
149
|
-
|
150
|
-
def test_associations_embedding_nothing_including_objects_serialization_using_as_json
|
151
|
-
@association.embed = nil
|
152
|
-
@association.embed_in_root = true
|
153
|
-
|
154
|
-
assert_equal({
|
155
|
-
'mail' => { body: 'Body 1' },
|
156
|
-
'attachments' => [
|
157
|
-
{ type: :image, image: { url: 'U1' }},
|
158
|
-
{ type: :video, video: { html: 'H1' }}
|
159
|
-
]
|
160
|
-
}, @mail_serializer.as_json)
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_associations_using_a_given_serializer
|
164
|
-
@association.embed = :ids
|
165
|
-
@association.embed_in_root = true
|
166
|
-
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
|
167
|
-
def fake
|
168
|
-
'fake'
|
169
|
-
end
|
170
|
-
|
171
|
-
attributes :fake
|
172
|
-
end
|
173
|
-
|
174
|
-
assert_equal({
|
175
|
-
'mail' => {
|
176
|
-
body: 'Body 1',
|
177
|
-
'attachment_ids' => @mail.attachments.map do |c|
|
178
|
-
{ id: c.object_id, type: model_name(c) }
|
179
|
-
end
|
180
|
-
},
|
181
|
-
'attachments' => [
|
182
|
-
{ type: :image, image: { fake: 'fake' }},
|
183
|
-
{ type: :video, video: { fake: 'fake' }}
|
184
|
-
]
|
185
|
-
}, @mail_serializer.as_json)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
@@ -1,265 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasManyTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@association = PostSerializer._associations[:comments]
|
8
|
-
@old_association = @association.dup
|
9
|
-
|
10
|
-
@post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
11
|
-
@post_serializer = PostSerializer.new(@post)
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
PostSerializer._associations[:comments] = @old_association
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_associations_definition
|
19
|
-
assert_equal 1, PostSerializer._associations.length
|
20
|
-
assert_kind_of Association::HasMany, @association
|
21
|
-
assert_equal 'comments', @association.name
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_associations_inheritance
|
25
|
-
inherited_serializer_klass = Class.new(PostSerializer) do
|
26
|
-
has_many :some_associations
|
27
|
-
end
|
28
|
-
another_inherited_serializer_klass = Class.new(PostSerializer)
|
29
|
-
|
30
|
-
assert_equal(PostSerializer._associations.length + 1,
|
31
|
-
inherited_serializer_klass._associations.length)
|
32
|
-
assert_equal(PostSerializer._associations.length,
|
33
|
-
another_inherited_serializer_klass._associations.length)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash
|
37
|
-
@association.embed = :ids
|
38
|
-
|
39
|
-
assert_equal({
|
40
|
-
title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
41
|
-
}, @post_serializer.serializable_hash)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_associations_embedding_ids_serialization_using_as_json
|
45
|
-
@association.embed = :ids
|
46
|
-
|
47
|
-
assert_equal({
|
48
|
-
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }
|
49
|
-
}, @post_serializer.as_json)
|
50
|
-
end
|
51
|
-
|
52
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
53
|
-
@association.embed = :ids
|
54
|
-
@association.key = 'key'
|
55
|
-
|
56
|
-
assert_equal({
|
57
|
-
title: 'Title 1', body: 'Body 1', 'key' => @post.comments.map { |c| c.object_id }
|
58
|
-
}, @post_serializer.serializable_hash)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash
|
62
|
-
@association.embed = :objects
|
63
|
-
|
64
|
-
assert_equal({
|
65
|
-
title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }]
|
66
|
-
}, @post_serializer.serializable_hash)
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_associations_embedding_objects_serialization_using_as_json
|
70
|
-
@association.embed = :objects
|
71
|
-
|
72
|
-
assert_equal({
|
73
|
-
'post' => { title: 'Title 1', body: 'Body 1', comments: [{ content: 'C1' }, { content: 'C2' }] }
|
74
|
-
}, @post_serializer.as_json)
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_associations_embedding_nil_objects_serialization_using_as_json
|
78
|
-
@association.embed = :objects
|
79
|
-
@post.instance_eval do
|
80
|
-
def comments
|
81
|
-
[nil]
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
assert_equal({
|
86
|
-
'post' => { title: 'Title 1', body: 'Body 1', comments: [nil] }
|
87
|
-
}, @post_serializer.as_json)
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
91
|
-
@association.embed = :objects
|
92
|
-
@association.embedded_key = 'root'
|
93
|
-
|
94
|
-
assert_equal({
|
95
|
-
title: 'Title 1', body: 'Body 1', 'root' => [{ content: 'C1' }, { content: 'C2' }]
|
96
|
-
}, @post_serializer.serializable_hash)
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
100
|
-
@association.embed = :ids
|
101
|
-
@association.embed_in_root = true
|
102
|
-
|
103
|
-
assert_equal({
|
104
|
-
title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id }
|
105
|
-
}, @post_serializer.serializable_hash)
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
109
|
-
@association.embed = :ids
|
110
|
-
@association.embed_in_root = true
|
111
|
-
|
112
|
-
assert_equal({
|
113
|
-
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
114
|
-
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
115
|
-
}, @post_serializer.as_json)
|
116
|
-
end
|
117
|
-
|
118
|
-
def test_associations_embedding_ids_including_objects_serialization_when_invoked_from_parent_serializer
|
119
|
-
@association.embed = :ids
|
120
|
-
@association.embed_in_root = true
|
121
|
-
|
122
|
-
category = Category.new(name: 'Name 1')
|
123
|
-
category.instance_variable_set(:@posts, [@post])
|
124
|
-
category_serializer = CategorySerializer.new(category)
|
125
|
-
|
126
|
-
assert_equal({
|
127
|
-
'category' => {
|
128
|
-
name: 'Name 1',
|
129
|
-
posts: [{ title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } }]
|
130
|
-
},
|
131
|
-
"comments" => [{ content: 'C1' }, { content: 'C2' }]
|
132
|
-
}, category_serializer.as_json)
|
133
|
-
end
|
134
|
-
|
135
|
-
def test_associations_embedding_nothing_including_objects_serialization_using_as_json
|
136
|
-
@association.embed = nil
|
137
|
-
@association.embed_in_root = true
|
138
|
-
|
139
|
-
assert_equal({
|
140
|
-
'post' => { title: 'Title 1', body: 'Body 1' },
|
141
|
-
'comments' => [{ content: 'C1' }, { content: 'C2' }]
|
142
|
-
}, @post_serializer.as_json)
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_associations_using_a_given_serializer
|
146
|
-
@association.embed = :ids
|
147
|
-
@association.embed_in_root = true
|
148
|
-
@association.serializer_from_options = Class.new(Serializer) do
|
149
|
-
def content
|
150
|
-
object.read_attribute_for_serialization(:content) + '!'
|
151
|
-
end
|
152
|
-
|
153
|
-
attributes :content
|
154
|
-
end
|
155
|
-
|
156
|
-
assert_equal({
|
157
|
-
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
158
|
-
'comments' => [{ content: 'C1!' }, { content: 'C2!' }]
|
159
|
-
}, @post_serializer.as_json)
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_associations_embedding_ids_using_a_given_array_serializer
|
163
|
-
@association.embed = :ids
|
164
|
-
@association.embed_in_root = true
|
165
|
-
@association.serializer_from_options = Class.new(ArraySerializer) do
|
166
|
-
def serializable_object
|
167
|
-
{ my_content: ['fake'] }
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
assert_equal({
|
172
|
-
'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
|
173
|
-
'comments' => { my_content: ['fake'] }
|
174
|
-
}, @post_serializer.as_json)
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_associations_embedding_objects_using_a_given_array_serializer
|
178
|
-
@association.serializer_from_options = Class.new(ArraySerializer) do
|
179
|
-
def serializable_object(options={})
|
180
|
-
{ my_content: ['fake'] }
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
assert_equal({
|
185
|
-
'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
|
186
|
-
}, @post_serializer.as_json)
|
187
|
-
end
|
188
|
-
|
189
|
-
def test_associations_embedding_ids_including_objects_serialization_with_embed_in_root_key
|
190
|
-
@association.embed_in_root = true
|
191
|
-
@association.embed_in_root_key = :linked
|
192
|
-
@association.embed = :ids
|
193
|
-
assert_equal({
|
194
|
-
'post' => {
|
195
|
-
title: 'Title 1', body: 'Body 1',
|
196
|
-
'comment_ids' => @post.comments.map(&:object_id)
|
197
|
-
},
|
198
|
-
linked: {
|
199
|
-
'comments' => [
|
200
|
-
{ content: 'C1' },
|
201
|
-
{ content: 'C2' }
|
202
|
-
]
|
203
|
-
},
|
204
|
-
}, @post_serializer.as_json)
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_associations_embedding_ids_using_embed_namespace_including_object_serialization_with_embed_in_root_key
|
208
|
-
@association.embed_in_root = true
|
209
|
-
@association.embed_in_root_key = :linked
|
210
|
-
@association.embed = :ids
|
211
|
-
@association.embed_namespace = :links
|
212
|
-
@association.key = :comments
|
213
|
-
assert_equal({
|
214
|
-
'post' => {
|
215
|
-
title: 'Title 1', body: 'Body 1',
|
216
|
-
links: {
|
217
|
-
comments: @post.comments.map(&:object_id)
|
218
|
-
}
|
219
|
-
},
|
220
|
-
linked: {
|
221
|
-
'comments' => [
|
222
|
-
{ content: 'C1' },
|
223
|
-
{ content: 'C2' }
|
224
|
-
]
|
225
|
-
},
|
226
|
-
}, @post_serializer.as_json)
|
227
|
-
end
|
228
|
-
|
229
|
-
def test_associations_embedding_objects_using_embed_namespace
|
230
|
-
@association.embed = :objects
|
231
|
-
@association.embed_namespace = :links
|
232
|
-
|
233
|
-
assert_equal({
|
234
|
-
'post' => {
|
235
|
-
title: 'Title 1', body: 'Body 1',
|
236
|
-
links: {
|
237
|
-
comments: [
|
238
|
-
{ content: 'C1' },
|
239
|
-
{ content: 'C2' }
|
240
|
-
]
|
241
|
-
}
|
242
|
-
}
|
243
|
-
}, @post_serializer.as_json)
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_associations_name_key_embedding_ids_serialization_using_serializable_hash
|
247
|
-
@association = NameKeyPostSerializer._associations[:comments]
|
248
|
-
@association.embed = :ids
|
249
|
-
|
250
|
-
assert_equal({
|
251
|
-
title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id }
|
252
|
-
}, NameKeyPostSerializer.new(@post).serializable_hash)
|
253
|
-
end
|
254
|
-
|
255
|
-
def test_associations_name_key_embedding_ids_serialization_using_as_json
|
256
|
-
@association = NameKeyPostSerializer._associations[:comments]
|
257
|
-
@association.embed = :ids
|
258
|
-
|
259
|
-
assert_equal({
|
260
|
-
'name_key_post' => { title: 'Title 1', body: 'Body 1', 'comments' => @post.comments.map { |c| c.object_id } }
|
261
|
-
}, NameKeyPostSerializer.new(@post).as_json)
|
262
|
-
end
|
263
|
-
end
|
264
|
-
end
|
265
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasOneAndHasManyTest < Minitest::Test
|
6
|
-
def setup
|
7
|
-
@post = SpecialPost.new({ title: 'T1', body: 'B1'})
|
8
|
-
@post_serializer = SpecialPostSerializer.new(@post)
|
9
|
-
end
|
10
|
-
|
11
|
-
def teardown
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_side_load_has_one_and_has_many_in_same_array
|
15
|
-
assert_equal({
|
16
|
-
"post" => {
|
17
|
-
title: 'T1',
|
18
|
-
body: 'B1',
|
19
|
-
'comment_ids' => @post.comments.map { |c| c.object_id },
|
20
|
-
'special_comment_id' => @post_serializer.special_comment.object_id,
|
21
|
-
},
|
22
|
-
"comments" => [{ content: 'C1' }, { content: 'C2' }, { content: 'special' }]
|
23
|
-
}, @post_serializer.as_json(root: 'post'))
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|