active_model_serializers 0.10.3 → 0.10.12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +154 -2
- data/README.md +153 -15
- data/lib/action_controller/serialization.rb +11 -1
- data/lib/active_model/serializable_resource.rb +2 -0
- data/lib/active_model/serializer.rb +275 -81
- data/lib/active_model/serializer/adapter.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/array_serializer.rb +2 -0
- data/lib/active_model/serializer/association.rb +53 -14
- data/lib/active_model/serializer/attribute.rb +2 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +7 -1
- data/lib/active_model/serializer/collection_serializer.rb +8 -5
- data/lib/active_model/serializer/concerns/caching.rb +36 -23
- 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 +6 -1
- data/lib/active_model/serializer/has_one_reflection.rb +3 -1
- 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 +2 -0
- data/lib/active_model/serializer/null.rb +2 -0
- data/lib/active_model/serializer/reflection.rb +122 -73
- data/lib/active_model/serializer/version.rb +3 -1
- data/lib/active_model_serializers.rb +29 -11
- data/lib/active_model_serializers/adapter.rb +3 -1
- data/lib/active_model_serializers/adapter/attributes.rb +23 -0
- data/lib/active_model_serializers/adapter/base.rb +4 -2
- data/lib/active_model_serializers/adapter/json.rb +2 -0
- data/lib/active_model_serializers/adapter/json_api.rb +44 -26
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
- 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 +42 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -9
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +35 -18
- data/lib/active_model_serializers/adapter/null.rb +2 -0
- 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.rb +111 -30
- data/lib/active_model_serializers/model/caching.rb +25 -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.rb +2 -0
- data/lib/active_model_serializers/test/schema.rb +4 -2
- data/lib/active_model_serializers/test/serializer.rb +2 -0
- data/lib/generators/rails/resource_override.rb +3 -1
- 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 +55 -0
- metadata +74 -291
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -102
- data/.simplecov +0 -110
- data/.travis.yml +0 -51
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -56
- data/Rakefile +0 -103
- data/active_model_serializers.gemspec +0 -62
- data/appveyor.yml +0 -24
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/serve_benchmark +0 -39
- data/docs/ARCHITECTURE.md +0 -125
- data/docs/README.md +0 -42
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -247
- 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 -14
- data/docs/general/rendering.md +0 -294
- data/docs/general/serializers.md +0 -461
- 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 -137
- 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 -65
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -32
- 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 -144
- 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/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/concerns/associations.rb +0 -102
- data/lib/active_model/serializer/concerns/attributes.rb +0 -82
- data/lib/active_model/serializer/concerns/configuration.rb +0 -59
- data/lib/active_model/serializer/concerns/links.rb +0 -35
- data/lib/active_model/serializer/concerns/meta.rb +0 -29
- data/lib/active_model/serializer/concerns/type.rb +0 -25
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model_serializers/key_transform.rb +0 -74
- data/test/action_controller/adapter_selector_test.rb +0 -53
- 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 -57
- 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 -181
- data/test/action_controller/lookup_proc_test.rb +0 -49
- data/test/action_controller/namespace_lookup_test.rb +0 -226
- data/test/action_controller/serialization_scope_name_test.rb +0 -229
- 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/key_transform_test.rb +0 -297
- data/test/active_model_serializers/logging_test.rb +0 -77
- data/test/active_model_serializers/model_test.rb +0 -22
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -63
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -143
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -71
- data/test/active_model_serializers/test/schema_test.rb +0 -130
- 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 -43
- 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 -45
- 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 -88
- 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 -165
- data/test/adapter/json_api/has_one_test.rb +0 -80
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -166
- 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 -504
- 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 -579
- data/test/collection_serializer_test.rb +0 -110
- data/test/fixtures/active_record.rb +0 -78
- data/test/fixtures/poro.rb +0 -286
- data/test/generators/scaffold_controller_generator_test.rb +0 -24
- data/test/generators/serializer_generator_test.rb +0 -74
- data/test/grape_test.rb +0 -178
- 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 -370
- data/test/serializers/attribute_test.rb +0 -151
- 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 -21
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -79
- 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 -87
- 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 -36
- 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 -58
@@ -1,21 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class OptionsTest < ActiveSupport::TestCase
|
6
|
-
def setup
|
7
|
-
@profile = Profile.new(name: 'Name 1', description: 'Description 1')
|
8
|
-
end
|
9
|
-
|
10
|
-
def test_options_are_accessible
|
11
|
-
@profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
|
12
|
-
assert @profile_serializer.arguments_passed_in?
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_no_option_is_passed_in
|
16
|
-
@profile_serializer = ProfileSerializer.new(@profile)
|
17
|
-
refute @profile_serializer.arguments_passed_in?
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class ReadAttributeForSerializationTest < ActiveSupport::TestCase
|
6
|
-
# https://github.com/rails-api/active_model_serializers/issues/1653
|
7
|
-
class Parent < ActiveModelSerializers::Model
|
8
|
-
attr_accessor :id
|
9
|
-
end
|
10
|
-
class Child < Parent
|
11
|
-
attr_accessor :name
|
12
|
-
end
|
13
|
-
class ParentSerializer < ActiveModel::Serializer
|
14
|
-
attributes :$id
|
15
|
-
|
16
|
-
define_method(:$id) do
|
17
|
-
object.id
|
18
|
-
end
|
19
|
-
end
|
20
|
-
class ChildSerializer < ParentSerializer
|
21
|
-
attributes :name
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_child_serializer_calls_dynamic_method_in_parent_serializer
|
25
|
-
parent = ParentSerializer.new(Parent.new(id: 5))
|
26
|
-
child = ChildSerializer.new(Child.new(id: 6, name: 'Child'))
|
27
|
-
assert_equal 5, parent.read_attribute_for_serialization(:$id)
|
28
|
-
assert_equal 6, child.read_attribute_for_serialization(:$id)
|
29
|
-
end
|
30
|
-
|
31
|
-
# https://github.com/rails-api/active_model_serializers/issues/1658
|
32
|
-
class ErrorResponse < ActiveModelSerializers::Model
|
33
|
-
attr_accessor :error
|
34
|
-
end
|
35
|
-
class ApplicationSerializer < ActiveModel::Serializer
|
36
|
-
attributes :status
|
37
|
-
|
38
|
-
def status
|
39
|
-
object.try(:errors).blank? && object.try(:error).blank?
|
40
|
-
end
|
41
|
-
end
|
42
|
-
class ErrorResponseSerializer < ApplicationSerializer
|
43
|
-
attributes :error
|
44
|
-
end
|
45
|
-
class ErrorResponseWithSuperSerializer < ApplicationSerializer
|
46
|
-
attributes :error
|
47
|
-
|
48
|
-
def success
|
49
|
-
super
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_child_serializer_with_error_attribute
|
54
|
-
error = ErrorResponse.new(error: 'i have an error')
|
55
|
-
serializer = ErrorResponseSerializer.new(error)
|
56
|
-
serializer_with_super = ErrorResponseWithSuperSerializer.new(error)
|
57
|
-
assert_equal false, serializer.read_attribute_for_serialization(:status)
|
58
|
-
assert_equal false, serializer_with_super.read_attribute_for_serialization(:status)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_child_serializer_with_errors
|
62
|
-
error = ErrorResponse.new
|
63
|
-
error.errors.add(:invalid, 'i am not valid')
|
64
|
-
serializer = ErrorResponseSerializer.new(error)
|
65
|
-
serializer_with_super = ErrorResponseWithSuperSerializer.new(error)
|
66
|
-
assert_equal false, serializer.read_attribute_for_serialization(:status)
|
67
|
-
assert_equal false, serializer_with_super.read_attribute_for_serialization(:status)
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_child_serializer_no_error_attribute_or_errors
|
71
|
-
error = ErrorResponse.new
|
72
|
-
serializer = ErrorResponseSerializer.new(error)
|
73
|
-
serializer_with_super = ErrorResponseWithSuperSerializer.new(error)
|
74
|
-
assert_equal true, serializer.read_attribute_for_serialization(:status)
|
75
|
-
assert_equal true, serializer_with_super.read_attribute_for_serialization(:status)
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
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
|
-
attr_accessor :id, :name, :authors
|
6
|
-
end
|
7
|
-
class Author < ActiveModelSerializers::Model
|
8
|
-
attr_accessor :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_equal 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_equal nil, serializer
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_serializer_for_not_existing_serializer
|
78
|
-
serializer = ActiveModel::Serializer.serializer_for(@model)
|
79
|
-
assert_equal 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_equal 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_equal 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_equal nil, serializer
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
end
|
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModel
|
4
|
-
class Serializer
|
5
|
-
class SerializerForWithNamespaceTest < ActiveSupport::TestCase
|
6
|
-
class Book < ::Model; end
|
7
|
-
class Page < ::Model; end
|
8
|
-
class Publisher < ::Model; end
|
9
|
-
|
10
|
-
module Api
|
11
|
-
module V3
|
12
|
-
class BookSerializer < ActiveModel::Serializer
|
13
|
-
attributes :title, :author_name
|
14
|
-
|
15
|
-
has_many :pages
|
16
|
-
belongs_to :publisher
|
17
|
-
end
|
18
|
-
|
19
|
-
class PageSerializer < ActiveModel::Serializer
|
20
|
-
attributes :number, :text
|
21
|
-
|
22
|
-
belongs_to :book
|
23
|
-
end
|
24
|
-
|
25
|
-
class PublisherSerializer < ActiveModel::Serializer
|
26
|
-
attributes :name
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
class BookSerializer < ActiveModel::Serializer
|
32
|
-
attributes :title, :author_name
|
33
|
-
end
|
34
|
-
test 'resource without a namespace' do
|
35
|
-
book = Book.new(title: 'A Post', author_name: 'hello')
|
36
|
-
|
37
|
-
# TODO: this should be able to pull up this serializer without explicitly specifying the serializer
|
38
|
-
# currently, with no options, it still uses the Api::V3 serializer
|
39
|
-
result = ActiveModelSerializers::SerializableResource.new(book, serializer: BookSerializer).serializable_hash
|
40
|
-
|
41
|
-
expected = { title: 'A Post', author_name: 'hello' }
|
42
|
-
assert_equal expected, result
|
43
|
-
end
|
44
|
-
|
45
|
-
test 'resource with namespace' do
|
46
|
-
book = Book.new(title: 'A Post', author_name: 'hi')
|
47
|
-
|
48
|
-
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
49
|
-
|
50
|
-
expected = { title: 'A Post', author_name: 'hi', pages: nil, publisher: nil }
|
51
|
-
assert_equal expected, result
|
52
|
-
end
|
53
|
-
|
54
|
-
test 'has_many with nested serializer under the namespace' do
|
55
|
-
page = Page.new(number: 1, text: 'hello')
|
56
|
-
book = Book.new(title: 'A Post', author_name: 'hi', pages: [page])
|
57
|
-
|
58
|
-
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
59
|
-
|
60
|
-
expected = {
|
61
|
-
title: 'A Post', author_name: 'hi',
|
62
|
-
publisher: nil,
|
63
|
-
pages: [{
|
64
|
-
number: 1, text: 'hello'
|
65
|
-
}]
|
66
|
-
}
|
67
|
-
assert_equal expected, result
|
68
|
-
end
|
69
|
-
|
70
|
-
test 'belongs_to with nested serializer under the namespace' do
|
71
|
-
publisher = Publisher.new(name: 'Disney')
|
72
|
-
book = Book.new(title: 'A Post', author_name: 'hi', publisher: publisher)
|
73
|
-
|
74
|
-
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
75
|
-
|
76
|
-
expected = {
|
77
|
-
title: 'A Post', author_name: 'hi',
|
78
|
-
pages: nil,
|
79
|
-
publisher: {
|
80
|
-
name: 'Disney'
|
81
|
-
}
|
82
|
-
}
|
83
|
-
assert_equal expected, result
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
# https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
|
2
|
-
|
3
|
-
# Usage Example:
|
4
|
-
#
|
5
|
-
# require 'support/isolated_unit'
|
6
|
-
#
|
7
|
-
# class RailtieTest < ActiveSupport::TestCase
|
8
|
-
# include ActiveSupport::Testing::Isolation
|
9
|
-
#
|
10
|
-
# class WithRailsDefinedOnLoad < RailtieTest
|
11
|
-
# setup do
|
12
|
-
# require 'rails'
|
13
|
-
# require 'active_model_serializers'
|
14
|
-
# make_basic_app
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# # some tests
|
18
|
-
# end
|
19
|
-
#
|
20
|
-
# class WithoutRailsDefinedOnLoad < RailtieTest
|
21
|
-
# setup do
|
22
|
-
# require 'active_model_serializers'
|
23
|
-
# make_basic_app
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# # some tests
|
27
|
-
# end
|
28
|
-
# end
|
29
|
-
#
|
30
|
-
# Note:
|
31
|
-
# It is important to keep this file as light as possible
|
32
|
-
# the goal for tests that require this is to test booting up
|
33
|
-
# rails from an empty state, so anything added here could
|
34
|
-
# hide potential failures
|
35
|
-
#
|
36
|
-
# It is also good to know what is the bare minimum to get
|
37
|
-
# Rails booted up.
|
38
|
-
require 'bundler/setup' unless defined?(Bundler)
|
39
|
-
require 'active_support'
|
40
|
-
require 'active_support/core_ext/string/access'
|
41
|
-
|
42
|
-
# These files do not require any others and are needed
|
43
|
-
# to run the tests
|
44
|
-
require 'active_support/testing/autorun'
|
45
|
-
require 'active_support/testing/isolation'
|
46
|
-
|
47
|
-
module TestHelpers
|
48
|
-
module Generation
|
49
|
-
module_function
|
50
|
-
|
51
|
-
# Make a very basic app, without creating the whole directory structure.
|
52
|
-
# Is faster and simpler than generating a Rails app in a temp directory
|
53
|
-
def make_basic_app
|
54
|
-
require 'rails'
|
55
|
-
require 'action_controller/railtie'
|
56
|
-
|
57
|
-
@app = Class.new(Rails::Application) do
|
58
|
-
config.eager_load = false
|
59
|
-
config.session_store :cookie_store, key: '_myapp_session'
|
60
|
-
config.active_support.deprecation = :log
|
61
|
-
config.active_support.test_order = :parallel
|
62
|
-
ActiveSupport::TestCase.respond_to?(:test_order=) && ActiveSupport::TestCase.test_order = :parallel
|
63
|
-
config.root = File.dirname(__FILE__)
|
64
|
-
config.log_level = :info
|
65
|
-
# Set a fake logger to avoid creating the log directory automatically
|
66
|
-
fake_logger = Logger.new(nil)
|
67
|
-
config.logger = fake_logger
|
68
|
-
Rails.application.routes.default_url_options = { host: 'example.com' }
|
69
|
-
end
|
70
|
-
@app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4'
|
71
|
-
|
72
|
-
yield @app if block_given?
|
73
|
-
@app.initialize!
|
74
|
-
end
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
module ActiveSupport
|
79
|
-
class TestCase
|
80
|
-
include TestHelpers::Generation
|
81
|
-
end
|
82
|
-
end
|