active_model_serializers 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +0 -6
- data/README.md +17 -109
- data/lib/action_controller/serialization.rb +8 -27
- data/lib/action_controller/serialization_test_case.rb +4 -4
- data/lib/active_model/array_serializer.rb +5 -12
- data/lib/active_model/serializable.rb +2 -24
- data/lib/active_model/serializer.rb +36 -70
- data/lib/active_model/serializer/{association.rb → associations.rb} +52 -8
- data/lib/active_model/serializer/railtie.rb +0 -8
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -1
- data/test/fixtures/poro.rb +1 -110
- data/test/integration/action_controller/serialization_test.rb +0 -16
- data/test/integration/action_controller/serialization_test_case_test.rb +0 -10
- data/test/integration/active_record/active_record_test.rb +2 -2
- data/test/test_app.rb +0 -3
- data/test/unit/active_model/array_serializer/serialization_test.rb +1 -1
- data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -15
- data/test/unit/active_model/serializer/attributes_test.rb +0 -16
- data/test/unit/active_model/serializer/config_test.rb +0 -3
- data/test/unit/active_model/serializer/has_many_test.rb +16 -51
- data/test/unit/active_model/serializer/has_one_test.rb +0 -32
- data/test/unit/active_model/serializer/options_test.rb +0 -19
- metadata +3 -18
- data/lib/active_model/serializable/utils.rb +0 -16
- data/lib/active_model/serializer/association/has_many.rb +0 -39
- data/lib/active_model/serializer/association/has_one.rb +0 -25
- data/test/fixtures/template.html.erb +0 -1
- data/test/integration/action_controller/namespaced_serialization_test.rb +0 -96
- data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
- 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/url_helpers_test.rb +0 -35
@@ -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
|
@@ -1,196 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class HasOnePolymorphicTest < ActiveModel::TestCase
|
6
|
-
def setup
|
7
|
-
@association = InterviewSerializer._associations[:attachment]
|
8
|
-
@old_association = @association.dup
|
9
|
-
|
10
|
-
@interview = Interview.new({ text: 'Text 1' })
|
11
|
-
@interview_serializer = InterviewSerializer.new(@interview)
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
InterviewSerializer._associations[:attachment] = @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, InterviewSerializer._associations.length
|
24
|
-
assert_kind_of Association::HasOne, @association
|
25
|
-
assert_equal true, @association.polymorphic
|
26
|
-
assert_equal 'attachment', @association.name
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash
|
30
|
-
@association.embed = :ids
|
31
|
-
|
32
|
-
assert_equal({
|
33
|
-
text: 'Text 1',
|
34
|
-
'attachment_id' => {
|
35
|
-
type: model_name(@interview.attachment),
|
36
|
-
id: @interview.attachment.object_id
|
37
|
-
}
|
38
|
-
}, @interview_serializer.serializable_hash)
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_associations_embedding_ids_serialization_using_as_json
|
42
|
-
@association.embed = :ids
|
43
|
-
|
44
|
-
assert_equal({
|
45
|
-
'interview' => {
|
46
|
-
text: 'Text 1',
|
47
|
-
'attachment_id' => {
|
48
|
-
type: model_name(@interview.attachment),
|
49
|
-
id: @interview.attachment.object_id
|
50
|
-
}
|
51
|
-
}
|
52
|
-
}, @interview_serializer.as_json)
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_associations_embedding_ids_serialization_using_serializable_hash_and_key_from_options
|
56
|
-
@association.embed = :ids
|
57
|
-
@association.key = 'key'
|
58
|
-
|
59
|
-
assert_equal({
|
60
|
-
text: 'Text 1',
|
61
|
-
'key' => {
|
62
|
-
type: model_name(@interview.attachment),
|
63
|
-
id: @interview.attachment.object_id
|
64
|
-
}
|
65
|
-
}, @interview_serializer.serializable_hash)
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash
|
69
|
-
@association.embed = :objects
|
70
|
-
|
71
|
-
assert_equal({
|
72
|
-
text: 'Text 1',
|
73
|
-
attachment: {
|
74
|
-
type: model_name(@interview.attachment),
|
75
|
-
model_name(@interview.attachment) => { url: 'U1'}
|
76
|
-
}
|
77
|
-
}, @interview_serializer.serializable_hash)
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_associations_embedding_objects_serialization_using_as_json
|
81
|
-
@association.embed = :objects
|
82
|
-
|
83
|
-
assert_equal({
|
84
|
-
'interview' => {
|
85
|
-
text: 'Text 1',
|
86
|
-
attachment: {
|
87
|
-
type: model_name(@interview.attachment),
|
88
|
-
model_name(@interview.attachment) => { url: 'U1'}
|
89
|
-
}
|
90
|
-
}
|
91
|
-
}, @interview_serializer.as_json)
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_associations_embedding_nil_ids_serialization_using_as_json
|
95
|
-
@association.embed = :ids
|
96
|
-
@interview.instance_eval do
|
97
|
-
def attachment
|
98
|
-
nil
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
assert_equal({
|
103
|
-
'interview' => { text: 'Text 1', 'attachment_id' => nil }
|
104
|
-
}, @interview_serializer.as_json)
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_associations_embedding_nil_objects_serialization_using_as_json
|
108
|
-
@association.embed = :objects
|
109
|
-
@interview.instance_eval do
|
110
|
-
def attachment
|
111
|
-
nil
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
assert_equal({
|
116
|
-
'interview' => { text: 'Text 1', attachment: nil }
|
117
|
-
}, @interview_serializer.as_json)
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_associations_embedding_objects_serialization_using_serializable_hash_and_root_from_options
|
121
|
-
@association.embed = :objects
|
122
|
-
@association.embedded_key = 'root'
|
123
|
-
|
124
|
-
assert_equal({
|
125
|
-
text: 'Text 1',
|
126
|
-
'root' => {
|
127
|
-
type: model_name(@interview.attachment),
|
128
|
-
model_name(@interview.attachment) => { url: 'U1'}
|
129
|
-
}
|
130
|
-
}, @interview_serializer.serializable_hash)
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_associations_embedding_ids_including_objects_serialization_using_serializable_hash
|
134
|
-
@association.embed = :ids
|
135
|
-
@association.embed_in_root = true
|
136
|
-
|
137
|
-
assert_equal({
|
138
|
-
text: 'Text 1',
|
139
|
-
'attachment_id' => {
|
140
|
-
type: model_name(@interview.attachment),
|
141
|
-
id: @interview.attachment.object_id
|
142
|
-
}
|
143
|
-
}, @interview_serializer.serializable_hash)
|
144
|
-
end
|
145
|
-
|
146
|
-
def test_associations_embedding_ids_including_objects_serialization_using_as_json
|
147
|
-
@association.embed = :ids
|
148
|
-
@association.embed_in_root = true
|
149
|
-
|
150
|
-
assert_equal({
|
151
|
-
'interview' => {
|
152
|
-
text: 'Text 1',
|
153
|
-
'attachment_id' => {
|
154
|
-
type: model_name(@interview.attachment),
|
155
|
-
id: @interview.attachment.object_id
|
156
|
-
}
|
157
|
-
},
|
158
|
-
"attachments" => [{
|
159
|
-
type: model_name(@interview.attachment),
|
160
|
-
model_name(@interview.attachment) => {
|
161
|
-
url: 'U1'
|
162
|
-
}
|
163
|
-
}]
|
164
|
-
}, @interview_serializer.as_json)
|
165
|
-
end
|
166
|
-
|
167
|
-
def test_associations_using_a_given_serializer
|
168
|
-
@association.embed = :ids
|
169
|
-
@association.embed_in_root = true
|
170
|
-
@association.serializer_from_options = Class.new(ActiveModel::Serializer) do
|
171
|
-
def name
|
172
|
-
'fake'
|
173
|
-
end
|
174
|
-
|
175
|
-
attributes :name
|
176
|
-
end
|
177
|
-
|
178
|
-
assert_equal({
|
179
|
-
'interview' => {
|
180
|
-
text: 'Text 1',
|
181
|
-
'attachment_id' => {
|
182
|
-
type: model_name(@interview.attachment),
|
183
|
-
id: @interview.attachment.object_id
|
184
|
-
}
|
185
|
-
},
|
186
|
-
"attachments" => [{
|
187
|
-
type: model_name(@interview.attachment),
|
188
|
-
model_name(@interview.attachment) => {
|
189
|
-
name: 'fake'
|
190
|
-
}
|
191
|
-
}]
|
192
|
-
}, @interview_serializer.as_json)
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class UrlHelpersTest < Minitest::Test
|
6
|
-
include Rails.application.routes.url_helpers
|
7
|
-
|
8
|
-
def setup
|
9
|
-
Object.const_set 'UserController', Class.new(ActionController::Base) do
|
10
|
-
def show
|
11
|
-
render text: 'profile'
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
Rails.application.routes.draw do
|
16
|
-
get '/profile/:id', as: :profile, controller: :user, action: :show
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_url_helpers_are_available
|
21
|
-
serializer = Class.new(ActiveModel::Serializer) do
|
22
|
-
attributes :url
|
23
|
-
|
24
|
-
def url
|
25
|
-
profile_url(id: object.object_id)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
profile = Profile.new
|
29
|
-
|
30
|
-
assert_equal({ url: profile_url(id: profile.object_id) },
|
31
|
-
serializer.new(profile).as_json)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|