active_model_serializers_custom 0.10.90
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +35 -0
- data/.rubocop.yml +109 -0
- data/.simplecov +110 -0
- data/.travis.yml +63 -0
- data/CHANGELOG.md +727 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +74 -0
- data/MIT-LICENSE +22 -0
- data/README.md +305 -0
- data/Rakefile +76 -0
- data/active_model_serializers.gemspec +64 -0
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +76 -0
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer.rb +418 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +91 -0
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +90 -0
- data/lib/active_model/serializer/concerns/caching.rb +304 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +7 -0
- data/lib/active_model_serializers.rb +63 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/adapter/attributes.rb +15 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +90 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +52 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +12 -0
- data/lib/generators/rails/serializer_generator.rb +38 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +64 -0
- data/test/action_controller/explicit_serializer_test.rb +137 -0
- data/test/action_controller/json/include_test.rb +248 -0
- data/test/action_controller/json_api/deserialization_test.rb +114 -0
- data/test/action_controller/json_api/errors_test.rb +42 -0
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +204 -0
- data/test/action_controller/json_api/pagination_test.rb +126 -0
- data/test/action_controller/json_api/transform_test.rb +191 -0
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +239 -0
- data/test/action_controller/serialization_scope_name_test.rb +237 -0
- data/test/action_controller/serialization_test.rb +480 -0
- data/test/active_model_serializers/adapter_for_test.rb +210 -0
- data/test/active_model_serializers/json_pointer_test.rb +24 -0
- data/test/active_model_serializers/logging_test.rb +79 -0
- data/test/active_model_serializers/model_test.rb +144 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +70 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +73 -0
- data/test/active_model_serializers/test/schema_test.rb +133 -0
- data/test/active_model_serializers/test/serializer_test.rb +64 -0
- data/test/active_record_test.rb +11 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +102 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +106 -0
- data/test/adapter/json/has_many_test.rb +55 -0
- data/test/adapter/json/transform_test.rb +95 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +98 -0
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +98 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +175 -0
- data/test/adapter/json_api/has_one_test.rb +82 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +35 -0
- data/test/adapter/json_api/linked_test.rb +415 -0
- data/test/adapter/json_api/links_test.rb +112 -0
- data/test/adapter/json_api/pagination_links_test.rb +208 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/relationship_test.rb +399 -0
- data/test/adapter/json_api/resource_meta_test.rb +102 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_api/transform_test.rb +514 -0
- data/test/adapter/json_api/type_test.rb +195 -0
- data/test/adapter/json_test.rb +48 -0
- data/test/adapter/null_test.rb +24 -0
- data/test/adapter/polymorphic_test.rb +220 -0
- data/test/adapter_test.rb +69 -0
- data/test/array_serializer_test.rb +24 -0
- data/test/benchmark/app.rb +67 -0
- data/test/benchmark/benchmarking_support.rb +69 -0
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +121 -0
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +47 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +85 -0
- data/test/benchmark/fixtures.rb +221 -0
- data/test/cache_test.rb +717 -0
- data/test/collection_serializer_test.rb +129 -0
- data/test/fixtures/active_record.rb +115 -0
- data/test/fixtures/poro.rb +227 -0
- data/test/generators/scaffold_controller_generator_test.rb +26 -0
- data/test/generators/serializer_generator_test.rb +77 -0
- data/test/grape_test.rb +198 -0
- data/test/lint_test.rb +51 -0
- data/test/logger_test.rb +22 -0
- data/test/poro_test.rb +11 -0
- data/test/serializable_resource_test.rb +81 -0
- data/test/serializers/association_macros_test.rb +39 -0
- data/test/serializers/associations_test.rb +520 -0
- data/test/serializers/attribute_test.rb +155 -0
- data/test/serializers/attributes_test.rb +54 -0
- data/test/serializers/caching_configuration_test_isolated.rb +172 -0
- data/test/serializers/configuration_test.rb +34 -0
- data/test/serializers/fieldset_test.rb +16 -0
- data/test/serializers/meta_test.rb +204 -0
- data/test/serializers/options_test.rb +34 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +81 -0
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serialization_test.rb +57 -0
- data/test/serializers/serializer_for_test.rb +138 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +86 -0
- data/test/support/rails5_shims.rb +55 -0
- data/test/support/rails_app.rb +40 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +81 -0
- data/test/test_helper.rb +72 -0
- metadata +622 -0
@@ -0,0 +1,195 @@
|
|
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
|
@@ -0,0 +1,48 @@
|
|
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
|
@@ -0,0 +1,24 @@
|
|
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
|
@@ -0,0 +1,220 @@
|
|
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
|