active_model_serializers 0.10.0.rc3 → 0.10.0.rc4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +37 -33
- data/.rubocop_todo.yml +13 -88
- data/.simplecov +23 -11
- data/.travis.yml +17 -12
- data/CHANGELOG.md +417 -12
- data/CONTRIBUTING.md +206 -17
- data/Gemfile +12 -11
- data/README.md +78 -286
- data/Rakefile +44 -8
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +6 -4
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +21 -15
- data/docs/general/adapters.md +79 -27
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +18 -2
- data/docs/general/getting_started.md +44 -19
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +153 -0
- data/docs/general/serializers.md +207 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +16 -7
- data/docs/howto/add_root_key.md +3 -3
- data/docs/howto/outside_controller_use.md +25 -9
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/schema.md +140 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/lib/action_controller/serialization.rb +13 -9
- data/lib/active_model/serializable_resource.rb +9 -7
- data/lib/active_model/serializer.rb +93 -129
- data/lib/active_model/serializer/adapter.rb +37 -105
- data/lib/active_model/serializer/adapter/attributes.rb +66 -0
- data/lib/active_model/serializer/adapter/base.rb +58 -0
- data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +43 -7
- data/lib/active_model/serializer/adapter/json.rb +11 -37
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +9 -1
- data/lib/active_model/serializer/adapter/json_api.rb +127 -62
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +9 -1
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +12 -4
- data/lib/active_model/serializer/adapter/null.rb +7 -1
- data/lib/active_model/serializer/array_serializer.rb +6 -37
- data/lib/active_model/serializer/associations.rb +21 -18
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +17 -3
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +9 -18
- data/lib/active_model/serializer/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +33 -0
- data/lib/active_model/serializer/lint.rb +16 -3
- data/lib/active_model/serializer/reflection.rb +25 -8
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +16 -26
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/logging.rb +119 -0
- data/lib/active_model_serializers/model.rb +39 -0
- data/lib/active_model_serializers/railtie.rb +38 -0
- data/lib/active_model_serializers/serialization_context.rb +10 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/generators/{serializer → rails}/USAGE +1 -1
- data/lib/generators/{serializer → rails}/resource_override.rb +1 -3
- data/lib/generators/{serializer → rails}/serializer_generator.rb +2 -3
- data/lib/generators/{serializer → rails}/templates/serializer.rb.erb +0 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -0
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +59 -0
- data/test/action_controller/json_api/linked_test.rb +20 -3
- data/test/action_controller/json_api/pagination_test.rb +7 -7
- data/test/action_controller/serialization_scope_name_test.rb +8 -12
- data/test/action_controller/serialization_test.rb +44 -29
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
- data/test/active_model_serializers/serialization_context_test.rb +18 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +1 -1
- data/test/adapter/fragment_cache_test.rb +3 -2
- data/test/adapter/json/belongs_to_test.rb +2 -2
- data/test/adapter/json/collection_test.rb +15 -5
- data/test/adapter/json/has_many_test.rb +3 -3
- data/test/adapter/json_api/belongs_to_test.rb +3 -3
- data/test/adapter/json_api/collection_test.rb +8 -6
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -2
- data/test/adapter/json_api/has_many_test.rb +3 -3
- data/test/adapter/json_api/has_one_test.rb +2 -2
- data/test/adapter/json_api/json_api_test.rb +2 -2
- data/test/adapter/json_api/linked_test.rb +116 -5
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +4 -4
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +27 -15
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +2 -2
- data/test/adapter/null_test.rb +2 -2
- data/test/adapter_test.rb +3 -3
- data/test/array_serializer_test.rb +30 -93
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/poro.rb +19 -51
- data/test/generators/scaffold_controller_generator_test.rb +1 -0
- data/test/generators/serializer_generator_test.rb +2 -1
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +4 -1
- data/test/logger_test.rb +2 -2
- data/test/poro_test.rb +1 -1
- data/test/serializable_resource_test.rb +1 -1
- data/test/serializers/adapter_for_test.rb +24 -28
- data/test/serializers/association_macros_test.rb +1 -1
- data/test/serializers/associations_test.rb +143 -26
- data/test/serializers/attribute_test.rb +64 -3
- data/test/serializers/attributes_test.rb +1 -6
- data/test/serializers/cache_test.rb +46 -2
- data/test/serializers/configuration_test.rb +20 -3
- data/test/serializers/fieldset_test.rb +5 -16
- data/test/serializers/meta_test.rb +38 -29
- data/test/serializers/options_test.rb +1 -1
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serializer_for_test.rb +78 -9
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +77 -0
- data/test/support/rails5_shims.rb +29 -0
- data/test/support/rails_app.rb +7 -3
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +46 -6
- data/test/support/test_case.rb +14 -0
- data/test/test_helper.rb +21 -14
- metadata +160 -16
- data/lib/active_model/serializer/adapter/flatten_json.rb +0 -12
- data/lib/active_model/serializer/railtie.rb +0 -15
- data/lib/active_model/serializer/utils.rb +0 -35
- data/lib/tasks/rubocop.rake +0 -0
- data/test/capture_warnings.rb +0 -65
- data/test/utils/include_args_to_hash_test.rb +0 -79
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveModelSerializers::SerializationContextTest < ActiveSupport::TestCase
|
4
|
+
def create_context
|
5
|
+
request = Minitest::Mock.new
|
6
|
+
request.expect(:original_url, 'original_url')
|
7
|
+
request.expect(:query_parameters, 'query_parameters')
|
8
|
+
|
9
|
+
ActiveModelSerializers::SerializationContext.new(request)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_create_context_with_request_url_and_query_parameters
|
13
|
+
context = create_context
|
14
|
+
|
15
|
+
assert_equal context.request_url, 'original_url'
|
16
|
+
assert_equal context.query_parameters, 'query_parameters'
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Test
|
5
|
+
class SchemaTest < ActionController::TestCase
|
6
|
+
include ActiveModelSerializers::Test::Schema
|
7
|
+
|
8
|
+
class MyController < ActionController::Base
|
9
|
+
def index
|
10
|
+
render json: profile
|
11
|
+
end
|
12
|
+
|
13
|
+
def show
|
14
|
+
index
|
15
|
+
end
|
16
|
+
|
17
|
+
def name_as_a_integer
|
18
|
+
profile.name = 1
|
19
|
+
index
|
20
|
+
end
|
21
|
+
|
22
|
+
def render_using_json_api
|
23
|
+
render json: profile, adapter: :json_api
|
24
|
+
end
|
25
|
+
|
26
|
+
def invalid_json_body
|
27
|
+
render json: ''
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def profile
|
33
|
+
@profile ||= Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
tests MyController
|
38
|
+
|
39
|
+
def test_that_assert_with_a_valid_schema
|
40
|
+
get :index
|
41
|
+
assert_response_schema
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_that_raises_a_minitest_error_with_a_invalid_schema
|
45
|
+
message = "#/name: failed schema #/properties/name: For 'properties/name', \"Name 1\" is not an integer. and #/description: failed schema #/properties/description: For 'properties/description', \"Description 1\" is not a boolean."
|
46
|
+
|
47
|
+
get :show
|
48
|
+
|
49
|
+
error = assert_raises Minitest::Assertion do
|
50
|
+
assert_response_schema
|
51
|
+
end
|
52
|
+
assert_equal(message, error.message)
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_that_raises_error_with_a_custom_message_with_a_invalid_schema
|
56
|
+
message = 'oh boy the show is broken'
|
57
|
+
|
58
|
+
get :show
|
59
|
+
|
60
|
+
error = assert_raises Minitest::Assertion do
|
61
|
+
assert_response_schema(nil, message)
|
62
|
+
end
|
63
|
+
assert_equal(message, error.message)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_that_assert_with_a_custom_schema
|
67
|
+
get :show
|
68
|
+
assert_response_schema('custom/show.json')
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_that_assert_with_a_hyper_schema
|
72
|
+
get :show
|
73
|
+
assert_response_schema('hyper_schema.json')
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_simple_json_pointers
|
77
|
+
get :show
|
78
|
+
assert_response_schema('simple_json_pointers.json')
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_simple_json_pointers_that_doesnt_match
|
82
|
+
get :name_as_a_integer
|
83
|
+
|
84
|
+
assert_raises Minitest::Assertion do
|
85
|
+
assert_response_schema('simple_json_pointers.json')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_json_api_schema
|
90
|
+
get :render_using_json_api
|
91
|
+
assert_response_schema('render_using_json_api.json')
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_that_assert_with_a_custom_schema_directory
|
95
|
+
original_schema_path = ActiveModelSerializers.config.schema_path
|
96
|
+
ActiveModelSerializers.config.schema_path = 'test/support/custom_schemas'
|
97
|
+
|
98
|
+
get :index
|
99
|
+
assert_response_schema
|
100
|
+
|
101
|
+
ActiveModelSerializers.config.schema_path = original_schema_path
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_with_a_non_existent_file
|
105
|
+
message = %r{.* - No Schema file at test/support/schemas/non-existent.json}
|
106
|
+
|
107
|
+
get :show
|
108
|
+
|
109
|
+
error = assert_raises ActiveModelSerializers::Test::Schema::MissingSchema do
|
110
|
+
assert_response_schema('non-existent.json')
|
111
|
+
end
|
112
|
+
assert_match(message, error.message)
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_that_raises_with_a_invalid_json_body
|
116
|
+
message = 'A JSON text must at least contain two octets!'
|
117
|
+
|
118
|
+
get :invalid_json_body
|
119
|
+
|
120
|
+
error = assert_raises ActiveModelSerializers::Test::Schema::InvalidSchemaError do
|
121
|
+
assert_response_schema('custom/show.json')
|
122
|
+
end
|
123
|
+
|
124
|
+
assert_equal(message, error.message)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Test
|
5
|
+
class SerializerTest < ActionController::TestCase
|
6
|
+
include ActiveModelSerializers::Test::Serializer
|
7
|
+
|
8
|
+
class MyController < ActionController::Base
|
9
|
+
def render_using_serializer
|
10
|
+
render json: Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
11
|
+
end
|
12
|
+
|
13
|
+
# For Rails4.0
|
14
|
+
def render_some_text
|
15
|
+
Rails.version > '4.1' ? render(plain: 'ok') : render(text: 'ok')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
tests MyController
|
20
|
+
|
21
|
+
def test_supports_specifying_serializers_with_a_serializer_class
|
22
|
+
get :render_using_serializer
|
23
|
+
assert_serializer ProfileSerializer
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_supports_specifying_serializers_with_a_regexp
|
27
|
+
get :render_using_serializer
|
28
|
+
assert_serializer(/\AProfile.+\Z/)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_supports_specifying_serializers_with_a_string
|
32
|
+
get :render_using_serializer
|
33
|
+
assert_serializer 'ProfileSerializer'
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_supports_specifying_serializers_with_a_symbol
|
37
|
+
get :render_using_serializer
|
38
|
+
assert_serializer :profile_serializer
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_supports_specifying_serializers_with_a_nil
|
42
|
+
get :render_some_text
|
43
|
+
assert_serializer nil
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_raises_descriptive_error_message_when_serializer_was_not_rendered
|
47
|
+
get :render_using_serializer
|
48
|
+
e = assert_raise ActiveSupport::TestCase::Assertion do
|
49
|
+
assert_serializer 'PostSerializer'
|
50
|
+
end
|
51
|
+
assert_match 'expecting <"PostSerializer"> but rendering with <["ProfileSerializer"]>', e.message
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_raises_argument_error_when_asserting_with_invalid_object
|
55
|
+
get :render_using_serializer
|
56
|
+
e = assert_raise ArgumentError do
|
57
|
+
assert_serializer Hash
|
58
|
+
end
|
59
|
+
assert_match 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil', e.message
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/test/active_record_test.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
module ActiveModel
|
3
3
|
class Serializer
|
4
|
-
|
5
|
-
class FragmentCacheTest <
|
4
|
+
module Adapter
|
5
|
+
class FragmentCacheTest < ActiveSupport::TestCase
|
6
6
|
def setup
|
7
|
+
super
|
7
8
|
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
|
8
9
|
@author = Author.new(name: 'Joao M. D. Moura')
|
9
10
|
@role = Role.new(name: 'Great Author', description: nil)
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class Json
|
7
|
-
class BelongsToTest <
|
7
|
+
class BelongsToTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
10
10
|
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class Json
|
7
|
-
class Collection <
|
7
|
+
class Collection < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
@author = Author.new(id: 1, name: 'Steve K.')
|
10
10
|
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
@@ -23,7 +23,7 @@ module ActiveModel
|
|
23
23
|
def test_with_serializer_option
|
24
24
|
@blog.special_attribute = 'Special'
|
25
25
|
@blog.articles = [@first_post, @second_post]
|
26
|
-
serializer =
|
26
|
+
serializer = CollectionSerializer.new([@blog], serializer: CustomBlogSerializer)
|
27
27
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
28
28
|
|
29
29
|
expected = { blogs: [{
|
@@ -35,7 +35,7 @@ module ActiveModel
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def test_include_multiple_posts
|
38
|
-
serializer =
|
38
|
+
serializer = CollectionSerializer.new([@first_post, @second_post])
|
39
39
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
40
40
|
|
41
41
|
expected = { posts: [{
|
@@ -70,11 +70,21 @@ module ActiveModel
|
|
70
70
|
|
71
71
|
def test_root_is_underscored
|
72
72
|
virtual_value = VirtualValue.new(id: 1)
|
73
|
-
serializer =
|
73
|
+
serializer = CollectionSerializer.new([virtual_value])
|
74
74
|
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
75
75
|
|
76
76
|
assert_equal 1, adapter.serializable_hash[:virtual_values].length
|
77
77
|
end
|
78
|
+
|
79
|
+
def test_include_option
|
80
|
+
serializer = CollectionSerializer.new([@first_post, @second_post])
|
81
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer, include: '')
|
82
|
+
actual = adapter.serializable_hash
|
83
|
+
expected = { posts: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' },
|
84
|
+
{ id: 2, title: 'New Post', body: 'Body' }] }
|
85
|
+
|
86
|
+
assert_equal(expected, actual)
|
87
|
+
end
|
78
88
|
end
|
79
89
|
end
|
80
90
|
end
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class Json
|
7
|
-
class HasManyTestTest <
|
7
|
+
class HasManyTestTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
ActionController::Base.cache_store.clear
|
10
10
|
@author = Author.new(id: 1, name: 'Steve K.')
|
@@ -36,7 +36,7 @@ module ActiveModel
|
|
36
36
|
assert_equal({
|
37
37
|
id: 42,
|
38
38
|
tags: [
|
39
|
-
{ '
|
39
|
+
{ 'id' => 1, 'name' => '#hash_tag' }
|
40
40
|
]
|
41
41
|
}.to_json, adapter.serializable_hash[:post].to_json)
|
42
42
|
end
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class JsonApi
|
7
|
-
class BelongsToTest <
|
7
|
+
class BelongsToTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
@author = Author.new(id: 1, name: 'Steve K.')
|
10
10
|
@author.bio = nil
|
@@ -56,7 +56,7 @@ module ActiveModel
|
|
56
56
|
end
|
57
57
|
|
58
58
|
def test_limiting_linked_post_fields
|
59
|
-
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:post], fields: { post: [:title] })
|
59
|
+
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: [:post], fields: { post: [:title, :comments, :blog, :author] })
|
60
60
|
expected = [{
|
61
61
|
id: '42',
|
62
62
|
type: 'posts',
|
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActiveModel
|
4
4
|
class Serializer
|
5
|
-
|
5
|
+
module Adapter
|
6
6
|
class JsonApi
|
7
|
-
class CollectionTest <
|
7
|
+
class CollectionTest < ActiveSupport::TestCase
|
8
8
|
def setup
|
9
9
|
@author = Author.new(id: 1, name: 'Steve K.')
|
10
10
|
@author.bio = nil
|
@@ -19,7 +19,7 @@ module ActiveModel
|
|
19
19
|
@second_post.author = @author
|
20
20
|
@author.posts = [@first_post, @second_post]
|
21
21
|
|
22
|
-
@serializer =
|
22
|
+
@serializer = CollectionSerializer.new([@first_post, @second_post])
|
23
23
|
@adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer)
|
24
24
|
ActionController::Base.cache_store.clear
|
25
25
|
end
|
@@ -58,8 +58,10 @@ module ActiveModel
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def test_limiting_fields
|
61
|
-
|
62
|
-
|
61
|
+
actual = ActiveModel::SerializableResource.new(
|
62
|
+
[@first_post, @second_post], adapter: :json_api,
|
63
|
+
fields: { posts: %w(title comments blog author) })
|
64
|
+
.serializable_hash
|
63
65
|
expected = [
|
64
66
|
{
|
65
67
|
id: '1',
|
@@ -86,7 +88,7 @@ module ActiveModel
|
|
86
88
|
}
|
87
89
|
}
|
88
90
|
]
|
89
|
-
assert_equal(expected,
|
91
|
+
assert_equal(expected, actual[:data])
|
90
92
|
end
|
91
93
|
end
|
92
94
|
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class JsonApi
|
7
|
+
class FieldsTest < ActiveSupport::TestCase
|
8
|
+
Post = Class.new(::Model)
|
9
|
+
class PostSerializer < ActiveModel::Serializer
|
10
|
+
type 'posts'
|
11
|
+
attributes :title, :body
|
12
|
+
belongs_to :author
|
13
|
+
has_many :comments
|
14
|
+
end
|
15
|
+
|
16
|
+
Author = Class.new(::Model)
|
17
|
+
class AuthorSerializer < ActiveModel::Serializer
|
18
|
+
type 'authors'
|
19
|
+
attributes :name, :birthday
|
20
|
+
end
|
21
|
+
|
22
|
+
Comment = Class.new(::Model)
|
23
|
+
class CommentSerializer < ActiveModel::Serializer
|
24
|
+
type 'comments'
|
25
|
+
attributes :body
|
26
|
+
belongs_to :author
|
27
|
+
end
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@author = Author.new(id: 1, name: 'Lucas', birthday: '10.01.1990')
|
31
|
+
@comment1 = Comment.new(id: 7, body: 'cool', author: @author)
|
32
|
+
@comment2 = Comment.new(id: 12, body: 'awesome', author: @author)
|
33
|
+
@post = Post.new(id: 1337, title: 'Title 1', body: 'Body 1',
|
34
|
+
author: @author, comments: [@comment1, @comment2])
|
35
|
+
@comment1.post = @post
|
36
|
+
@comment2.post = @post
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_fields_attributes
|
40
|
+
fields = { posts: [:title] }
|
41
|
+
hash = serializable(@post, adapter: :json_api, fields: fields).serializable_hash
|
42
|
+
expected = {
|
43
|
+
title: 'Title 1'
|
44
|
+
}
|
45
|
+
|
46
|
+
assert_equal(expected, hash[:data][:attributes])
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_fields_relationships
|
50
|
+
fields = { posts: [:author] }
|
51
|
+
hash = serializable(@post, adapter: :json_api, fields: fields).serializable_hash
|
52
|
+
expected = {
|
53
|
+
author: {
|
54
|
+
data: {
|
55
|
+
type: 'authors',
|
56
|
+
id: '1'
|
57
|
+
}
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
assert_equal(expected, hash[:data][:relationships])
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_fields_included
|
65
|
+
fields = { posts: [:author], comments: [:body] }
|
66
|
+
hash = serializable(@post, adapter: :json_api, fields: fields, include: 'comments').serializable_hash
|
67
|
+
expected = [
|
68
|
+
{
|
69
|
+
type: 'comments',
|
70
|
+
id: '7',
|
71
|
+
attributes: {
|
72
|
+
body: 'cool'
|
73
|
+
}
|
74
|
+
}, {
|
75
|
+
type: 'comments',
|
76
|
+
id: '12',
|
77
|
+
attributes: {
|
78
|
+
body: 'awesome'
|
79
|
+
}
|
80
|
+
}
|
81
|
+
]
|
82
|
+
|
83
|
+
assert_equal(expected, hash[:included])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|