active_model_serializers 0.10.2 → 0.10.3
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/.travis.yml +8 -1
- data/CHANGELOG.md +45 -3
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -1
- data/README.md +5 -2
- data/active_model_serializers.gemspec +2 -2
- data/docs/ARCHITECTURE.md +6 -7
- data/docs/README.md +2 -0
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +64 -0
- data/docs/general/rendering.md +35 -1
- data/docs/general/serializers.md +35 -5
- data/docs/howto/add_pagination_links.md +2 -2
- data/docs/howto/add_relationship_links.md +137 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +9 -2
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +41 -24
- data/lib/action_controller/serialization.rb +9 -0
- data/lib/active_model/serializer.rb +19 -22
- data/lib/active_model/serializer/association.rb +19 -4
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +8 -5
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +0 -0
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +5 -1
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +24 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -0
- data/lib/active_model_serializers/adapter/attributes.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api.rb +15 -22
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/key_transform.rb +4 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +1 -1
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/generators/rails/serializer_generator.rb +1 -1
- data/test/action_controller/json_api/fields_test.rb +57 -0
- data/test/action_controller/json_api/transform_test.rb +3 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +226 -0
- data/test/active_model_serializers/key_transform_test.rb +32 -0
- data/test/active_model_serializers/model_test.rb +11 -0
- data/test/adapter/attributes_test.rb +43 -0
- data/test/adapter/json/transform_test.rb +1 -1
- data/test/adapter/json_api/fields_test.rb +4 -3
- data/test/adapter/json_api/has_many_test.rb +21 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +166 -0
- data/test/adapter/json_api/linked_test.rb +24 -6
- data/test/adapter/json_api/links_test.rb +1 -1
- data/test/adapter/json_api/pagination_links_test.rb +17 -2
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +20 -0
- data/test/adapter/json_api/transform_test.rb +4 -3
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +1 -1
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/cache_test.rb +42 -4
- data/test/collection_serializer_test.rb +1 -1
- data/test/fixtures/poro.rb +44 -41
- data/test/generators/serializer_generator_test.rb +22 -5
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +97 -22
- data/test/serializers/attribute_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +3 -3
- data/test/serializers/serializer_for_with_namespace_test.rb +87 -0
- data/test/support/serialization_testing.rb +16 -0
- data/test/test_helper.rb +1 -0
- metadata +35 -14
- data/test/adapter/json_api/relationships_test.rb +0 -204
@@ -1,204 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
module Adapter
|
6
|
-
class JsonApi
|
7
|
-
class RelationshipTest < ActiveSupport::TestCase
|
8
|
-
class RelationshipAuthor < ::Model; end
|
9
|
-
|
10
|
-
class RelationshipAuthorSerializer < ActiveModel::Serializer
|
11
|
-
has_one :bio do
|
12
|
-
link :self, '//example.com/link_author/relationships/bio'
|
13
|
-
end
|
14
|
-
|
15
|
-
has_one :profile do
|
16
|
-
link :related do
|
17
|
-
"//example.com/profiles/#{object.profile.id}"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
has_many :locations do
|
22
|
-
link :related do
|
23
|
-
ids = object.locations.map(&:id).join(',')
|
24
|
-
href "//example.com/locations/#{ids}"
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
has_many :posts do
|
29
|
-
link :related do
|
30
|
-
ids = object.posts.map(&:id).join(',')
|
31
|
-
href "//example.com/posts/#{ids}"
|
32
|
-
meta ids: ids
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
has_many :comments do
|
37
|
-
link :self do
|
38
|
-
meta ids: [1]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
has_many :roles do |serializer|
|
43
|
-
meta count: object.posts.count
|
44
|
-
serializer.cached_roles
|
45
|
-
end
|
46
|
-
|
47
|
-
has_one :blog do
|
48
|
-
link :self, '//example.com/link_author/relationships/blog'
|
49
|
-
include_data false
|
50
|
-
end
|
51
|
-
|
52
|
-
belongs_to :reviewer do
|
53
|
-
meta name: 'Dan Brown'
|
54
|
-
include_data true
|
55
|
-
end
|
56
|
-
|
57
|
-
has_many :likes do
|
58
|
-
link :related do
|
59
|
-
ids = object.likes.map(&:id).join(',')
|
60
|
-
href "//example.com/likes/#{ids}"
|
61
|
-
meta ids: ids
|
62
|
-
end
|
63
|
-
meta liked: object.likes.any?
|
64
|
-
end
|
65
|
-
|
66
|
-
def cached_roles
|
67
|
-
[
|
68
|
-
Role.new(id: 'from-serializer-method')
|
69
|
-
]
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
def setup
|
74
|
-
@post = Post.new(id: 1337, comments: [], author: nil)
|
75
|
-
@bio = Bio.new(id: 1337)
|
76
|
-
@like = Like.new(id: 1337)
|
77
|
-
@role = Role.new(id: 'from-record')
|
78
|
-
@profile = Profile.new(id: 1337)
|
79
|
-
@location = Location.new(id: 1337)
|
80
|
-
@reviewer = Author.new(id: 1337)
|
81
|
-
@comment = Comment.new(id: 1337)
|
82
|
-
@author = RelationshipAuthor.new(
|
83
|
-
id: 1337,
|
84
|
-
posts: [@post],
|
85
|
-
reviewer: @reviewer,
|
86
|
-
bio: @bio,
|
87
|
-
likes: [@like],
|
88
|
-
roles: [@role],
|
89
|
-
locations: [@location],
|
90
|
-
profile: @profile,
|
91
|
-
comments: [@comment]
|
92
|
-
)
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_relationship_simple_link
|
96
|
-
expected = {
|
97
|
-
data: {
|
98
|
-
id: '1337',
|
99
|
-
type: 'bios'
|
100
|
-
},
|
101
|
-
links: {
|
102
|
-
self: '//example.com/link_author/relationships/bio'
|
103
|
-
}
|
104
|
-
}
|
105
|
-
assert_relationship(:bio, expected)
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_relationship_block_link
|
109
|
-
expected = {
|
110
|
-
data: { id: '1337', type: 'profiles' },
|
111
|
-
links: { related: '//example.com/profiles/1337' }
|
112
|
-
}
|
113
|
-
assert_relationship(:profile, expected)
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_relationship_block_link_href
|
117
|
-
expected = {
|
118
|
-
data: [{ id: '1337', type: 'locations' }],
|
119
|
-
links: {
|
120
|
-
related: { href: '//example.com/locations/1337' }
|
121
|
-
}
|
122
|
-
}
|
123
|
-
assert_relationship(:locations, expected)
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_relationship_block_link_href_and_meta
|
127
|
-
expected = {
|
128
|
-
data: [{ id: '1337', type: 'posts' }],
|
129
|
-
links: {
|
130
|
-
related: {
|
131
|
-
href: '//example.com/posts/1337',
|
132
|
-
meta: { ids: '1337' }
|
133
|
-
}
|
134
|
-
}
|
135
|
-
}
|
136
|
-
assert_relationship(:posts, expected)
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_relationship_block_link_meta
|
140
|
-
expected = {
|
141
|
-
data: [{ id: '1337', type: 'comments' }],
|
142
|
-
links: {
|
143
|
-
self: {
|
144
|
-
meta: { ids: [1] }
|
145
|
-
}
|
146
|
-
}
|
147
|
-
}
|
148
|
-
assert_relationship(:comments, expected)
|
149
|
-
end
|
150
|
-
|
151
|
-
def test_relationship_meta
|
152
|
-
expected = {
|
153
|
-
data: [{ id: 'from-serializer-method', type: 'roles' }],
|
154
|
-
meta: { count: 1 }
|
155
|
-
}
|
156
|
-
assert_relationship(:roles, expected)
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_relationship_not_including_data
|
160
|
-
@author.define_singleton_method(:read_attribute_for_serialization) do |attr|
|
161
|
-
fail 'should not be called' if attr == :blog
|
162
|
-
super(attr)
|
163
|
-
end
|
164
|
-
expected = {
|
165
|
-
links: { self: '//example.com/link_author/relationships/blog' }
|
166
|
-
}
|
167
|
-
assert_nothing_raised do
|
168
|
-
assert_relationship(:blog, expected)
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
def test_relationship_including_data_explicit
|
173
|
-
expected = {
|
174
|
-
data: { id: '1337', type: 'authors' },
|
175
|
-
meta: { name: 'Dan Brown' }
|
176
|
-
}
|
177
|
-
assert_relationship(:reviewer, expected)
|
178
|
-
end
|
179
|
-
|
180
|
-
def test_relationship_with_everything
|
181
|
-
expected = {
|
182
|
-
data: [{ id: '1337', type: 'likes' }],
|
183
|
-
links: {
|
184
|
-
related: {
|
185
|
-
href: '//example.com/likes/1337',
|
186
|
-
meta: { ids: '1337' }
|
187
|
-
}
|
188
|
-
},
|
189
|
-
meta: { liked: true }
|
190
|
-
}
|
191
|
-
assert_relationship(:likes, expected)
|
192
|
-
end
|
193
|
-
|
194
|
-
private
|
195
|
-
|
196
|
-
def assert_relationship(relationship_name, expected)
|
197
|
-
hash = serializable(@author, adapter: :json_api).serializable_hash
|
198
|
-
assert_equal(expected, hash[:data][:relationships][relationship_name])
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|