active_model_serializers 0.8.3 → 0.9.0

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.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -5
  3. data/CONTRIBUTING.md +20 -0
  4. data/DESIGN.textile +4 -4
  5. data/{MIT-LICENSE.txt → MIT-LICENSE} +0 -0
  6. data/README.md +307 -99
  7. data/lib/action_controller/serialization.rb +35 -16
  8. data/lib/action_controller/serialization_test_case.rb +79 -0
  9. data/lib/active_model/array_serializer.rb +40 -79
  10. data/lib/active_model/default_serializer.rb +32 -0
  11. data/lib/active_model/serializable.rb +40 -0
  12. data/lib/active_model/serializer/associations.rb +71 -202
  13. data/lib/active_model/serializer/config.rb +31 -0
  14. data/lib/active_model/serializer/generators/resource_override.rb +13 -0
  15. data/lib/{generators → active_model/serializer/generators}/serializer/USAGE +0 -0
  16. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
  17. data/lib/active_model/serializer/generators/serializer/serializer_generator.rb +37 -0
  18. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
  19. data/lib/active_model/serializer/generators/serializer/templates/serializer.rb +8 -0
  20. data/lib/active_model/serializer/railtie.rb +10 -0
  21. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  22. data/lib/active_model/serializer.rb +186 -433
  23. data/lib/active_model/serializer_support.rb +5 -0
  24. data/lib/active_model_serializers.rb +13 -88
  25. data/test/fixtures/active_record.rb +92 -0
  26. data/test/fixtures/poro.rb +75 -0
  27. data/test/integration/action_controller/serialization_test.rb +287 -0
  28. data/test/integration/action_controller/serialization_test_case_test.rb +61 -0
  29. data/test/integration/active_record/active_record_test.rb +77 -0
  30. data/test/integration/generators/resource_generator_test.rb +26 -0
  31. data/test/integration/generators/scaffold_controller_generator_test.rb +64 -0
  32. data/test/integration/generators/serializer_generator_test.rb +41 -0
  33. data/test/test_app.rb +11 -0
  34. data/test/test_helper.rb +10 -18
  35. data/test/unit/active_model/array_serializer/except_test.rb +18 -0
  36. data/test/unit/active_model/array_serializer/key_format_test.rb +18 -0
  37. data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
  38. data/test/unit/active_model/array_serializer/only_test.rb +18 -0
  39. data/test/unit/active_model/array_serializer/root_test.rb +102 -0
  40. data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
  41. data/test/unit/active_model/array_serializer/serialization_test.rb +199 -0
  42. data/test/unit/active_model/default_serializer_test.rb +13 -0
  43. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
  44. data/test/unit/active_model/serializer/associations_test.rb +19 -0
  45. data/test/unit/active_model/serializer/attributes_test.rb +41 -0
  46. data/test/unit/active_model/serializer/config_test.rb +88 -0
  47. data/test/unit/active_model/serializer/filter_test.rb +69 -0
  48. data/test/unit/active_model/serializer/has_many_test.rb +230 -0
  49. data/test/unit/active_model/serializer/has_one_test.rb +207 -0
  50. data/test/unit/active_model/serializer/key_format_test.rb +25 -0
  51. data/test/unit/active_model/serializer/meta_test.rb +39 -0
  52. data/test/unit/active_model/serializer/options_test.rb +15 -0
  53. data/test/unit/active_model/serializer/root_test.rb +117 -0
  54. data/test/unit/active_model/serializer/scope_test.rb +49 -0
  55. metadata +86 -62
  56. data/.gitignore +0 -18
  57. data/.travis.yml +0 -28
  58. data/Gemfile +0 -4
  59. data/Gemfile.edge +0 -9
  60. data/Rakefile +0 -18
  61. data/active_model_serializers.gemspec +0 -24
  62. data/bench/perf.rb +0 -43
  63. data/cruft.md +0 -19
  64. data/lib/active_record/serializer_override.rb +0 -16
  65. data/lib/generators/resource_override.rb +0 -13
  66. data/lib/generators/serializer/serializer_generator.rb +0 -42
  67. data/lib/generators/serializer/templates/serializer.rb +0 -19
  68. data/test/array_serializer_test.rb +0 -75
  69. data/test/association_test.rb +0 -592
  70. data/test/caching_test.rb +0 -96
  71. data/test/generators_test.rb +0 -85
  72. data/test/no_serialization_scope_test.rb +0 -34
  73. data/test/serialization_scope_name_test.rb +0 -67
  74. data/test/serialization_test.rb +0 -392
  75. data/test/serializer_support_test.rb +0 -51
  76. data/test/serializer_test.rb +0 -1465
  77. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,199 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class ArraySerializer
