active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -11,7 +11,7 @@ module ActiveModel
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_meta_is_present_with_root
|
14
|
-
actual =
|
14
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
15
15
|
@blog,
|
16
16
|
adapter: :json,
|
17
17
|
serializer: AlternateBlogSerializer,
|
@@ -28,8 +28,40 @@ module ActiveModel
|
|
28
28
|
assert_equal(expected, actual)
|
29
29
|
end
|
30
30
|
|
31
|
+
def test_meta_is_not_included_when_blank
|
32
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
33
|
+
@blog,
|
34
|
+
adapter: :json,
|
35
|
+
serializer: AlternateBlogSerializer,
|
36
|
+
meta: {}
|
37
|
+
).as_json
|
38
|
+
expected = {
|
39
|
+
blog: {
|
40
|
+
id: 1,
|
41
|
+
title: 'AMS Hints'
|
42
|
+
}
|
43
|
+
}
|
44
|
+
assert_equal(expected, actual)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_meta_is_not_included_when_empty_string
|
48
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
49
|
+
@blog,
|
50
|
+
adapter: :json,
|
51
|
+
serializer: AlternateBlogSerializer,
|
52
|
+
meta: ''
|
53
|
+
).as_json
|
54
|
+
expected = {
|
55
|
+
blog: {
|
56
|
+
id: 1,
|
57
|
+
title: 'AMS Hints'
|
58
|
+
}
|
59
|
+
}
|
60
|
+
assert_equal(expected, actual)
|
61
|
+
end
|
62
|
+
|
31
63
|
def test_meta_is_not_included_when_root_is_missing
|
32
|
-
actual =
|
64
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
33
65
|
@blog,
|
34
66
|
adapter: :attributes,
|
35
67
|
serializer: AlternateBlogSerializer,
|
@@ -42,7 +74,7 @@ module ActiveModel
|
|
42
74
|
end
|
43
75
|
|
44
76
|
def test_meta_key_is_used
|
45
|
-
actual =
|
77
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
46
78
|
@blog,
|
47
79
|
adapter: :json,
|
48
80
|
serializer: AlternateBlogSerializer,
|
@@ -61,7 +93,7 @@ module ActiveModel
|
|
61
93
|
end
|
62
94
|
|
63
95
|
def test_meta_key_is_used_with_json_api
|
64
|
-
actual =
|
96
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
65
97
|
@blog,
|
66
98
|
adapter: :json_api,
|
67
99
|
serializer: AlternateBlogSerializer,
|
@@ -78,8 +110,44 @@ module ActiveModel
|
|
78
110
|
assert_equal(expected, actual)
|
79
111
|
end
|
80
112
|
|
113
|
+
def test_meta_key_is_not_present_when_blank_object_with_json_api
|
114
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
115
|
+
@blog,
|
116
|
+
adapter: :json_api,
|
117
|
+
serializer: AlternateBlogSerializer,
|
118
|
+
meta: {},
|
119
|
+
meta_key: 'haha_meta'
|
120
|
+
).as_json
|
121
|
+
expected = {
|
122
|
+
data: {
|
123
|
+
id: '1',
|
124
|
+
type: 'blogs',
|
125
|
+
attributes: { title: 'AMS Hints' }
|
126
|
+
}
|
127
|
+
}
|
128
|
+
assert_equal(expected, actual)
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_meta_key_is_not_present_when_empty_string_with_json_api
|
132
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
133
|
+
@blog,
|
134
|
+
adapter: :json_api,
|
135
|
+
serializer: AlternateBlogSerializer,
|
136
|
+
meta: '',
|
137
|
+
meta_key: 'haha_meta'
|
138
|
+
).as_json
|
139
|
+
expected = {
|
140
|
+
data: {
|
141
|
+
id: '1',
|
142
|
+
type: 'blogs',
|
143
|
+
attributes: { title: 'AMS Hints' }
|
144
|
+
}
|
145
|
+
}
|
146
|
+
assert_equal(expected, actual)
|
147
|
+
end
|
148
|
+
|
81
149
|
def test_meta_is_not_present_on_arrays_without_root
|
82
|
-
actual =
|
150
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
83
151
|
[@blog],
|
84
152
|
adapter: :attributes,
|
85
153
|
meta: { total: 10 }).as_json
|
@@ -100,7 +168,7 @@ module ActiveModel
|
|
100
168
|
end
|
101
169
|
|
102
170
|
def test_meta_is_present_on_arrays_with_root
|
103
|
-
actual =
|
171
|
+
actual = ActiveModelSerializers::SerializableResource.new(
|
104
172
|
[@blog],
|
105
173
|
adapter: :json,
|
106
174
|
meta: { total: 10 },
|
@@ -0,0 +1,79 @@
|
|
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
|
@@ -0,0 +1,55 @@
|
|
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
|
@@ -46,6 +46,8 @@ require 'active_support/testing/isolation'
|
|
46
46
|
|
47
47
|
module TestHelpers
|
48
48
|
module Generation
|
49
|
+
module_function
|
50
|
+
|
49
51
|
# Make a very basic app, without creating the whole directory structure.
|
50
52
|
# Is faster and simpler than generating a Rails app in a temp directory
|
51
53
|
def make_basic_app
|
@@ -63,6 +65,7 @@ module TestHelpers
|
|
63
65
|
# Set a fake logger to avoid creating the log directory automatically
|
64
66
|
fake_logger = Logger.new(nil)
|
65
67
|
config.logger = fake_logger
|
68
|
+
Rails.application.routes.default_url_options = { host: 'example.com' }
|
66
69
|
end
|
67
70
|
@app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4'
|
68
71
|
|
@@ -1,19 +1,37 @@
|
|
1
1
|
module Rails5Shims
|
2
2
|
module ControllerTests
|
3
3
|
# https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb
|
4
|
-
REQUEST_KWARGS = [:params, :session, :flash, :method, :body, :xhr]
|
4
|
+
REQUEST_KWARGS = [:params, :session, :flash, :method, :body, :xhr].freeze
|
5
|
+
|
6
|
+
def get(path, *args)
|
7
|
+
fold_kwargs!(args)
|
8
|
+
super
|
9
|
+
end
|
10
|
+
|
11
|
+
def post(path, *args)
|
12
|
+
fold_kwargs!(args)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def patch(path, *args)
|
17
|
+
fold_kwargs!(args)
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
def put(path, *args)
|
22
|
+
fold_kwargs!(args)
|
23
|
+
super
|
24
|
+
end
|
5
25
|
|
6
26
|
# Fold kwargs from test request into args
|
7
27
|
# Band-aid for DEPRECATION WARNING
|
8
|
-
def
|
28
|
+
def fold_kwargs!(args)
|
9
29
|
hash = args && args[0]
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
end
|
30
|
+
return unless hash.respond_to?(:key)
|
31
|
+
Rails5Shims::ControllerTests::REQUEST_KWARGS.each do |kwarg|
|
32
|
+
next unless hash.key?(kwarg)
|
33
|
+
hash.merge! hash.delete(kwarg)
|
15
34
|
end
|
16
|
-
super
|
17
35
|
end
|
18
36
|
|
19
37
|
# Uncomment for debugging where the kwargs warnings come from
|
data/test/support/rails_app.rb
CHANGED
@@ -1,25 +1,45 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'support/isolated_unit'
|
2
|
+
module ActiveModelSerializers
|
3
|
+
RailsApplication = TestHelpers::Generation.make_basic_app do |app|
|
4
|
+
app.configure do
|
5
|
+
config.secret_key_base = 'abc123'
|
6
|
+
config.active_support.test_order = :random
|
7
|
+
config.action_controller.perform_caching = true
|
8
|
+
config.action_controller.cache_store = :memory_store
|
9
|
+
end
|
4
10
|
|
5
|
-
|
6
|
-
|
7
|
-
config.active_support.test_order = :random
|
8
|
-
|
9
|
-
config.logger = Logger.new(nil)
|
10
|
-
|
11
|
-
config.action_controller.perform_caching = true
|
12
|
-
ActionController::Base.cache_store = :memory_store
|
11
|
+
app.routes.default_url_options = { host: 'example.com' }
|
13
12
|
end
|
14
13
|
end
|
15
|
-
ActiveModelSerializers::RailsApplication.initialize!
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
Routes = ActionDispatch::Routing::RouteSet.new
|
16
|
+
Routes.draw do
|
17
|
+
get ':controller(/:action(/:id))'
|
18
|
+
get ':controller(/:action)'
|
19
|
+
end
|
20
|
+
ActionController::Base.send :include, Routes.url_helpers
|
21
|
+
ActionController::TestCase.class_eval do
|
22
|
+
def setup
|
23
|
+
@routes = Routes
|
22
24
|
end
|
23
25
|
|
24
|
-
|
26
|
+
# For Rails5
|
27
|
+
# https://github.com/rails/rails/commit/ca83436d1b3b6cedd1eca2259f65661e69b01909#diff-b9bbf56e85d3fe1999f16317f2751e76L17
|
28
|
+
def assigns(key = nil)
|
29
|
+
warn "DEPRECATION: Calling 'assigns(#{key})' from #{caller[0]}"
|
30
|
+
assigns = {}.with_indifferent_access
|
31
|
+
@controller.view_assigns.each { |k, v| assigns.regular_writer(k, v) }
|
32
|
+
key.nil? ? assigns : assigns[key]
|
33
|
+
end
|
25
34
|
end
|
35
|
+
|
36
|
+
# ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
|
37
|
+
# ActiveRecord::Migrator.migrations_paths << File.expand_path('../../db/migrate', __FILE__)
|
38
|
+
#
|
39
|
+
# Load fixtures from the engine
|
40
|
+
# if ActiveSupport::TestCase.respond_to?(:fixture_path=)
|
41
|
+
# ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
|
42
|
+
# ActionDispatch::IntegrationTest.fixture_path = ActiveSupport::TestCase.fixture_path
|
43
|
+
# ActiveSupport::TestCase.file_fixture_path = ActiveSupport::TestCase.fixture_path + "/files"
|
44
|
+
# ActiveSupport::TestCase.fixtures :all
|
45
|
+
# end
|
@@ -6,14 +6,14 @@ module SerializationTesting
|
|
6
6
|
private
|
7
7
|
|
8
8
|
def generate_cached_serializer(obj)
|
9
|
-
|
9
|
+
ActiveModelSerializers::SerializableResource.new(obj).to_json
|
10
10
|
end
|
11
11
|
|
12
12
|
# Aliased as :with_configured_adapter to clarify that
|
13
13
|
# this method tests the configured adapter.
|
14
14
|
# When not testing configuration, it may be preferable
|
15
|
-
# to pass in the +adapter+ option to <tt>
|
16
|
-
# e.g
|
15
|
+
# to pass in the +adapter+ option to <tt>ActiveModelSerializers::SerializableResource</tt>.
|
16
|
+
# e.g ActiveModelSerializers::SerializableResource.new(resource, adapter: :json_api)
|
17
17
|
def with_adapter(adapter)
|
18
18
|
old_adapter = ActiveModelSerializers.config.adapter
|
19
19
|
ActiveModelSerializers.config.adapter = adapter
|
@@ -21,7 +21,7 @@ module SerializationTesting
|
|
21
21
|
ensure
|
22
22
|
ActiveModelSerializers.config.adapter = old_adapter
|
23
23
|
end
|
24
|
-
|
24
|
+
alias with_configured_adapter with_adapter
|
25
25
|
|
26
26
|
def with_config(hash)
|
27
27
|
old_config = config.dup
|
@@ -40,7 +40,7 @@ module SerializationTesting
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def serializable(resource, options = {})
|
43
|
-
|
43
|
+
ActiveModelSerializers::SerializableResource.new(resource, options)
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
data/test/test_helper.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
1
3
|
require 'bundler/setup'
|
2
4
|
|
3
5
|
begin
|
4
6
|
require 'simplecov'
|
5
|
-
# HACK: till https://github.com/colszowka/simplecov/pull/400 is merged and released.
|
6
|
-
# Otherwise you may get:
|
7
|
-
# simplecov-0.10.0/lib/simplecov/defaults.rb:50: warning: global variable `$ERROR_INFO' not initialized
|
8
|
-
require 'support/simplecov'
|
9
7
|
AppCoverage.start
|
10
8
|
rescue LoadError
|
11
9
|
STDERR.puts 'Running without SimpleCov'
|
@@ -16,7 +14,6 @@ require 'rails'
|
|
16
14
|
require 'action_controller'
|
17
15
|
require 'action_controller/test_case'
|
18
16
|
require 'action_controller/railtie'
|
19
|
-
require 'active_support/json'
|
20
17
|
require 'active_model_serializers'
|
21
18
|
require 'fileutils'
|
22
19
|
FileUtils.mkdir_p(File.expand_path('../../tmp/cache', __FILE__))
|
@@ -39,15 +36,14 @@ else
|
|
39
36
|
$minitest_version = 5
|
40
37
|
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest/autorun.rb
|
41
38
|
# https://github.com/seattlerb/minitest/blob/e21fdda9d/lib/minitest.rb#L45-L59
|
39
|
+
# Filter out Minitest backtrace while allowing backtrace from other libraries
|
40
|
+
# to be shown.
|
41
|
+
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
42
42
|
end
|
43
|
-
require 'minitest/reporters'
|
44
|
-
Minitest::Reporters.use!
|
45
|
-
|
46
|
-
require 'support/stream_capture'
|
47
43
|
|
48
44
|
require 'support/rails_app'
|
49
45
|
|
50
|
-
require
|
46
|
+
# require "rails/test_help"
|
51
47
|
|
52
48
|
require 'support/serialization_testing'
|
53
49
|
|