active_model_serializers_rails_2.3 0.9.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +87 -0
- data/CONTRIBUTING.md +20 -0
- data/DESIGN.textile +586 -0
- data/MIT-LICENSE +21 -0
- data/README.md +793 -0
- data/lib/active_model/array_serializer.rb +60 -0
- data/lib/active_model/default_serializer.rb +27 -0
- data/lib/active_model/serializable.rb +25 -0
- data/lib/active_model/serializer.rb +220 -0
- data/lib/active_model/serializer/associations.rb +98 -0
- data/lib/active_model/serializer/config.rb +31 -0
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +14 -0
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +93 -0
- data/lib/active_model/serializer/railtie.rb +10 -0
- data/lib/active_model/serializer/version.rb +5 -0
- data/lib/active_model/serializer_support.rb +5 -0
- data/lib/active_model_serializers.rb +33 -0
- data/test/coverage_setup.rb +15 -0
- data/test/fixtures/active_record.rb +92 -0
- data/test/fixtures/poro.rb +64 -0
- data/test/integration/active_record/active_record_test.rb +77 -0
- data/test/test_app.rb +11 -0
- data/test/test_helper.rb +13 -0
- data/test/unit/active_model/array_serializer/meta_test.rb +53 -0
- data/test/unit/active_model/array_serializer/root_test.rb +102 -0
- data/test/unit/active_model/array_serializer/scope_test.rb +24 -0
- data/test/unit/active_model/array_serializer/serialization_test.rb +182 -0
- data/test/unit/active_model/default_serializer_test.rb +13 -0
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +21 -0
- data/test/unit/active_model/serializer/associations_test.rb +19 -0
- data/test/unit/active_model/serializer/attributes_test.rb +41 -0
- data/test/unit/active_model/serializer/config_test.rb +86 -0
- data/test/unit/active_model/serializer/filter_test.rb +49 -0
- data/test/unit/active_model/serializer/has_many_test.rb +174 -0
- data/test/unit/active_model/serializer/has_one_test.rb +151 -0
- data/test/unit/active_model/serializer/meta_test.rb +39 -0
- data/test/unit/active_model/serializer/root_test.rb +117 -0
- data/test/unit/active_model/serializer/scope_test.rb +49 -0
- metadata +127 -0
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'active_model/serializer'
|
3
|
+
|
4
|
+
module ActiveModel
|
5
|
+
class ArraySerializer
|
6
|
+
class MetaTest < Minitest::Test
|
7
|
+
def setup
|
8
|
+
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
10
|
+
@serializer = ArraySerializer.new([@profile1, @profile2], root: 'profiles')
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_meta
|
14
|
+
@serializer.meta = { total: 10 }
|
15
|
+
|
16
|
+
assert_equal({
|
17
|
+
'profiles' => [
|
18
|
+
{
|
19
|
+
name: 'Name 1',
|
20
|
+
description: 'Description 1'
|
21
|
+
}, {
|
22
|
+
name: 'Name 2',
|
23
|
+
description: 'Description 2'
|
24
|
+
}
|
25
|
+
],
|
26
|
+
meta: {
|
27
|
+
total: 10
|
28
|
+
}
|
29
|
+
}, @serializer.as_json)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_meta_using_meta_key
|
33
|
+
@serializer.meta_key = :my_meta
|
34
|
+
@serializer.meta = { total: 10 }
|
35
|
+
|
36
|
+
assert_equal({
|
37
|
+
'profiles' => [
|
38
|
+
{
|
39
|
+
name: 'Name 1',
|
40
|
+
description: 'Description 1'
|
41
|
+
}, {
|
42
|
+
name: 'Name 2',
|
43
|
+
description: 'Description 2'
|
44
|
+
}
|
45
|
+
],
|
46
|
+
my_meta: {
|
47
|
+
total: 10
|
48
|
+
}
|
49
|
+
}, @serializer.as_json)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,102 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class RootAsOptionTest < Minitest::Test
|
6
|
+
def setup
|
7
|
+
@old_root = ArraySerializer._root
|
8
|
+
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
10
|
+
@serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
ArraySerializer._root = @old_root
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_root_is_not_displayed_using_serializable_array
|
18
|
+
assert_equal([
|
19
|
+
{ name: 'Name 1', description: 'Description 1' },
|
20
|
+
{ name: 'Name 2', description: 'Description 2' }
|
21
|
+
], @serializer.serializable_array)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_root_using_as_json
|
25
|
+
assert_equal({
|
26
|
+
initialize: [
|
27
|
+
{ name: 'Name 1', description: 'Description 1' },
|
28
|
+
{ name: 'Name 2', description: 'Description 2' }
|
29
|
+
]
|
30
|
+
}, @serializer.as_json)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_root_as_argument_takes_precedence
|
34
|
+
assert_equal({
|
35
|
+
argument: [
|
36
|
+
{ name: 'Name 1', description: 'Description 1' },
|
37
|
+
{ name: 'Name 2', description: 'Description 2' }
|
38
|
+
]
|
39
|
+
}, @serializer.as_json(root: :argument))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_using_false_root_in_initialize_takes_precedence
|
43
|
+
ArraySerializer._root = 'root'
|
44
|
+
@serializer = ArraySerializer.new([@profile1, @profile2], root: false)
|
45
|
+
|
46
|
+
assert_equal([
|
47
|
+
{ name: 'Name 1', description: 'Description 1' },
|
48
|
+
{ name: 'Name 2', description: 'Description 2' }
|
49
|
+
], @serializer.as_json)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class RootInSerializerTest < Minitest::Test
|
54
|
+
def setup
|
55
|
+
@old_root = ArraySerializer._root
|
56
|
+
ArraySerializer._root = :in_serializer
|
57
|
+
@profile1 = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
58
|
+
@profile2 = Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
59
|
+
@serializer = ArraySerializer.new([@profile1, @profile2])
|
60
|
+
@rooted_serializer = ArraySerializer.new([@profile1, @profile2], root: :initialize)
|
61
|
+
end
|
62
|
+
|
63
|
+
def teardown
|
64
|
+
ArraySerializer._root = @old_root
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_root_is_not_displayed_using_serializable_hash
|
68
|
+
assert_equal([
|
69
|
+
{ name: 'Name 1', description: 'Description 1' },
|
70
|
+
{ name: 'Name 2', description: 'Description 2' }
|
71
|
+
], @serializer.serializable_array)
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_root_using_as_json
|
75
|
+
assert_equal({
|
76
|
+
in_serializer: [
|
77
|
+
{ name: 'Name 1', description: 'Description 1' },
|
78
|
+
{ name: 'Name 2', description: 'Description 2' }
|
79
|
+
]
|
80
|
+
}, @serializer.as_json)
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_root_in_initializer_takes_precedence
|
84
|
+
assert_equal({
|
85
|
+
initialize: [
|
86
|
+
{ name: 'Name 1', description: 'Description 1' },
|
87
|
+
{ name: 'Name 2', description: 'Description 2' }
|
88
|
+
]
|
89
|
+
}, @rooted_serializer.as_json)
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_root_as_argument_takes_precedence
|
93
|
+
assert_equal({
|
94
|
+
argument: [
|
95
|
+
{ name: 'Name 1', description: 'Description 1' },
|
96
|
+
{ name: 'Name 2', description: 'Description 2' }
|
97
|
+
]
|
98
|
+
}, @rooted_serializer.as_json(root: :argument))
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class ArraySerializer
|
5
|
+
class ScopeTest < Minitest::Test
|
6
|
+
def test_array_serializer_pass_options_to_items_serializers
|
7
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
8
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
9
|
+
serializer = ArraySerializer.new(array, scope: current_user)
|
10
|
+
|
11
|
+
expected = [{ name: 'Name 1', description: 'Description 1 - user' },
|
12
|
+
{ name: 'Name 2', description: 'Description 2 - user' }]
|
13
|
+
|
14
|
+
assert_equal expected, serializer.serializable_array
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def current_user
|
20
|
+
'user'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,182 @@
|
|
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 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 ModelSerializationTest < Minitest::Test
|
22
|
+
def test_array_serializer_serializes_models
|
23
|
+
array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
24
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
25
|
+
serializer = ArraySerializer.new(array)
|
26
|
+
|
27
|
+
expected = [{ name: 'Name 1', description: 'Description 1' },
|
28
|
+
{ name: 'Name 2', description: 'Description 2' }]
|
29
|
+
|
30
|
+
assert_equal expected, serializer.serializable_array
|
31
|
+
assert_equal expected, serializer.as_json
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_array_serializers_each_serializer
|
35
|
+
array = [::Model.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
36
|
+
::Model.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
|
37
|
+
serializer = ArraySerializer.new(array, each_serializer: ProfileSerializer)
|
38
|
+
|
39
|
+
expected = [{ name: 'Name 1', description: 'Description 1' },
|
40
|
+
{ name: 'Name 2', description: 'Description 2' }]
|
41
|
+
|
42
|
+
assert_equal expected, serializer.serializable_array
|
43
|
+
assert_equal expected, serializer.as_json
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_associated_objects_of_multiple_instances_embedded_in_root
|
47
|
+
@association = PostSerializer._associations[:comments]
|
48
|
+
@old_association = @association.dup
|
49
|
+
|
50
|
+
@association.embed = :ids
|
51
|
+
@association.embed_in_root = true
|
52
|
+
|
53
|
+
@post1 = Post.new({ title: 'Title 1', body: 'Body 1', date: '1/1/2000' })
|
54
|
+
@post2 = Post.new({ title: 'Title 2', body: 'Body 2', date: '1/1/2000' })
|
55
|
+
|
56
|
+
class << @post2
|
57
|
+
attr_writer :comments
|
58
|
+
end
|
59
|
+
|
60
|
+
@post2.comments = [
|
61
|
+
Comment.new(content: 'C3'),
|
62
|
+
Comment.new(content: 'C4')
|
63
|
+
]
|
64
|
+
|
65
|
+
@serializer = ArraySerializer.new([@post1, @post2], root: :posts)
|
66
|
+
assert_equal({
|
67
|
+
posts: [
|
68
|
+
{title: "Title 1", body: "Body 1", "comment_ids" => @post1.comments.map(&:object_id) },
|
69
|
+
{title: "Title 2", body: "Body 2", "comment_ids" => @post2.comments.map(&:object_id) }
|
70
|
+
],
|
71
|
+
comments: [
|
72
|
+
{content: "C1"},
|
73
|
+
{content: "C2"},
|
74
|
+
{content: "C3"},
|
75
|
+
{content: "C4"}
|
76
|
+
]
|
77
|
+
}, @serializer.as_json)
|
78
|
+
ensure
|
79
|
+
PostSerializer._associations[:comments] = @old_association
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_embed_object_for_has_one_association_with_nil_value
|
83
|
+
@association = UserSerializer._associations[:profile]
|
84
|
+
@old_association = @association.dup
|
85
|
+
|
86
|
+
@association.embed = :objects
|
87
|
+
|
88
|
+
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
89
|
+
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
90
|
+
|
91
|
+
class << @user1
|
92
|
+
def profile
|
93
|
+
nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
class << @user2
|
98
|
+
def profile
|
99
|
+
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
@serializer = ArraySerializer.new([@user1, @user2]) #, root: :posts)
|
104
|
+
assert_equal([
|
105
|
+
{ name: "User 1", email: "email1@server.com", profile: nil },
|
106
|
+
{ name: "User 2", email: "email2@server.com", profile: { name: 'Name 1', description: 'Desc 1' } }
|
107
|
+
], @serializer.as_json)
|
108
|
+
ensure
|
109
|
+
UserSerializer._associations[:profile] = @old_association
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_embed_object_in_root_for_has_one_association_with_nil_value
|
113
|
+
@association = UserSerializer._associations[:profile]
|
114
|
+
@old_association = @association.dup
|
115
|
+
|
116
|
+
@association.embed = :ids
|
117
|
+
@association.embed_in_root = true
|
118
|
+
|
119
|
+
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
120
|
+
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
121
|
+
|
122
|
+
class << @user1
|
123
|
+
def profile
|
124
|
+
nil
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class << @user2
|
129
|
+
def profile
|
130
|
+
@profile ||= Profile.new(name: 'Name 1', description: 'Desc 1')
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
135
|
+
assert_equal({
|
136
|
+
users: [
|
137
|
+
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
138
|
+
{ name: "User 2", email: "email2@server.com", 'profile_id' => @user2.profile.object_id }
|
139
|
+
],
|
140
|
+
'profiles' => [
|
141
|
+
{ name: 'Name 1', description: 'Desc 1' }
|
142
|
+
]
|
143
|
+
}, @serializer.as_json)
|
144
|
+
ensure
|
145
|
+
UserSerializer._associations[:profile] = @old_association
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_embed_object_in_root_for_has_one_association_with_all_nil_values
|
149
|
+
@association = UserSerializer._associations[:profile]
|
150
|
+
@old_association = @association.dup
|
151
|
+
|
152
|
+
@association.embed = :ids
|
153
|
+
@association.embed_in_root = true
|
154
|
+
|
155
|
+
@user1 = User.new({ name: 'User 1', email: 'email1@server.com' })
|
156
|
+
@user2 = User.new({ name: 'User 2', email: 'email2@server.com' })
|
157
|
+
|
158
|
+
class << @user1
|
159
|
+
def profile
|
160
|
+
nil
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
class << @user2
|
165
|
+
def profile
|
166
|
+
nil
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
@serializer = ArraySerializer.new([@user1, @user2], root: :users)
|
171
|
+
assert_equal({
|
172
|
+
users: [
|
173
|
+
{ name: "User 1", email: "email1@server.com", 'profile_id' => nil },
|
174
|
+
{ name: "User 2", email: "email2@server.com", 'profile_id' => nil }
|
175
|
+
]
|
176
|
+
}, @serializer.as_json)
|
177
|
+
ensure
|
178
|
+
UserSerializer._associations[:profile] = @old_association
|
179
|
+
end
|
180
|
+
end
|
181
|
+
end
|
182
|
+
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
|