5
+ class BasicObjectsSerializationTest < Minitest::Test
6
+ def setup
7
+ array = [1, 2, 3]
8
+ @serializer = Serializer.serializer_for(array).new(array)
9
+ end
10
+
11
+ def test_serializer_for_array_returns_appropriate_type
12
+ assert_kind_of ActiveModel::ArraySerializer, @serializer
13
+ end
14
+
15
+ def test_array_serializer_serializes_simple_objects
16
+ assert_equal [1, 2, 3], @serializer.serializable_array
17
+ assert_equal [1, 2, 3], @serializer.as_json
18
+ end
19
+ end
20
+
21
+ class CustomArraySerializerSupport < Minitest::Test
22
+ def setup
23
+ Object.const_set(:ArraySerializer, Class.new)
24
+
25
+ array = [1, 2, 3]
26
+ @serializer_class = Serializer.serializer_for(array)
27
+ end
28
+
29
+ def teardown
30
+ Object.send(:remove_const, :ArraySerializer)
31
+ end
32
+
33
+ def test_serializer_for_array_returns_appropriate_type
34
+ assert_equal ::ArraySerializer, @serializer_class
35
+ end
36
+ end
37
+
38
+ class ModelSerializationTest < Minitest::Test
39
+ def test_array_serializer_serializes_models
40
+ array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
41
+ Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
42
+ serializer = ArraySerializer.new(array)
43
+
44
+ expected = [{ name: 'Name 1', description: 'Description 1' },
45
+ { name: 'Name 2', description: 'Description 2' }]
46
+
47
+ assert_equal expected, serializer.serializable_array
48
+ assert_equal expected, serializer.as_json
49
+ end
50
+
51
+ def test_array_serializers_each_serializer
52
+ array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
53
+ ::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
54
+ serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
55
+
56
+ expected = [{ name: 'Name 1', description: 'Description 1' },
57
+ { name: 'Name 2', description: 'Description 2' }]
58
+
59
+ assert_equal expected, serializer.serializable_array
60
+ assert_equal expected, serializer.as_json
61
+ end
62
+
63
+ def test_associated_objects_of_multiple_instances_embedded_in_root
64
+ @association = PostSerializer._associations[:comments]
65
+ @old_association = @association.dup
66
+
67
+ @association.embed = :ids
68
+ @association.embed_in_root = true
69
+
70
+ @post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
71
+ @post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
72
+
73
+ class << @post2
74
+ attr_writer :comments
75
+ end
76
+
77
+ @post2.comments = [
78
+ Comment.new(content: 'C3'),
79
+ Comment.new(content: 'C4')
80
+ ]
81
+
82
+ @serializer = ArraySerializer.new([@post1, @post2], root: :posts)
83
+ assert_equal({
84
+ posts: [
85
+ {title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
86
+ {title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
87
+ ],
88
+ comments: [
89
+ {content: "C1"},
90
+ {content: "C2"},
91
+ {content: "C3"},
92
+ {content: "C4"}
93
+ ]
94
+ }, @serializer.as_json)
95
+ ensure
96
+ PostSerializer._associations[:comments] = @old_association
97
+ end
98
+
99
+ def test_embed_object_for_has_one_association_with_nil_value
100
+ @association = UserSerializer._associations[:profile]
101
+ @old_association = @association.dup
102
+
103
+ @association.embed = :objects
104
+
105
+ @user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
106
+ @user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
107
+
108
+ class << @user1
109
+ def profile
110
+ nil
111
+ end
112
+ end
113
+
114
+ class << @user2
115
+ def profile
116
+ @profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
117
+ end
118
+ end
119
+
120
+ @serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts)
121
+ assert_equal([
122
+ { name: "User 1", email: "email1@server.com", profile: nil },
123
+ { name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } }
124
+ ], @serializer.as_json)
125
+ ensure
126
+ UserSerializer._associations[:profile] = @old_association
127
+ end
128
+
129
+ def test_embed_object_in_root_for_has_one_association_with_nil_value
130
+ @association = UserSerializer._associations[:profile]
131
+ @old_association = @association.dup
132
+
133
+ @association.embed = :ids
134
+ @association.embed_in_root = true
135
+
136
+ @user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
137
+ @user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
138
+
139
+ class << @user1
140
+ def profile
141
+ nil
142
+ end
143
+ end
144
+
145
+ class << @user2
146
+ def profile
147
+ @profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
148
+ end
149
+ end
150
+
151
+ @serializer = ArraySerializer.new([@user1, @user2], root: :users)
152
+ assert_equal({
153
+ users: [
154
+ { name: "User 1", email: "email1@server.com", 'profile_id' => nil },
155
+ { name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id }
156
+ ],
157
+ 'profiles' => [
158
+ { name: 'Name 1', description: 'Desc 1' }
159
+ ]
160
+ }, @serializer.as_json)
161
+ ensure
162
+ UserSerializer._associations[:profile] = @old_association
163
+ end
164
+
165
+ def test_embed_object_in_root_for_has_one_association_with_all_nil_values
166
+ @association = UserSerializer._associations[:profile]
167
+ @old_association = @association.dup
168
+
169
+ @association.embed = :ids
170
+ @association.embed_in_root = true
171
+
172
+ @user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
173
+ @user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
174
+
175
+ class << @user1
176
+ def profile
177
+ nil
178
+ end
179
+ end
180
+
181
+ class << @user2
182
+ def profile
183
+ nil
184
+ end
185
+ end
186
+
187
+ @serializer = ArraySerializer.new([@user1, @user2], root: :users)
188
+ assert_equal({
189
+ users: [
190
+ { name: "User 1", email: "email1@server.com", 'profile_id' => nil },
191
+ { name: "User 2", email: "email2@server.com", 'profile_id' => nil }
192
+ ]
193
+ }, @serializer.as_json)
194
+ ensure
195
+ UserSerializer._associations[:profile] = @old_association
196
+ end
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class DefaultSerializer
5
+ class Test < Minitest::Test
6
+ def test_serialize_objects
7
+ assert_equal(nil, DefaultSerializer.new(nil).serializable_object)
8
+ assert_equal(1, DefaultSerializer.new(1).serializable_object)
9
+ assert_equal('hi', DefaultSerializer.new('hi').serializable_object)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class Association
6
+ class BuildSerializerTest < Minitest::Test
7
+ def setup
8
+ @association = Association::HasOne.new('post', serializer: PostSerializer)
9
+ @post = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
10
+ end
11
+
12
+ def test_build_serializer_for_array_called_twice
13
+ 2.times do
14
+ serializer = @association.build_serializer(@post)
15
+ assert_instance_of(PostSerializer, serializer)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AssociationsTest < Minitest::Test
6
+ def test_associations_inheritance
7
+ inherited_serializer_klass = Class.new(PostSerializer) do
8
+ has_many :users
9
+ end
10
+ another_inherited_serializer_klass = Class.new(PostSerializer)
11
+
12
+ assert_equal([:comments, :users],
13
+ inherited_serializer_klass._associations.keys)
14
+ assert_equal([:comments],
15
+ another_inherited_serializer_klass._associations.keys)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class AttributesTest < Minitest::Test
6
+ def setup
7
+ @profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
8
+ @profile_serializer = ProfileSerializer.new(@profile)
9
+ end
10
+
11
+ def test_attributes_definition
12
+ assert_equal([:name, :description],
13
+ @profile_serializer.class._attributes)
14
+ end
15
+
16
+ def test_attributes_serialization_using_serializable_hash
17
+ assert_equal({
18
+ name: 'Name 1', description: 'Description 1'
19
+ }, @profile_serializer.serializable_hash)
20
+ end
21
+
22
+ def test_attributes_serialization_using_as_json
23
+ assert_equal({
24
+ 'profile' => { name: 'Name 1', description: 'Description 1' }
25
+ }, @profile_serializer.as_json)
26
+ end
27
+
28
+ def test_attributes_inheritance
29
+ inherited_serializer_klass = Class.new(ProfileSerializer) do
30
+ attributes :comments
31
+ end
32
+ another_inherited_serializer_klass = Class.new(ProfileSerializer)
33
+
34
+ assert_equal([:name, :description, :comments],
35
+ inherited_serializer_klass._attributes)
36
+ assert_equal([:name, :description],
37
+ another_inherited_serializer_klass._attributes)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,88 @@
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
+ ensure
82
+ PostSerializer._associations[:comments] = old_association
83
+ CONFIG.clear
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,69 @@
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
@@ -0,0 +1,230 @@
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_nothing_including_objects_serialization_using_as_json
119
+ @association.embed = nil
120
+ @association.embed_in_root = true
121
+
122
+ assert_equal({
123
+ 'post' => { title: 'Title 1', body: 'Body 1' },
124
+ comments: [{ content: 'C1' }, { content: 'C2' }]
125
+ }, @post_serializer.as_json)
126
+ end
127
+
128
+ def test_associations_using_a_given_serializer
129
+ @association.embed = :ids
130
+ @association.embed_in_root = true
131
+ @association.serializer_from_options = Class.new(Serializer) do
132
+ def content
133
+ object.read_attribute_for_serialization(:content) + '!'
134
+ end
135
+
136
+ attributes :content
137
+ end
138
+
139
+ assert_equal({
140
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
141
+ comments: [{ content: 'C1!' }, { content: 'C2!' }]
142
+ }, @post_serializer.as_json)
143
+ end
144
+
145
+ def test_associations_embedding_ids_using_a_given_array_serializer
146
+ @association.embed = :ids
147
+ @association.embed_in_root = true
148
+ @association.serializer_from_options = Class.new(ArraySerializer) do
149
+ def serializable_object
150
+ { my_content: ['fake'] }
151
+ end
152
+ end
153
+
154
+ assert_equal({
155
+ 'post' => { title: 'Title 1', body: 'Body 1', 'comment_ids' => @post.comments.map { |c| c.object_id } },
156
+ comments: { my_content: ['fake'] }
157
+ }, @post_serializer.as_json)
158
+ end
159
+
160
+ def test_associations_embedding_objects_using_a_given_array_serializer
161
+ @association.serializer_from_options = Class.new(ArraySerializer) do
162
+ def serializable_object
163
+ { my_content: ['fake'] }
164
+ end
165
+ end
166
+
167
+ assert_equal({
168
+ 'post' => { title: 'Title 1', body: 'Body 1', comments: { my_content: ['fake'] } }
169
+ }, @post_serializer.as_json)
170
+ end
171
+
172
+ def test_associations_embedding_ids_including_objects_serialization_with_embed_in_root_key
173
+ @association.embed_in_root = true
174
+ @association.embed_in_root_key = :linked
175
+ @association.embed = :ids
176
+ assert_equal({
177
+ linked: {
178
+ comments: [
179
+ { content: 'C1' },
180
+ { content: 'C2' }
181
+ ]
182
+ },
183
+ 'post' => {
184
+ title: 'Title 1', body: 'Body 1',
185
+ 'comment_ids' => @post.comments.map(&:object_id)
186
+ }
187
+ }, @post_serializer.as_json)
188
+ end
189
+
190
+ def test_associations_embedding_ids_using_embed_namespace_including_object_serialization_with_embed_in_root_key
191
+ @association.embed_in_root = true
192
+ @association.embed_in_root_key = :linked
193
+ @association.embed = :ids
194
+ @association.embed_namespace = :links
195
+ @association.key = :comments
196
+ assert_equal({
197
+ linked: {
198
+ comments: [
199
+ { content: 'C1' },
200
+ { content: 'C2' }
201
+ ]
202
+ },
203
+ 'post' => {
204
+ title: 'Title 1', body: 'Body 1',
205
+ links: {
206
+ comments: @post.comments.map(&:object_id)
207
+ }
208
+ }
209
+ }, @post_serializer.as_json)
210
+ end
211
+
212
+ def test_associations_embedding_objects_using_embed_namespace
213
+ @association.embed = :objects
214
+ @association.embed_namespace = :links
215
+
216
+ assert_equal({
217
+ 'post' => {
218
+ title: 'Title 1', body: 'Body 1',
219
+ links: {
220
+ comments: [
221
+ { content: 'C1' },
222
+ { content: 'C2' }
223
+ ]
224
+ }
225
+ }
226
+ }, @post_serializer.as_json)
227
+ end
228
+ end
229
+ end
230
+ end