active_model_serializers 0.10.9 → 0.10.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -2
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer.rb +10 -2
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers/adapter/attributes.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
- metadata +12 -259
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -109
- data/.simplecov +0 -110
- data/.travis.yml +0 -63
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -74
- data/Rakefile +0 -76
- data/active_model_serializers.gemspec +0 -64
- data/appveyor.yml +0 -28
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -269
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -185
- data/docs/general/deserialization.md +0 -100
- data/docs/general/fields.md +0 -31
- data/docs/general/getting_started.md +0 -133
- data/docs/general/instrumentation.md +0 -40
- data/docs/general/key_transforms.md +0 -40
- data/docs/general/logging.md +0 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -495
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +0 -138
- data/docs/howto/add_relationship_links.md +0 -140
- data/docs/howto/add_root_key.md +0 -62
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- data/docs/howto/test.md +0 -154
- data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
- data/docs/integrations/ember-and-json-api.md +0 -147
- data/docs/integrations/grape.md +0 -19
- data/docs/jsonapi/errors.md +0 -56
- data/docs/jsonapi/schema.md +0 -151
- data/docs/jsonapi/schema/schema.json +0 -366
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/test/action_controller/adapter_selector_test.rb +0 -64
- data/test/action_controller/explicit_serializer_test.rb +0 -137
- data/test/action_controller/json/include_test.rb +0 -248
- data/test/action_controller/json_api/deserialization_test.rb +0 -114
- data/test/action_controller/json_api/errors_test.rb +0 -42
- data/test/action_controller/json_api/fields_test.rb +0 -68
- data/test/action_controller/json_api/linked_test.rb +0 -204
- data/test/action_controller/json_api/pagination_test.rb +0 -126
- data/test/action_controller/json_api/transform_test.rb +0 -191
- data/test/action_controller/lookup_proc_test.rb +0 -51
- data/test/action_controller/namespace_lookup_test.rb +0 -234
- data/test/action_controller/serialization_scope_name_test.rb +0 -237
- data/test/action_controller/serialization_test.rb +0 -480
- data/test/active_model_serializers/adapter_for_test.rb +0 -210
- data/test/active_model_serializers/json_pointer_test.rb +0 -24
- data/test/active_model_serializers/logging_test.rb +0 -79
- data/test/active_model_serializers/model_test.rb +0 -144
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
- data/test/active_model_serializers/test/schema_test.rb +0 -133
- data/test/active_model_serializers/test/serializer_test.rb +0 -64
- data/test/active_record_test.rb +0 -11
- data/test/adapter/attributes_test.rb +0 -42
- data/test/adapter/deprecation_test.rb +0 -102
- data/test/adapter/json/belongs_to_test.rb +0 -47
- data/test/adapter/json/collection_test.rb +0 -106
- data/test/adapter/json/has_many_test.rb +0 -55
- data/test/adapter/json/transform_test.rb +0 -95
- data/test/adapter/json_api/belongs_to_test.rb +0 -157
- data/test/adapter/json_api/collection_test.rb +0 -98
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -98
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
- data/test/adapter/json_api/has_many_test.rb +0 -175
- data/test/adapter/json_api/has_one_test.rb +0 -82
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -415
- data/test/adapter/json_api/links_test.rb +0 -112
- data/test/adapter/json_api/pagination_links_test.rb +0 -208
- data/test/adapter/json_api/parse_test.rb +0 -139
- data/test/adapter/json_api/relationship_test.rb +0 -399
- data/test/adapter/json_api/resource_meta_test.rb +0 -102
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
- data/test/adapter/json_api/transform_test.rb +0 -514
- data/test/adapter/json_api/type_test.rb +0 -195
- data/test/adapter/json_test.rb +0 -48
- data/test/adapter/null_test.rb +0 -24
- data/test/adapter/polymorphic_test.rb +0 -220
- data/test/adapter_test.rb +0 -69
- data/test/array_serializer_test.rb +0 -24
- data/test/benchmark/app.rb +0 -67
- data/test/benchmark/benchmarking_support.rb +0 -69
- data/test/benchmark/bm_active_record.rb +0 -83
- data/test/benchmark/bm_adapter.rb +0 -40
- data/test/benchmark/bm_caching.rb +0 -121
- data/test/benchmark/bm_lookup_chain.rb +0 -85
- data/test/benchmark/bm_transform.rb +0 -47
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -85
- data/test/benchmark/fixtures.rb +0 -221
- data/test/cache_test.rb +0 -717
- data/test/collection_serializer_test.rb +0 -129
- data/test/fixtures/active_record.rb +0 -115
- data/test/fixtures/poro.rb +0 -227
- data/test/generators/scaffold_controller_generator_test.rb +0 -26
- data/test/generators/serializer_generator_test.rb +0 -77
- data/test/grape_test.rb +0 -198
- data/test/lint_test.rb +0 -51
- data/test/logger_test.rb +0 -22
- data/test/poro_test.rb +0 -11
- data/test/serializable_resource_test.rb +0 -81
- data/test/serializers/association_macros_test.rb +0 -39
- data/test/serializers/associations_test.rb +0 -520
- data/test/serializers/attribute_test.rb +0 -155
- data/test/serializers/attributes_test.rb +0 -54
- data/test/serializers/caching_configuration_test_isolated.rb +0 -172
- data/test/serializers/configuration_test.rb +0 -34
- data/test/serializers/fieldset_test.rb +0 -16
- data/test/serializers/meta_test.rb +0 -204
- data/test/serializers/options_test.rb +0 -34
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
- data/test/serializers/reflection_test.rb +0 -481
- data/test/serializers/root_test.rb +0 -23
- data/test/serializers/serialization_test.rb +0 -57
- data/test/serializers/serializer_for_test.rb +0 -138
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -86
- data/test/support/rails5_shims.rb +0 -55
- data/test/support/rails_app.rb +0 -40
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
- data/test/support/schemas/custom/show.json +0 -7
- data/test/support/schemas/hyper_schema.json +0 -93
- data/test/support/schemas/render_using_json_api.json +0 -43
- data/test/support/schemas/simple_json_pointers.json +0 -10
- data/test/support/serialization_testing.rb +0 -81
- data/test/test_helper.rb +0 -72
@@ -1,195 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModelSerializers
|
6
|
-
module Adapter
|
7
|
-
class JsonApi
|
8
|
-
class TypeTest < ActiveSupport::TestCase
|
9
|
-
class StringTypeSerializer < ActiveModel::Serializer
|
10
|
-
attribute :name
|
11
|
-
type 'profile'
|
12
|
-
end
|
13
|
-
|
14
|
-
class SymbolTypeSerializer < ActiveModel::Serializer
|
15
|
-
attribute :name
|
16
|
-
type :profile
|
17
|
-
end
|
18
|
-
|
19
|
-
setup do
|
20
|
-
@author = Author.new(id: 1, name: 'Steve K.')
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_config_plural
|
24
|
-
with_jsonapi_inflection :plural do
|
25
|
-
assert_type(@author, 'authors')
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_config_singular
|
30
|
-
with_jsonapi_inflection :singular do
|
31
|
-
assert_type(@author, 'author')
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def test_explicit_string_type_value
|
36
|
-
assert_type(@author, 'profile', serializer: StringTypeSerializer)
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_explicit_symbol_type_value
|
40
|
-
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
|
41
|
-
end
|
42
|
-
|
43
|
-
private
|
44
|
-
|
45
|
-
def assert_type(resource, expected_type, opts = {})
|
46
|
-
opts = opts.reverse_merge(adapter: :json_api)
|
47
|
-
hash = serializable(resource, opts).serializable_hash
|
48
|
-
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
49
|
-
end
|
50
|
-
end
|
51
|
-
class ResourceIdentifierTest < ActiveSupport::TestCase
|
52
|
-
class WithDefinedTypeSerializer < ActiveModel::Serializer
|
53
|
-
type 'with_defined_types'
|
54
|
-
end
|
55
|
-
|
56
|
-
class WithDefinedIdSerializer < ActiveModel::Serializer
|
57
|
-
def id
|
58
|
-
'special_id'
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
class FragmentedSerializer < ActiveModel::Serializer
|
63
|
-
cache only: :id
|
64
|
-
|
65
|
-
def id
|
66
|
-
'special_id'
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
setup do
|
71
|
-
@model = Author.new(id: 1, name: 'Steve K.')
|
72
|
-
ActionController::Base.cache_store.clear
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_defined_type
|
76
|
-
actual = with_jsonapi_inflection :plural do
|
77
|
-
actual_resource_identifier_object(WithDefinedTypeSerializer, @model)
|
78
|
-
end
|
79
|
-
expected = { id: expected_model_id(@model), type: 'with-defined-types' }
|
80
|
-
assert_equal actual, expected
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_defined_type_not_inflected
|
84
|
-
actual = with_jsonapi_inflection :singular do
|
85
|
-
actual_resource_identifier_object(WithDefinedTypeSerializer, @model)
|
86
|
-
end
|
87
|
-
expected = { id: expected_model_id(@model), type: 'with-defined-types' }
|
88
|
-
assert_equal actual, expected
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_singular_type
|
92
|
-
actual = with_jsonapi_inflection :singular do
|
93
|
-
actual_resource_identifier_object(AuthorSerializer, @model)
|
94
|
-
end
|
95
|
-
expected = { id: expected_model_id(@model), type: 'author' }
|
96
|
-
assert_equal actual, expected
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_plural_type
|
100
|
-
actual = with_jsonapi_inflection :plural do
|
101
|
-
actual_resource_identifier_object(AuthorSerializer, @model)
|
102
|
-
end
|
103
|
-
expected = { id: expected_model_id(@model), type: 'authors' }
|
104
|
-
assert_equal actual, expected
|
105
|
-
end
|
106
|
-
|
107
|
-
def test_type_with_namespace
|
108
|
-
Object.const_set(:Admin, Module.new)
|
109
|
-
model = Class.new(::Model)
|
110
|
-
Admin.const_set(:PowerUser, model)
|
111
|
-
serializer = Class.new(ActiveModel::Serializer)
|
112
|
-
Admin.const_set(:PowerUserSerializer, serializer)
|
113
|
-
with_namespace_separator '--' do
|
114
|
-
admin_user = Admin::PowerUser.new
|
115
|
-
serializer = Admin::PowerUserSerializer.new(admin_user)
|
116
|
-
expected = {
|
117
|
-
id: admin_user.id,
|
118
|
-
type: 'admin--power-users'
|
119
|
-
}
|
120
|
-
|
121
|
-
identifier = ResourceIdentifier.new(serializer, {})
|
122
|
-
actual = identifier.as_json
|
123
|
-
assert_equal(expected, actual)
|
124
|
-
end
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_id_defined_on_object
|
128
|
-
actual = actual_resource_identifier_object(AuthorSerializer, @model)
|
129
|
-
expected = { id: @model.id.to_s, type: expected_model_type(@model) }
|
130
|
-
assert_equal actual, expected
|
131
|
-
end
|
132
|
-
|
133
|
-
def test_blank_id
|
134
|
-
model = Author.new(id: nil, name: 'Steve K.')
|
135
|
-
actual = actual_resource_identifier_object(AuthorSerializer, model)
|
136
|
-
expected = { type: expected_model_type(model) }
|
137
|
-
assert_equal actual, expected
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_for_type_with_id
|
141
|
-
id = 1
|
142
|
-
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
|
143
|
-
expected = { id: '1', type: 'admin-users' }
|
144
|
-
assert_equal actual, expected
|
145
|
-
end
|
146
|
-
|
147
|
-
def test_for_type_with_id_given_blank_id
|
148
|
-
id = ''
|
149
|
-
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
|
150
|
-
expected = { type: 'admin-users' }
|
151
|
-
assert_equal actual, expected
|
152
|
-
end
|
153
|
-
|
154
|
-
def test_for_type_with_id_inflected
|
155
|
-
id = 2
|
156
|
-
actual = with_jsonapi_inflection :singular do
|
157
|
-
ResourceIdentifier.for_type_with_id('admin_users', id, {})
|
158
|
-
end
|
159
|
-
expected = { id: '2', type: 'admin-user' }
|
160
|
-
assert_equal actual, expected
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_id_defined_on_serializer
|
164
|
-
actual = actual_resource_identifier_object(WithDefinedIdSerializer, @model)
|
165
|
-
expected = { id: 'special_id', type: expected_model_type(@model) }
|
166
|
-
assert_equal actual, expected
|
167
|
-
end
|
168
|
-
|
169
|
-
def test_id_defined_on_fragmented
|
170
|
-
actual = actual_resource_identifier_object(FragmentedSerializer, @model)
|
171
|
-
expected = { id: 'special_id', type: expected_model_type(@model) }
|
172
|
-
assert_equal actual, expected
|
173
|
-
end
|
174
|
-
|
175
|
-
private
|
176
|
-
|
177
|
-
def actual_resource_identifier_object(serializer_class, model)
|
178
|
-
serializer = serializer_class.new(model)
|
179
|
-
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
180
|
-
resource_identifier.as_json
|
181
|
-
end
|
182
|
-
|
183
|
-
def expected_model_type(model, inflection = ActiveModelSerializers.config.jsonapi_resource_type)
|
184
|
-
with_jsonapi_inflection inflection do
|
185
|
-
model.class.model_name.send(inflection)
|
186
|
-
end
|
187
|
-
end
|
188
|
-
|
189
|
-
def expected_model_id(model)
|
190
|
-
model.id.to_s
|
191
|
-
end
|
192
|
-
end
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
data/test/adapter/json_test.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModelSerializers
|
6
|
-
module Adapter
|
7
|
-
class JsonTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
ActionController::Base.cache_store.clear
|
10
|
-
@author = Author.new(id: 1, name: 'Steve K.')
|
11
|
-
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
12
|
-
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
13
|
-
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
14
|
-
@post.comments = [@first_comment, @second_comment]
|
15
|
-
@first_comment.post = @post
|
16
|
-
@second_comment.post = @post
|
17
|
-
@post.author = @author
|
18
|
-
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
19
|
-
@post.blog = @blog
|
20
|
-
|
21
|
-
@serializer = PostSerializer.new(@post)
|
22
|
-
@adapter = ActiveModelSerializers::Adapter::Json.new(@serializer)
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_has_many
|
26
|
-
assert_equal([
|
27
|
-
{ id: 1, body: 'ZOMG A COMMENT' },
|
28
|
-
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
29
|
-
], @adapter.serializable_hash[:post][:comments])
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_custom_keys
|
33
|
-
serializer = PostWithCustomKeysSerializer.new(@post)
|
34
|
-
adapter = ActiveModelSerializers::Adapter::Json.new(serializer)
|
35
|
-
|
36
|
-
assert_equal({
|
37
|
-
id: 1,
|
38
|
-
reviews: [
|
39
|
-
{ id: 1, body: 'ZOMG A COMMENT' },
|
40
|
-
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
41
|
-
],
|
42
|
-
writer: { id: 1, name: 'Steve K.' },
|
43
|
-
site: { id: 1, name: 'My Blog!!' }
|
44
|
-
}, adapter.serializable_hash[:post])
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
data/test/adapter/null_test.rb
DELETED
@@ -1,24 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModelSerializers
|
6
|
-
module Adapter
|
7
|
-
class NullTest < ActiveSupport::TestCase
|
8
|
-
def setup
|
9
|
-
profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
10
|
-
serializer = ProfileSerializer.new(profile)
|
11
|
-
|
12
|
-
@adapter = Null.new(serializer)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_serializable_hash
|
16
|
-
assert_equal({}, @adapter.serializable_hash)
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_it_returns_empty_json
|
20
|
-
assert_equal('{}', @adapter.to_json)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,220 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
module Adapter
|
8
|
-
class PolymorphicTest < ActiveSupport::TestCase
|
9
|
-
setup do
|
10
|
-
@employee = Employee.new(id: 42, name: 'Zoop Zoopler', email: 'zoop@example.com')
|
11
|
-
@picture = @employee.pictures.new(id: 1, title: 'headshot-1.jpg')
|
12
|
-
@picture.imageable = @employee
|
13
|
-
end
|
14
|
-
|
15
|
-
def serialization(resource, adapter = :attributes)
|
16
|
-
serializable(resource, adapter: adapter, serializer: PolymorphicBelongsToSerializer).as_json
|
17
|
-
end
|
18
|
-
|
19
|
-
def tag_serialization(adapter = :attributes)
|
20
|
-
tag = PolyTag.new(id: 1, phrase: 'foo')
|
21
|
-
tag.object_tags << ObjectTag.new(id: 1, poly_tag_id: 1, taggable: @employee)
|
22
|
-
tag.object_tags << ObjectTag.new(id: 5, poly_tag_id: 1, taggable: @picture)
|
23
|
-
serializable(tag, adapter: adapter, serializer: PolymorphicTagSerializer, include: '*.*').as_json
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_attributes_serialization
|
27
|
-
expected =
|
28
|
-
{
|
29
|
-
id: 1,
|
30
|
-
title: 'headshot-1.jpg',
|
31
|
-
imageable: {
|
32
|
-
type: 'employee',
|
33
|
-
employee: {
|
34
|
-
id: 42,
|
35
|
-
name: 'Zoop Zoopler'
|
36
|
-
}
|
37
|
-
}
|
38
|
-
}
|
39
|
-
|
40
|
-
assert_equal(expected, serialization(@picture))
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_attributes_serialization_without_polymorphic_association
|
44
|
-
expected =
|
45
|
-
{
|
46
|
-
id: 2,
|
47
|
-
title: 'headshot-2.jpg',
|
48
|
-
imageable: nil
|
49
|
-
}
|
50
|
-
|
51
|
-
simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg')
|
52
|
-
assert_equal(expected, serialization(simple_picture))
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_attributes_serialization_with_polymorphic_has_many
|
56
|
-
expected =
|
57
|
-
{
|
58
|
-
id: 1,
|
59
|
-
phrase: 'foo',
|
60
|
-
object_tags: [
|
61
|
-
{
|
62
|
-
id: 1,
|
63
|
-
taggable: {
|
64
|
-
type: 'employee',
|
65
|
-
employee: {
|
66
|
-
id: 42
|
67
|
-
}
|
68
|
-
}
|
69
|
-
},
|
70
|
-
{
|
71
|
-
id: 5,
|
72
|
-
taggable: {
|
73
|
-
type: 'picture',
|
74
|
-
picture: {
|
75
|
-
id: 1
|
76
|
-
}
|
77
|
-
}
|
78
|
-
}
|
79
|
-
]
|
80
|
-
}
|
81
|
-
assert_equal(expected, tag_serialization)
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_json_serialization
|
85
|
-
expected =
|
86
|
-
{
|
87
|
-
picture: {
|
88
|
-
id: 1,
|
89
|
-
title: 'headshot-1.jpg',
|
90
|
-
imageable: {
|
91
|
-
type: 'employee',
|
92
|
-
employee: {
|
93
|
-
id: 42,
|
94
|
-
name: 'Zoop Zoopler'
|
95
|
-
}
|
96
|
-
}
|
97
|
-
}
|
98
|
-
}
|
99
|
-
|
100
|
-
assert_equal(expected, serialization(@picture, :json))
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_json_serialization_without_polymorphic_association
|
104
|
-
expected =
|
105
|
-
{
|
106
|
-
picture: {
|
107
|
-
id: 2,
|
108
|
-
title: 'headshot-2.jpg',
|
109
|
-
imageable: nil
|
110
|
-
}
|
111
|
-
}
|
112
|
-
|
113
|
-
simple_picture = Picture.new(id: 2, title: 'headshot-2.jpg')
|
114
|
-
assert_equal(expected, serialization(simple_picture, :json))
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_json_serialization_with_polymorphic_has_many
|
118
|
-
expected =
|
119
|
-
{
|
120
|
-
poly_tag: {
|
121
|
-
id: 1,
|
122
|
-
phrase: 'foo',
|
123
|
-
object_tags: [
|
124
|
-
{
|
125
|
-
id: 1,
|
126
|
-
taggable: {
|
127
|
-
type: 'employee',
|
128
|
-
employee: {
|
129
|
-
id: 42
|
130
|
-
}
|
131
|
-
}
|
132
|
-
},
|
133
|
-
{
|
134
|
-
id: 5,
|
135
|
-
taggable: {
|
136
|
-
type: 'picture',
|
137
|
-
picture: {
|
138
|
-
id: 1
|
139
|
-
}
|
140
|
-
}
|
141
|
-
}
|
142
|
-
]
|
143
|
-
}
|
144
|
-
}
|
145
|
-
assert_equal(expected, tag_serialization(:json))
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_json_api_serialization
|
149
|
-
expected =
|
150
|
-
{
|
151
|
-
data: {
|
152
|
-
id: '1',
|
153
|
-
type: 'pictures',
|
154
|
-
attributes: {
|
155
|
-
title: 'headshot-1.jpg'
|
156
|
-
},
|
157
|
-
relationships: {
|
158
|
-
imageable: {
|
159
|
-
data: {
|
160
|
-
id: '42',
|
161
|
-
type: 'employees'
|
162
|
-
}
|
163
|
-
}
|
164
|
-
}
|
165
|
-
}
|
166
|
-
}
|
167
|
-
|
168
|
-
assert_equal(expected, serialization(@picture, :json_api))
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_json_api_serialization_with_polymorphic_belongs_to
|
172
|
-
expected = {
|
173
|
-
data: {
|
174
|
-
id: '1',
|
175
|
-
type: 'poly-tags',
|
176
|
-
attributes: { phrase: 'foo' },
|
177
|
-
relationships: {
|
178
|
-
:"object-tags" => {
|
179
|
-
data: [
|
180
|
-
{ id: '1', type: 'object-tags' },
|
181
|
-
{ id: '5', type: 'object-tags' }
|
182
|
-
]
|
183
|
-
}
|
184
|
-
}
|
185
|
-
},
|
186
|
-
included: [
|
187
|
-
{
|
188
|
-
id: '1',
|
189
|
-
type: 'object-tags',
|
190
|
-
relationships: {
|
191
|
-
taggable: {
|
192
|
-
data: { id: '42', type: 'employees' }
|
193
|
-
}
|
194
|
-
}
|
195
|
-
},
|
196
|
-
{
|
197
|
-
id: '42',
|
198
|
-
type: 'employees'
|
199
|
-
},
|
200
|
-
{
|
201
|
-
id: '5',
|
202
|
-
type: 'object-tags',
|
203
|
-
relationships: {
|
204
|
-
taggable: {
|
205
|
-
data: { id: '1', type: 'pictures' }
|
206
|
-
}
|
207
|
-
}
|
208
|
-
},
|
209
|
-
{
|
210
|
-
id: '1',
|
211
|
-
type: 'pictures'
|
212
|
-
}
|
213
|
-
]
|
214
|
-
}
|
215
|
-
assert_equal(expected, tag_serialization(:json_api))
|
216
|
-
end
|
217
|
-
end
|
218
|
-
end
|
219
|
-
end
|
220
|
-
end
|