active_model_serializers 0.10.6 → 0.10.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +114 -1
- data/README.md +21 -24
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer/adapter/attributes.rb +2 -0
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/adapter/json.rb +2 -0
- data/lib/active_model/serializer/adapter/json_api.rb +2 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -0
- data/lib/active_model/serializer/adapter.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +2 -0
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +2 -0
- data/lib/active_model/serializer/collection_serializer.rb +17 -5
- data/lib/active_model/serializer/concerns/caching.rb +7 -2
- data/lib/active_model/serializer/error_serializer.rb +2 -0
- data/lib/active_model/serializer/errors_serializer.rb +2 -0
- data/lib/active_model/serializer/field.rb +2 -0
- data/lib/active_model/serializer/fieldset.rb +3 -1
- data/lib/active_model/serializer/has_many_reflection.rb +2 -0
- data/lib/active_model/serializer/has_one_reflection.rb +2 -0
- data/lib/active_model/serializer/lazy_association.rb +6 -2
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +10 -5
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model/serializer.rb +37 -19
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +2 -0
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +3 -1
- data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +45 -20
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +18 -4
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +33 -25
- data/lib/active_model_serializers/adapter/json_api.rb +14 -9
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/callbacks.rb +2 -0
- data/lib/active_model_serializers/deprecate.rb +2 -0
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/json_pointer.rb +2 -0
- data/lib/active_model_serializers/logging.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +2 -0
- data/lib/active_model_serializers/model/caching.rb +26 -0
- data/lib/active_model_serializers/model.rb +2 -0
- data/lib/active_model_serializers/railtie.rb +4 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +2 -0
- data/lib/active_model_serializers/serializable_resource.rb +4 -2
- data/lib/active_model_serializers/serialization_context.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +2 -0
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/active_model_serializers/test.rb +2 -0
- data/lib/active_model_serializers.rb +21 -11
- data/lib/generators/rails/resource_override.rb +2 -0
- data/lib/generators/rails/serializer_generator.rb +2 -0
- data/lib/grape/active_model_serializers.rb +2 -0
- data/lib/grape/formatters/active_model_serializers.rb +2 -0
- data/lib/grape/helpers/active_model_serializers.rb +2 -0
- data/lib/tasks/rubocop.rake +2 -0
- metadata +43 -298
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -105
- data/.simplecov +0 -110
- data/.travis.yml +0 -55
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -74
- data/active_model_serializers.gemspec +0 -63
- data/appveyor.yml +0 -30
- 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 -263
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -169
- 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 -480
- 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 -55
- 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/schema.json +0 -366
- data/docs/jsonapi/schema.md +0 -151
- data/docs/rfcs/0000-namespace.md +0 -106
- data/docs/rfcs/template.md +0 -15
- data/test/action_controller/adapter_selector_test.rb +0 -62
- data/test/action_controller/explicit_serializer_test.rb +0 -135
- data/test/action_controller/json/include_test.rb +0 -246
- data/test/action_controller/json_api/deserialization_test.rb +0 -112
- data/test/action_controller/json_api/errors_test.rb +0 -40
- data/test/action_controller/json_api/fields_test.rb +0 -66
- data/test/action_controller/json_api/linked_test.rb +0 -202
- data/test/action_controller/json_api/pagination_test.rb +0 -116
- data/test/action_controller/json_api/transform_test.rb +0 -189
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -232
- data/test/action_controller/serialization_scope_name_test.rb +0 -235
- data/test/action_controller/serialization_test.rb +0 -472
- data/test/active_model_serializers/adapter_for_test.rb +0 -208
- data/test/active_model_serializers/json_pointer_test.rb +0 -22
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -142
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -68
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -161
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -131
- data/test/active_model_serializers/test/serializer_test.rb +0 -62
- data/test/active_record_test.rb +0 -9
- data/test/adapter/attributes_test.rb +0 -40
- data/test/adapter/deprecation_test.rb +0 -100
- data/test/adapter/json/belongs_to_test.rb +0 -45
- data/test/adapter/json/collection_test.rb +0 -104
- data/test/adapter/json/has_many_test.rb +0 -53
- data/test/adapter/json/transform_test.rb +0 -93
- data/test/adapter/json_api/belongs_to_test.rb +0 -155
- data/test/adapter/json_api/collection_test.rb +0 -96
- data/test/adapter/json_api/errors_test.rb +0 -76
- data/test/adapter/json_api/fields_test.rb +0 -96
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -96
- data/test/adapter/json_api/has_many_test.rb +0 -173
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -183
- data/test/adapter/json_api/json_api_test.rb +0 -33
- data/test/adapter/json_api/linked_test.rb +0 -413
- data/test/adapter/json_api/links_test.rb +0 -95
- data/test/adapter/json_api/pagination_links_test.rb +0 -193
- data/test/adapter/json_api/parse_test.rb +0 -137
- data/test/adapter/json_api/relationship_test.rb +0 -397
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
- data/test/adapter/json_api/resource_meta_test.rb +0 -100
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -82
- data/test/adapter/json_api/transform_test.rb +0 -512
- data/test/adapter/json_api/type_test.rb +0 -61
- data/test/adapter/json_test.rb +0 -46
- data/test/adapter/null_test.rb +0 -22
- data/test/adapter/polymorphic_test.rb +0 -171
- data/test/adapter_test.rb +0 -67
- data/test/array_serializer_test.rb +0 -22
- data/test/benchmark/app.rb +0 -65
- data/test/benchmark/benchmarking_support.rb +0 -67
- data/test/benchmark/bm_active_record.rb +0 -81
- data/test/benchmark/bm_adapter.rb +0 -38
- data/test/benchmark/bm_caching.rb +0 -119
- data/test/benchmark/bm_lookup_chain.rb +0 -83
- data/test/benchmark/bm_transform.rb +0 -45
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -83
- data/test/benchmark/fixtures.rb +0 -219
- data/test/cache_test.rb +0 -651
- data/test/collection_serializer_test.rb +0 -123
- data/test/fixtures/active_record.rb +0 -113
- data/test/fixtures/poro.rb +0 -225
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -75
- data/test/grape_test.rb +0 -196
- data/test/lint_test.rb +0 -49
- data/test/logger_test.rb +0 -20
- data/test/poro_test.rb +0 -9
- data/test/serializable_resource_test.rb +0 -79
- data/test/serializers/association_macros_test.rb +0 -37
- data/test/serializers/associations_test.rb +0 -424
- data/test/serializers/attribute_test.rb +0 -153
- data/test/serializers/attributes_test.rb +0 -52
- data/test/serializers/caching_configuration_test_isolated.rb +0 -170
- data/test/serializers/configuration_test.rb +0 -32
- data/test/serializers/fieldset_test.rb +0 -14
- data/test/serializers/meta_test.rb +0 -202
- data/test/serializers/options_test.rb +0 -32
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- data/test/serializers/reflection_test.rb +0 -427
- data/test/serializers/root_test.rb +0 -21
- data/test/serializers/serialization_test.rb +0 -55
- data/test/serializers/serializer_for_test.rb +0 -136
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -88
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -82
- data/test/support/rails5_shims.rb +0 -53
- data/test/support/rails_app.rb +0 -38
- 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 -71
- data/test/test_helper.rb +0 -70
@@ -1,427 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
module ActiveModel
|
3
|
-
class Serializer
|
4
|
-
class ReflectionTest < ActiveSupport::TestCase
|
5
|
-
class Blog < ActiveModelSerializers::Model
|
6
|
-
attributes :id
|
7
|
-
end
|
8
|
-
class BlogSerializer < ActiveModel::Serializer
|
9
|
-
type 'blog'
|
10
|
-
attributes :id
|
11
|
-
end
|
12
|
-
|
13
|
-
setup do
|
14
|
-
@expected_meta = { id: 1 }
|
15
|
-
@expected_links = { self: 'no_uri_validation' }
|
16
|
-
@empty_links = {}
|
17
|
-
model_attributes = { blog: Blog.new(@expected_meta) }
|
18
|
-
@model = Class.new(ActiveModelSerializers::Model) do
|
19
|
-
attributes(*model_attributes.keys)
|
20
|
-
|
21
|
-
def self.name
|
22
|
-
'TestModel'
|
23
|
-
end
|
24
|
-
end.new(model_attributes)
|
25
|
-
@instance_options = {}
|
26
|
-
end
|
27
|
-
|
28
|
-
def evaluate_association_value(association)
|
29
|
-
association.lazy_association.eval_reflection_block
|
30
|
-
end
|
31
|
-
|
32
|
-
# TODO: Remaining tests
|
33
|
-
# test_reflection_value_block_with_scope
|
34
|
-
# test_reflection_value_uses_serializer_instance_method
|
35
|
-
# test_reflection_excluded_eh_blank_is_false
|
36
|
-
# test_reflection_excluded_eh_if
|
37
|
-
# test_reflection_excluded_eh_unless
|
38
|
-
# test_evaluate_condition_symbol_serializer_method
|
39
|
-
# test_evaluate_condition_string_serializer_method
|
40
|
-
# test_evaluate_condition_proc
|
41
|
-
# test_evaluate_condition_proc_yields_serializer
|
42
|
-
# test_evaluate_condition_other
|
43
|
-
# test_options_key
|
44
|
-
# test_options_polymorphic
|
45
|
-
# test_options_serializer
|
46
|
-
# test_options_virtual_value
|
47
|
-
# test_options_namespace
|
48
|
-
|
49
|
-
def test_reflection_value
|
50
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
51
|
-
has_one :blog
|
52
|
-
end
|
53
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
54
|
-
|
55
|
-
# Get Reflection
|
56
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
57
|
-
|
58
|
-
# Assert
|
59
|
-
assert_nil reflection.block
|
60
|
-
assert_equal Serializer.config.include_data_default, reflection.options.fetch(:include_data_setting)
|
61
|
-
assert_equal true, reflection.options.fetch(:include_data_setting)
|
62
|
-
|
63
|
-
include_slice = :does_not_matter
|
64
|
-
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
65
|
-
end
|
66
|
-
|
67
|
-
def test_reflection_value_block
|
68
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
69
|
-
has_one :blog do
|
70
|
-
object.blog
|
71
|
-
end
|
72
|
-
end
|
73
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
74
|
-
|
75
|
-
# Get Reflection
|
76
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
77
|
-
|
78
|
-
# Assert
|
79
|
-
assert_respond_to reflection.block, :call
|
80
|
-
assert_equal Serializer.config.include_data_default, reflection.options.fetch(:include_data_setting)
|
81
|
-
assert_equal true, reflection.options.fetch(:include_data_setting)
|
82
|
-
|
83
|
-
include_slice = :does_not_matter
|
84
|
-
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_reflection_value_block_with_explicit_include_data_true
|
88
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
89
|
-
has_one :blog do
|
90
|
-
include_data true
|
91
|
-
object.blog
|
92
|
-
end
|
93
|
-
end
|
94
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
95
|
-
|
96
|
-
# Get Reflection
|
97
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
98
|
-
|
99
|
-
# Assert
|
100
|
-
assert_respond_to reflection.block, :call
|
101
|
-
assert_equal Serializer.config.include_data_default, reflection.options.fetch(:include_data_setting)
|
102
|
-
assert_equal true, reflection.options.fetch(:include_data_setting)
|
103
|
-
|
104
|
-
include_slice = :does_not_matter
|
105
|
-
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_reflection_value_block_with_include_data_false_mutates_the_reflection_include_data
|
109
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
110
|
-
has_one :blog do
|
111
|
-
include_data false
|
112
|
-
object.blog
|
113
|
-
end
|
114
|
-
end
|
115
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
116
|
-
|
117
|
-
# Get Reflection
|
118
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
119
|
-
|
120
|
-
# Assert
|
121
|
-
assert_respond_to reflection.block, :call
|
122
|
-
assert_equal true, reflection.options.fetch(:include_data_setting)
|
123
|
-
include_slice = :does_not_matter
|
124
|
-
assert_nil reflection.send(:value, serializer_instance, include_slice)
|
125
|
-
assert_equal false, reflection.options.fetch(:include_data_setting)
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_reflection_value_block_with_include_data_if_sideloaded_included_mutates_the_reflection_include_data
|
129
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
130
|
-
has_one :blog do
|
131
|
-
include_data :if_sideloaded
|
132
|
-
object.blog
|
133
|
-
end
|
134
|
-
end
|
135
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
136
|
-
|
137
|
-
# Get Reflection
|
138
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
139
|
-
|
140
|
-
# Assert
|
141
|
-
assert_respond_to reflection.block, :call
|
142
|
-
assert_equal true, reflection.options.fetch(:include_data_setting)
|
143
|
-
include_slice = {}
|
144
|
-
assert_nil reflection.send(:value, serializer_instance, include_slice)
|
145
|
-
assert_equal :if_sideloaded, reflection.options.fetch(:include_data_setting)
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_reflection_value_block_with_include_data_if_sideloaded_excluded_mutates_the_reflection_include_data
|
149
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
150
|
-
has_one :blog do
|
151
|
-
include_data :if_sideloaded
|
152
|
-
object.blog
|
153
|
-
end
|
154
|
-
end
|
155
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
156
|
-
|
157
|
-
# Get Reflection
|
158
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
159
|
-
|
160
|
-
# Assert
|
161
|
-
assert_respond_to reflection.block, :call
|
162
|
-
assert_equal true, reflection.options.fetch(:include_data_setting)
|
163
|
-
include_slice = { blog: :does_not_matter }
|
164
|
-
assert_equal @model.blog, reflection.send(:value, serializer_instance, include_slice)
|
165
|
-
assert_equal :if_sideloaded, reflection.options.fetch(:include_data_setting)
|
166
|
-
end
|
167
|
-
|
168
|
-
def test_reflection_block_with_link_mutates_the_reflection_links
|
169
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
170
|
-
has_one :blog do
|
171
|
-
link :self, 'no_uri_validation'
|
172
|
-
end
|
173
|
-
end
|
174
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
175
|
-
|
176
|
-
# Get Reflection
|
177
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
178
|
-
assert_equal @empty_links, reflection.options.fetch(:links)
|
179
|
-
|
180
|
-
# Build Association
|
181
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
182
|
-
|
183
|
-
# Assert association links empty when not yet evaluated
|
184
|
-
assert_equal @empty_links, reflection.options.fetch(:links)
|
185
|
-
assert_equal @empty_links, association.links
|
186
|
-
|
187
|
-
evaluate_association_value(association)
|
188
|
-
|
189
|
-
assert_equal @expected_links, association.links
|
190
|
-
assert_equal @expected_links, reflection.options.fetch(:links)
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_reflection_block_with_link_block_mutates_the_reflection_links
|
194
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
195
|
-
has_one :blog do
|
196
|
-
link :self do
|
197
|
-
'no_uri_validation'
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
201
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
202
|
-
|
203
|
-
# Get Reflection
|
204
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
205
|
-
assert_equal @empty_links, reflection.options.fetch(:links)
|
206
|
-
|
207
|
-
# Build Association
|
208
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
209
|
-
|
210
|
-
# Assert association links empty when not yet evaluated
|
211
|
-
assert_equal @empty_links, association.links
|
212
|
-
|
213
|
-
evaluate_association_value(association)
|
214
|
-
|
215
|
-
# Assert before instance_eval link
|
216
|
-
link = association.links.fetch(:self)
|
217
|
-
assert_respond_to link, :call
|
218
|
-
assert_respond_to reflection.options.fetch(:links).fetch(:self), :call
|
219
|
-
|
220
|
-
# Assert after instance_eval link
|
221
|
-
assert_equal @expected_links.fetch(:self), reflection.instance_eval(&link)
|
222
|
-
assert_respond_to reflection.options.fetch(:links).fetch(:self), :call
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_reflection_block_with_meta_mutates_the_reflection_meta
|
226
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
227
|
-
has_one :blog do
|
228
|
-
meta(id: object.blog.id)
|
229
|
-
end
|
230
|
-
end
|
231
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
232
|
-
|
233
|
-
# Get Reflection
|
234
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
235
|
-
assert_nil reflection.options.fetch(:meta)
|
236
|
-
|
237
|
-
# Build Association
|
238
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
239
|
-
|
240
|
-
evaluate_association_value(association)
|
241
|
-
|
242
|
-
assert_equal @expected_meta, association.meta
|
243
|
-
assert_equal @expected_meta, reflection.options.fetch(:meta)
|
244
|
-
end
|
245
|
-
|
246
|
-
def test_reflection_block_with_meta_block_mutates_the_reflection_meta
|
247
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
248
|
-
has_one :blog do
|
249
|
-
meta do
|
250
|
-
{ id: object.blog.id }
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
255
|
-
|
256
|
-
# Get Reflection
|
257
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
258
|
-
assert_nil reflection.options.fetch(:meta)
|
259
|
-
|
260
|
-
# Build Association
|
261
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
262
|
-
# Assert before instance_eval meta
|
263
|
-
|
264
|
-
evaluate_association_value(association)
|
265
|
-
|
266
|
-
assert_respond_to association.meta, :call
|
267
|
-
assert_respond_to reflection.options.fetch(:meta), :call
|
268
|
-
|
269
|
-
# Assert after instance_eval meta
|
270
|
-
assert_equal @expected_meta, reflection.instance_eval(&association.meta)
|
271
|
-
assert_respond_to reflection.options.fetch(:meta), :call
|
272
|
-
assert_respond_to association.meta, :call
|
273
|
-
end
|
274
|
-
|
275
|
-
# rubocop:disable Metrics/AbcSize
|
276
|
-
def test_reflection_block_with_meta_in_link_block_mutates_the_reflection_meta
|
277
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
278
|
-
has_one :blog do
|
279
|
-
link :self do
|
280
|
-
meta(id: object.blog.id)
|
281
|
-
'no_uri_validation'
|
282
|
-
end
|
283
|
-
end
|
284
|
-
end
|
285
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
286
|
-
|
287
|
-
# Get Reflection
|
288
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
289
|
-
assert_nil reflection.options.fetch(:meta)
|
290
|
-
assert_equal @empty_links, reflection.options.fetch(:links)
|
291
|
-
|
292
|
-
# Build Association
|
293
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
294
|
-
# Assert before instance_eval link meta
|
295
|
-
assert_nil association.meta
|
296
|
-
assert_nil reflection.options.fetch(:meta)
|
297
|
-
|
298
|
-
evaluate_association_value(association)
|
299
|
-
|
300
|
-
link = association.links.fetch(:self)
|
301
|
-
assert_respond_to link, :call
|
302
|
-
assert_respond_to reflection.options.fetch(:links).fetch(:self), :call
|
303
|
-
assert_nil reflection.options.fetch(:meta)
|
304
|
-
|
305
|
-
# Assert after instance_eval link
|
306
|
-
assert_equal 'no_uri_validation', reflection.instance_eval(&link)
|
307
|
-
assert_equal @expected_meta, reflection.options.fetch(:meta)
|
308
|
-
assert_equal @expected_meta, association.meta
|
309
|
-
end
|
310
|
-
# rubocop:enable Metrics/AbcSize
|
311
|
-
|
312
|
-
# rubocop:disable Metrics/AbcSize
|
313
|
-
def test_reflection_block_with_meta_block_in_link_block_mutates_the_reflection_meta
|
314
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
315
|
-
has_one :blog do
|
316
|
-
link :self do
|
317
|
-
meta do
|
318
|
-
{ id: object.blog.id }
|
319
|
-
end
|
320
|
-
'no_uri_validation'
|
321
|
-
end
|
322
|
-
end
|
323
|
-
end
|
324
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
325
|
-
|
326
|
-
# Get Reflection
|
327
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
328
|
-
assert_nil reflection.options.fetch(:meta)
|
329
|
-
|
330
|
-
# Build Association
|
331
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
332
|
-
assert_nil association.meta
|
333
|
-
assert_nil reflection.options.fetch(:meta)
|
334
|
-
|
335
|
-
# Assert before instance_eval link
|
336
|
-
|
337
|
-
evaluate_association_value(association)
|
338
|
-
|
339
|
-
link = association.links.fetch(:self)
|
340
|
-
assert_nil reflection.options.fetch(:meta)
|
341
|
-
assert_respond_to link, :call
|
342
|
-
assert_respond_to association.links.fetch(:self), :call
|
343
|
-
|
344
|
-
# Assert after instance_eval link
|
345
|
-
assert_equal 'no_uri_validation', reflection.instance_eval(&link)
|
346
|
-
assert_respond_to association.links.fetch(:self), :call
|
347
|
-
# Assert before instance_eval link meta
|
348
|
-
assert_respond_to reflection.options.fetch(:meta), :call
|
349
|
-
assert_respond_to association.meta, :call
|
350
|
-
|
351
|
-
# Assert after instance_eval link meta
|
352
|
-
assert_equal @expected_meta, reflection.instance_eval(&reflection.options.fetch(:meta))
|
353
|
-
assert_respond_to association.meta, :call
|
354
|
-
end
|
355
|
-
# rubocop:enable Metrics/AbcSize
|
356
|
-
|
357
|
-
def test_no_href_in_vanilla_reflection
|
358
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
359
|
-
has_one :blog do
|
360
|
-
link :self do
|
361
|
-
href 'no_uri_validation'
|
362
|
-
end
|
363
|
-
end
|
364
|
-
end
|
365
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
366
|
-
|
367
|
-
# Get Reflection
|
368
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
369
|
-
assert_equal @empty_links, reflection.options.fetch(:links)
|
370
|
-
|
371
|
-
# Build Association
|
372
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
373
|
-
# Assert before instance_eval link
|
374
|
-
|
375
|
-
evaluate_association_value(association)
|
376
|
-
|
377
|
-
link = association.links.fetch(:self)
|
378
|
-
assert_respond_to link, :call
|
379
|
-
|
380
|
-
# Assert after instance_eval link
|
381
|
-
exception = assert_raise(NoMethodError) do
|
382
|
-
reflection.instance_eval(&link)
|
383
|
-
end
|
384
|
-
assert_match(/undefined method `href'/, exception.message)
|
385
|
-
end
|
386
|
-
|
387
|
-
# rubocop:disable Metrics/AbcSize
|
388
|
-
def test_mutating_reflection_block_is_not_thread_safe
|
389
|
-
serializer_class = Class.new(ActiveModel::Serializer) do
|
390
|
-
has_one :blog do
|
391
|
-
meta(id: object.blog.id)
|
392
|
-
end
|
393
|
-
end
|
394
|
-
model1_meta = @expected_meta
|
395
|
-
# Evaluate reflection meta for model with id 1
|
396
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
397
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
398
|
-
assert_nil reflection.options.fetch(:meta)
|
399
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
400
|
-
|
401
|
-
evaluate_association_value(association)
|
402
|
-
|
403
|
-
assert_equal model1_meta, association.meta
|
404
|
-
assert_equal model1_meta, reflection.options.fetch(:meta)
|
405
|
-
|
406
|
-
model2_meta = @expected_meta.merge(id: 2)
|
407
|
-
# Evaluate reflection meta for model with id 2
|
408
|
-
@model.blog.id = 2
|
409
|
-
assert_equal 2, @model.blog.id # sanity check
|
410
|
-
serializer_instance = serializer_class.new(@model, @instance_options)
|
411
|
-
reflection = serializer_class._reflections.fetch(:blog)
|
412
|
-
|
413
|
-
# WARN: Thread-safety issue
|
414
|
-
# Before the reflection is evaluated, it has the value from the previous evaluation
|
415
|
-
assert_equal model1_meta, reflection.options.fetch(:meta)
|
416
|
-
|
417
|
-
association = reflection.build_association(serializer_instance, @instance_options)
|
418
|
-
|
419
|
-
evaluate_association_value(association)
|
420
|
-
|
421
|
-
assert_equal model2_meta, association.meta
|
422
|
-
assert_equal model2_meta, reflection.options.fetch(:meta)
|
423
|
-
end
|
424
|
-
# rubocop:enable Metrics/AbcSize
|
425
|
-
end
|
426
|
-
end
|
427
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class RootTest < ActiveSupport::TestCase
|
6
|
-
def setup
|
7
|
-
@virtual_value = VirtualValue.new(id: 1)
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_overwrite_root
|
11
|
-
serializer = VirtualValueSerializer.new(@virtual_value, root: 'smth')
|
12
|
-
assert_equal('smth', serializer.json_key)
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_underscore_in_root
|
16
|
-
serializer = VirtualValueSerializer.new(@virtual_value)
|
17
|
-
assert_equal('virtual_value', serializer.json_key)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
class SerializationTest < ActiveSupport::TestCase
|
4
|
-
class Blog < ActiveModelSerializers::Model
|
5
|
-
attributes :id, :name, :authors
|
6
|
-
end
|
7
|
-
class Author < ActiveModelSerializers::Model
|
8
|
-
attributes :id, :name
|
9
|
-
end
|
10
|
-
class BlogSerializer < ActiveModel::Serializer
|
11
|
-
attributes :id
|
12
|
-
attribute :name, key: :title
|
13
|
-
|
14
|
-
has_many :authors
|
15
|
-
end
|
16
|
-
class AuthorSerializer < ActiveModel::Serializer
|
17
|
-
attributes :id, :name
|
18
|
-
end
|
19
|
-
|
20
|
-
setup do
|
21
|
-
@authors = [Author.new(id: 1, name: 'Blog Author')]
|
22
|
-
@blog = Blog.new(id: 2, name: 'The Blog', authors: @authors)
|
23
|
-
@serializer_instance = BlogSerializer.new(@blog)
|
24
|
-
@serializable = ActiveModelSerializers::SerializableResource.new(@blog, serializer: BlogSerializer, adapter: :attributes)
|
25
|
-
@expected_hash = { id: 2, title: 'The Blog', authors: [{ id: 1, name: 'Blog Author' }] }
|
26
|
-
@expected_json = '{"id":2,"title":"The Blog","authors":[{"id":1,"name":"Blog Author"}]}'
|
27
|
-
end
|
28
|
-
|
29
|
-
test '#serializable_hash is the same as generated by the attributes adapter' do
|
30
|
-
assert_equal @serializable.serializable_hash, @serializer_instance.serializable_hash
|
31
|
-
assert_equal @expected_hash, @serializer_instance.serializable_hash
|
32
|
-
end
|
33
|
-
|
34
|
-
test '#as_json is the same as generated by the attributes adapter' do
|
35
|
-
assert_equal @serializable.as_json, @serializer_instance.as_json
|
36
|
-
assert_equal @expected_hash, @serializer_instance.as_json
|
37
|
-
end
|
38
|
-
|
39
|
-
test '#to_json is the same as generated by the attributes adapter' do
|
40
|
-
assert_equal @serializable.to_json, @serializer_instance.to_json
|
41
|
-
assert_equal @expected_json, @serializer_instance.to_json
|
42
|
-
end
|
43
|
-
|
44
|
-
test '#to_h is an alias for #serializable_hash' do
|
45
|
-
assert_equal @serializable.serializable_hash, @serializer_instance.to_h
|
46
|
-
assert_equal @expected_hash, @serializer_instance.to_h
|
47
|
-
end
|
48
|
-
|
49
|
-
test '#to_hash is an alias for #serializable_hash' do
|
50
|
-
assert_equal @serializable.serializable_hash, @serializer_instance.to_hash
|
51
|
-
assert_equal @expected_hash, @serializer_instance.to_hash
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
@@ -1,136 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class SerializerForTest < ActiveSupport::TestCase
|
6
|
-
class CollectionSerializerTest < ActiveSupport::TestCase
|
7
|
-
def setup
|
8
|
-
@array = [1, 2, 3]
|
9
|
-
@previous_collection_serializer = ActiveModelSerializers.config.collection_serializer
|
10
|
-
end
|
11
|
-
|
12
|
-
def teardown
|
13
|
-
ActiveModelSerializers.config.collection_serializer = @previous_collection_serializer
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_serializer_for_array
|
17
|
-
serializer = ActiveModel::Serializer.serializer_for(@array)
|
18
|
-
assert_equal ActiveModelSerializers.config.collection_serializer, serializer
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_overwritten_serializer_for_array
|
22
|
-
new_collection_serializer = Class.new
|
23
|
-
ActiveModelSerializers.config.collection_serializer = new_collection_serializer
|
24
|
-
serializer = ActiveModel::Serializer.serializer_for(@array)
|
25
|
-
assert_equal new_collection_serializer, serializer
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class SerializerTest < ActiveSupport::TestCase
|
30
|
-
module ResourceNamespace
|
31
|
-
class Post < ::Model; end
|
32
|
-
class Comment < ::Model; end
|
33
|
-
|
34
|
-
class PostSerializer < ActiveModel::Serializer
|
35
|
-
class CommentSerializer < ActiveModel::Serializer
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
class MyProfile < Profile
|
41
|
-
end
|
42
|
-
|
43
|
-
class CustomProfile
|
44
|
-
def serializer_class
|
45
|
-
ProfileSerializer
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
class Tweet < ::Model; end
|
50
|
-
TweetSerializer = Class.new
|
51
|
-
|
52
|
-
def setup
|
53
|
-
@profile = Profile.new
|
54
|
-
@my_profile = MyProfile.new
|
55
|
-
@custom_profile = CustomProfile.new
|
56
|
-
@model = ::Model.new
|
57
|
-
@tweet = Tweet.new
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_serializer_for_non_ams_serializer
|
61
|
-
serializer = ActiveModel::Serializer.serializer_for(@tweet)
|
62
|
-
assert_nil serializer
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_serializer_for_existing_serializer
|
66
|
-
serializer = ActiveModel::Serializer.serializer_for(@profile)
|
67
|
-
assert_equal ProfileSerializer, serializer
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_serializer_for_existing_serializer_with_lookup_disabled
|
71
|
-
serializer = with_serializer_lookup_disabled do
|
72
|
-
ActiveModel::Serializer.serializer_for(@profile)
|
73
|
-
end
|
74
|
-
assert_nil serializer
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_serializer_for_not_existing_serializer
|
78
|
-
serializer = ActiveModel::Serializer.serializer_for(@model)
|
79
|
-
assert_nil serializer
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_serializer_inherited_serializer
|
83
|
-
serializer = ActiveModel::Serializer.serializer_for(@my_profile)
|
84
|
-
assert_equal ProfileSerializer, serializer
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_serializer_inherited_serializer_with_lookup_disabled
|
88
|
-
serializer = with_serializer_lookup_disabled do
|
89
|
-
ActiveModel::Serializer.serializer_for(@my_profile)
|
90
|
-
end
|
91
|
-
assert_nil serializer
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_serializer_custom_serializer
|
95
|
-
serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
|
96
|
-
assert_equal ProfileSerializer, serializer
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_serializer_custom_serializer_with_lookup_disabled
|
100
|
-
serializer = with_serializer_lookup_disabled do
|
101
|
-
ActiveModel::Serializer.serializer_for(@custom_profile)
|
102
|
-
end
|
103
|
-
assert_equal ProfileSerializer, serializer
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_serializer_for_namespaced_resource
|
107
|
-
post = ResourceNamespace::Post.new
|
108
|
-
serializer = ActiveModel::Serializer.serializer_for(post)
|
109
|
-
assert_equal ResourceNamespace::PostSerializer, serializer
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_serializer_for_namespaced_resource_with_lookup_disabled
|
113
|
-
post = ResourceNamespace::Post.new
|
114
|
-
serializer = with_serializer_lookup_disabled do
|
115
|
-
ActiveModel::Serializer.serializer_for(post)
|
116
|
-
end
|
117
|
-
assert_nil serializer
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_serializer_for_nested_resource
|
121
|
-
comment = ResourceNamespace::Comment.new
|
122
|
-
serializer = ResourceNamespace::PostSerializer.serializer_for(comment)
|
123
|
-
assert_equal ResourceNamespace::PostSerializer::CommentSerializer, serializer
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_serializer_for_nested_resource_with_lookup_disabled
|
127
|
-
comment = ResourceNamespace::Comment.new
|
128
|
-
serializer = with_serializer_lookup_disabled do
|
129
|
-
ResourceNamespace::PostSerializer.serializer_for(comment)
|
130
|
-
end
|
131
|
-
assert_nil serializer
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|