active_model_serializers_rails_2.3 0.9.0.alpha1 → 0.9.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +11 -55
- data/lib/active_model/array_serializer.rb +9 -34
- data/lib/active_model/as_json_overrides.rb +6 -0
- data/lib/active_model/default_serializer.rb +3 -12
- data/lib/active_model/serializable.rb +5 -9
- data/lib/active_model/serializer/associations.rb +15 -53
- data/lib/active_model/serializer/railtie.rb +2 -2
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +29 -56
- data/lib/active_model_serializers.rb +2 -18
- data/test/coverage_setup.rb +9 -12
- data/test/fixtures/active_record.rb +3 -3
- data/test/integration/active_record/active_record_test.rb +21 -23
- data/test/test_app.rb +1 -4
- data/test/test_helper.rb +0 -3
- data/test/unit/active_model/array_serializer/meta_test.rb +1 -1
- data/test/unit/active_model/array_serializer/root_test.rb +2 -2
- data/test/unit/active_model/array_serializer/scope_test.rb +1 -1
- data/test/unit/active_model/array_serializer/serialize_test.rb +45 -0
- data/test/unit/active_model/default_serializer_test.rb +6 -4
- data/test/unit/active_model/serializer/attributes_test.rb +1 -13
- data/test/unit/active_model/serializer/config_test.rb +5 -5
- data/test/unit/active_model/serializer/filter_test.rb +2 -2
- data/test/unit/active_model/serializer/has_many_test.rb +6 -30
- data/test/unit/active_model/serializer/has_one_test.rb +5 -19
- data/test/unit/active_model/serializer/meta_test.rb +1 -1
- data/test/unit/active_model/serializer/root_test.rb +2 -16
- data/test/unit/active_model/serializer/scope_test.rb +1 -29
- metadata +11 -21
- data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
- data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
- data/test/unit/active_model/array_serializer/serialization_test.rb +0 -182
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -21
- data/test/unit/active_model/serializer/associations_test.rb +0 -19
@@ -1,182 +0,0 @@
|
|
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
|
@@ -1,21 +0,0 @@
|
|
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
|
@@ -1,19 +0,0 @@
|
|
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
|