active_model_serializers 0.10.6 → 0.10.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.travis.yml +28 -31
- data/CHANGELOG.md +47 -1
- data/Gemfile +20 -4
- data/README.md +1 -3
- data/active_model_serializers.gemspec +1 -1
- data/appveyor.yml +4 -6
- data/docs/general/adapters.md +6 -0
- data/docs/general/configuration_options.md +16 -0
- data/docs/general/rendering.md +1 -1
- data/docs/general/serializers.md +4 -1
- data/docs/howto/add_root_key.md +7 -0
- data/lib/active_model/serializer.rb +2 -1
- data/lib/active_model/serializer/collection_serializer.rb +4 -1
- data/lib/active_model/serializer/reflection.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +40 -21
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +16 -4
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +31 -25
- data/test/action_controller/json_api/deserialization_test.rb +1 -1
- data/test/action_controller/json_api/pagination_test.rb +14 -6
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +32 -2
- data/test/adapter/json_api/pagination_links_test.rb +20 -7
- data/test/adapter/json_api/parse_test.rb +1 -1
- data/test/adapter/json_api/type_test.rb +168 -36
- data/test/adapter/polymorphic_test.rb +47 -0
- data/test/collection_serializer_test.rb +6 -2
- data/test/fixtures/active_record.rb +2 -2
- data/test/serializers/associations_test.rb +45 -1
- data/test/support/isolated_unit.rb +4 -2
- data/test/support/serialization_testing.rb +8 -0
- metadata +37 -41
- data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
- data/test/adapter/json_api/resource_identifier_test.rb +0 -110
@@ -1,43 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
module Adapter
|
5
|
-
class JsonApi
|
6
|
-
class HasManyEmbedIdsTest < ActiveSupport::TestCase
|
7
|
-
def setup
|
8
|
-
@author = Author.new(name: 'Steve K.')
|
9
|
-
@author.bio = nil
|
10
|
-
@author.roles = nil
|
11
|
-
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
12
|
-
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
13
|
-
@author.posts = [@first_post, @second_post]
|
14
|
-
@first_post.author = @author
|
15
|
-
@second_post.author = @author
|
16
|
-
@first_post.comments = []
|
17
|
-
@second_post.comments = []
|
18
|
-
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
19
|
-
@first_post.blog = @blog
|
20
|
-
@second_post.blog = nil
|
21
|
-
|
22
|
-
@serializer = AuthorSerializer.new(@author)
|
23
|
-
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(@serializer)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_includes_comment_ids
|
27
|
-
expected = {
|
28
|
-
data: [
|
29
|
-
{ type: 'posts', id: '1' },
|
30
|
-
{ type: 'posts', id: '2' }
|
31
|
-
]
|
32
|
-
}
|
33
|
-
|
34
|
-
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:posts])
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_no_includes_linked_comments
|
38
|
-
assert_nil @adapter.serializable_hash[:linked]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
@@ -1,110 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
module ActiveModelSerializers
|
4
|
-
module Adapter
|
5
|
-
class JsonApi
|
6
|
-
class ResourceIdentifierTest < ActiveSupport::TestCase
|
7
|
-
class WithDefinedTypeSerializer < ActiveModel::Serializer
|
8
|
-
type 'with_defined_type'
|
9
|
-
end
|
10
|
-
|
11
|
-
class WithDefinedIdSerializer < ActiveModel::Serializer
|
12
|
-
def id
|
13
|
-
'special_id'
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class FragmentedSerializer < ActiveModel::Serializer
|
18
|
-
cache only: :id
|
19
|
-
|
20
|
-
def id
|
21
|
-
'special_id'
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
setup do
|
26
|
-
@model = Author.new(id: 1, name: 'Steve K.')
|
27
|
-
ActionController::Base.cache_store.clear
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_defined_type
|
31
|
-
test_type(WithDefinedTypeSerializer, 'with-defined-type')
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_singular_type
|
35
|
-
test_type_inflection(AuthorSerializer, 'author', :singular)
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_plural_type
|
39
|
-
test_type_inflection(AuthorSerializer, 'authors', :plural)
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_type_with_namespace
|
43
|
-
Object.const_set(:Admin, Module.new)
|
44
|
-
model = Class.new(::Model)
|
45
|
-
Admin.const_set(:PowerUser, model)
|
46
|
-
serializer = Class.new(ActiveModel::Serializer)
|
47
|
-
Admin.const_set(:PowerUserSerializer, serializer)
|
48
|
-
with_namespace_separator '--' do
|
49
|
-
admin_user = Admin::PowerUser.new
|
50
|
-
serializer = Admin::PowerUserSerializer.new(admin_user)
|
51
|
-
expected = {
|
52
|
-
id: admin_user.id,
|
53
|
-
type: 'admin--power-users'
|
54
|
-
}
|
55
|
-
|
56
|
-
identifier = ResourceIdentifier.new(serializer, {})
|
57
|
-
actual = identifier.as_json
|
58
|
-
assert_equal(expected, actual)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def test_id_defined_on_object
|
63
|
-
test_id(AuthorSerializer, @model.id.to_s)
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_id_defined_on_serializer
|
67
|
-
test_id(WithDefinedIdSerializer, 'special_id')
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_id_defined_on_fragmented
|
71
|
-
test_id(FragmentedSerializer, 'special_id')
|
72
|
-
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
def test_type_inflection(serializer_class, expected_type, inflection)
|
77
|
-
original_inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
78
|
-
ActiveModelSerializers.config.jsonapi_resource_type = inflection
|
79
|
-
test_type(serializer_class, expected_type)
|
80
|
-
ensure
|
81
|
-
ActiveModelSerializers.config.jsonapi_resource_type = original_inflection
|
82
|
-
end
|
83
|
-
|
84
|
-
def test_type(serializer_class, expected_type)
|
85
|
-
serializer = serializer_class.new(@model)
|
86
|
-
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
87
|
-
expected = {
|
88
|
-
id: @model.id.to_s,
|
89
|
-
type: expected_type
|
90
|
-
}
|
91
|
-
|
92
|
-
assert_equal(expected, resource_identifier.as_json)
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_id(serializer_class, id)
|
96
|
-
serializer = serializer_class.new(@model)
|
97
|
-
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
98
|
-
inflection = ActiveModelSerializers.config.jsonapi_resource_type
|
99
|
-
type = @model.class.model_name.send(inflection)
|
100
|
-
expected = {
|
101
|
-
id: id,
|
102
|
-
type: type
|
103
|
-
}
|
104
|
-
|
105
|
-
assert_equal(expected, resource_identifier.as_json)
|
106
|
-
end
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|