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
@@ -43,10 +43,16 @@ module ActiveModelSerializers
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def data_for_one(association)
|
46
|
-
if association
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
if belongs_to_id_on_self?(association)
|
47
|
+
id = parent_serializer.read_attribute_for_serialization(association.reflection.foreign_key)
|
48
|
+
type =
|
49
|
+
if association.polymorphic?
|
50
|
+
# We can't infer resource type for polymorphic relationships from the serializer.
|
51
|
+
# We can ONLY know a polymorphic resource type by inspecting each resource.
|
52
|
+
association.lazy_association.serializer.json_key
|
53
|
+
else
|
54
|
+
association.reflection.type.to_s
|
55
|
+
end
|
50
56
|
ResourceIdentifier.for_type_with_id(type, id, serializable_resource_options)
|
51
57
|
else
|
52
58
|
# TODO(BF): Process relationship without evaluating lazy_association
|
@@ -86,6 +92,12 @@ module ActiveModelSerializers
|
|
86
92
|
meta = association.meta
|
87
93
|
meta.respond_to?(:call) ? parent_serializer.instance_eval(&meta) : meta
|
88
94
|
end
|
95
|
+
|
96
|
+
def belongs_to_id_on_self?(association)
|
97
|
+
parent_serializer.config.jsonapi_use_foreign_key_on_belongs_to_relationship &&
|
98
|
+
association.belongs_to? &&
|
99
|
+
parent_serializer.object.respond_to?(association.reflection.foreign_key)
|
100
|
+
end
|
89
101
|
end
|
90
102
|
end
|
91
103
|
end
|
@@ -2,32 +2,34 @@ module ActiveModelSerializers
|
|
2
2
|
module Adapter
|
3
3
|
class JsonApi
|
4
4
|
class ResourceIdentifier
|
5
|
-
def self.type_for(
|
6
|
-
|
7
|
-
raw_type = serializer_type
|
8
|
-
else
|
9
|
-
inflection =
|
10
|
-
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
11
|
-
:singularize
|
12
|
-
else
|
13
|
-
:pluralize
|
14
|
-
end
|
15
|
-
|
16
|
-
raw_type = class_name.underscore
|
17
|
-
raw_type = ActiveSupport::Inflector.public_send(inflection, raw_type)
|
18
|
-
raw_type
|
19
|
-
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
20
|
-
raw_type
|
21
|
-
end
|
5
|
+
def self.type_for(serializer, serializer_type = nil, transform_options = {})
|
6
|
+
raw_type = serializer_type ? serializer_type : raw_type_from_serializer_object(serializer.object)
|
22
7
|
JsonApi.send(:transform_key_casing!, raw_type, transform_options)
|
23
8
|
end
|
24
9
|
|
25
10
|
def self.for_type_with_id(type, id, options)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
type:
|
30
|
-
|
11
|
+
type = inflect_type(type)
|
12
|
+
type = type_for(:no_class_needed, type, options)
|
13
|
+
if id.blank?
|
14
|
+
{ type: type }
|
15
|
+
else
|
16
|
+
{ id: id.to_s, type: type }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.raw_type_from_serializer_object(object)
|
21
|
+
class_name = object.class.name # should use model_name
|
22
|
+
raw_type = class_name.underscore
|
23
|
+
raw_type = inflect_type(raw_type)
|
24
|
+
raw_type
|
25
|
+
.gsub!('/'.freeze, ActiveModelSerializers.config.jsonapi_namespace_separator)
|
26
|
+
raw_type
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.inflect_type(type)
|
30
|
+
singularize = ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
31
|
+
inflection = singularize ? :singularize : :pluralize
|
32
|
+
ActiveSupport::Inflector.public_send(inflection, type)
|
31
33
|
end
|
32
34
|
|
33
35
|
# {http://jsonapi.org/format/#document-resource-identifier-objects Resource Identifier Objects}
|
@@ -37,8 +39,11 @@ module ActiveModelSerializers
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def as_json
|
40
|
-
|
41
|
-
|
42
|
+
if id.blank?
|
43
|
+
{ type: type }
|
44
|
+
else
|
45
|
+
{ id: id.to_s, type: type }
|
46
|
+
end
|
42
47
|
end
|
43
48
|
|
44
49
|
protected
|
@@ -48,7 +53,8 @@ module ActiveModelSerializers
|
|
48
53
|
private
|
49
54
|
|
50
55
|
def type_for(serializer, transform_options)
|
51
|
-
|
56
|
+
serializer_type = serializer._type
|
57
|
+
self.class.type_for(serializer, serializer_type, transform_options)
|
52
58
|
end
|
53
59
|
|
54
60
|
def id_for(serializer)
|
@@ -45,7 +45,7 @@ module ActionController
|
|
45
45
|
response = JSON.parse(@response.body)
|
46
46
|
expected = {
|
47
47
|
'restriction_for_id' => '67',
|
48
|
-
'restriction_for_type' => '
|
48
|
+
'restriction_for_type' => 'Discount',
|
49
49
|
'restricted_to_id' => nil,
|
50
50
|
'restricted_to_type' => nil
|
51
51
|
}
|
@@ -58,8 +58,10 @@ module ActionController
|
|
58
58
|
assert_equal expected_links, response['links']
|
59
59
|
end
|
60
60
|
|
61
|
-
def
|
61
|
+
def test_render_only_first_last_and_next_pagination_links
|
62
62
|
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
63
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
64
|
+
'prev' => nil,
|
63
65
|
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
64
66
|
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
65
67
|
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 } }
|
@@ -78,17 +80,21 @@ module ActionController
|
|
78
80
|
assert_equal expected_links, response['links']
|
79
81
|
end
|
80
82
|
|
81
|
-
def
|
83
|
+
def test_render_only_prev_first_and_last_pagination_links
|
82
84
|
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
83
85
|
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
84
|
-
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1"
|
86
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
87
|
+
'next' => nil,
|
88
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
85
89
|
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 } }
|
86
90
|
response = JSON.parse(@response.body)
|
87
91
|
assert_equal expected_links, response['links']
|
88
92
|
end
|
89
93
|
|
90
|
-
def
|
94
|
+
def test_render_only_first_last_and_next_pagination_links_with_additional_params
|
91
95
|
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
96
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
97
|
+
'prev' => nil,
|
92
98
|
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
93
99
|
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
94
100
|
get :render_pagination_using_will_paginate, params: { page: { number: 1, size: 2 }, teste: 'additional' }
|
@@ -96,10 +102,12 @@ module ActionController
|
|
96
102
|
assert_equal expected_links, response['links']
|
97
103
|
end
|
98
104
|
|
99
|
-
def
|
105
|
+
def test_render_only_prev_first_and_last_pagination_links_with_additional_params
|
100
106
|
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
101
107
|
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
102
|
-
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional"
|
108
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional",
|
109
|
+
'next' => nil,
|
110
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional" }
|
103
111
|
get :render_pagination_using_kaminari, params: { page: { number: 3, size: 1 }, teste: 'additional' }
|
104
112
|
response = JSON.parse(@response.body)
|
105
113
|
assert_equal expected_links, response['links']
|
@@ -6,7 +6,7 @@ module ActiveModel
|
|
6
6
|
class JsonApi
|
7
7
|
class IncludeParamTest < ActiveSupport::TestCase
|
8
8
|
IncludeParamAuthor = Class.new(::Model) do
|
9
|
-
associations :tags, :posts
|
9
|
+
associations :tags, :posts, :roles
|
10
10
|
end
|
11
11
|
|
12
12
|
class CustomCommentLoader
|
@@ -51,14 +51,19 @@ module ActiveModel
|
|
51
51
|
include_data :if_sideloaded
|
52
52
|
IncludeParamAuthorSerializer.comment_loader.all
|
53
53
|
end
|
54
|
+
has_many :roles, key: :granted_roles do
|
55
|
+
include_data :if_sideloaded
|
56
|
+
end
|
54
57
|
end
|
55
58
|
|
56
59
|
def setup
|
57
60
|
IncludeParamAuthorSerializer.comment_loader = Class.new(CustomCommentLoader).new
|
58
61
|
@tag = Tag.new(id: 1337, name: 'mytag')
|
62
|
+
@role = Role.new(id: 1337, name: 'myrole')
|
59
63
|
@author = IncludeParamAuthor.new(
|
60
64
|
id: 1337,
|
61
|
-
tags: [@tag]
|
65
|
+
tags: [@tag],
|
66
|
+
roles: [@role]
|
62
67
|
)
|
63
68
|
end
|
64
69
|
|
@@ -105,6 +110,31 @@ module ActiveModel
|
|
105
110
|
assert_equal(expected, hash[:included])
|
106
111
|
end
|
107
112
|
|
113
|
+
def test_sideloads_included_when_using_key
|
114
|
+
expected = [
|
115
|
+
{
|
116
|
+
id: '1337',
|
117
|
+
type: 'roles',
|
118
|
+
attributes: {
|
119
|
+
name: 'myrole',
|
120
|
+
description: nil,
|
121
|
+
slug: 'myrole-1337'
|
122
|
+
},
|
123
|
+
relationships: {
|
124
|
+
author: { data: nil }
|
125
|
+
}
|
126
|
+
}
|
127
|
+
]
|
128
|
+
|
129
|
+
hash = result(include: :granted_roles)
|
130
|
+
assert_equal(expected, hash[:included])
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_sideloads_not_included_when_using_name_when_key_defined
|
134
|
+
hash = result(include: :roles)
|
135
|
+
assert_nil(hash[:included])
|
136
|
+
end
|
137
|
+
|
108
138
|
def test_nested_relationship
|
109
139
|
expected = {
|
110
140
|
data: [
|
@@ -54,6 +54,16 @@ module ActiveModelSerializers
|
|
54
54
|
}
|
55
55
|
end
|
56
56
|
|
57
|
+
def empty_collection_links
|
58
|
+
{
|
59
|
+
self: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
60
|
+
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
61
|
+
prev: nil,
|
62
|
+
next: nil,
|
63
|
+
last: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2"
|
64
|
+
}
|
65
|
+
end
|
66
|
+
|
57
67
|
def links
|
58
68
|
{
|
59
69
|
links: {
|
@@ -71,7 +81,9 @@ module ActiveModelSerializers
|
|
71
81
|
links: {
|
72
82
|
self: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2",
|
73
83
|
first: "#{URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
74
|
-
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2"
|
84
|
+
prev: "#{URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
85
|
+
next: nil,
|
86
|
+
last: "#{URI}?page%5Bnumber%5D=3&page%5Bsize%5D=2"
|
75
87
|
}
|
76
88
|
}
|
77
89
|
end
|
@@ -108,10 +120,10 @@ module ActiveModelSerializers
|
|
108
120
|
end
|
109
121
|
end
|
110
122
|
|
111
|
-
def
|
123
|
+
def expected_response_with_empty_collection_pagination_links
|
112
124
|
{}.tap do |hash|
|
113
125
|
hash[:data] = []
|
114
|
-
hash
|
126
|
+
hash.merge! links: empty_collection_links
|
115
127
|
end
|
116
128
|
end
|
117
129
|
|
@@ -139,7 +151,7 @@ module ActiveModelSerializers
|
|
139
151
|
|
140
152
|
adapter = load_adapter(using_kaminari(1), mock_request)
|
141
153
|
|
142
|
-
assert_equal
|
154
|
+
assert_equal expected_response_with_empty_collection_pagination_links, adapter.serializable_hash
|
143
155
|
end
|
144
156
|
|
145
157
|
def test_pagination_links_when_zero_results_will_paginate
|
@@ -147,7 +159,7 @@ module ActiveModelSerializers
|
|
147
159
|
|
148
160
|
adapter = load_adapter(using_will_paginate(1), mock_request)
|
149
161
|
|
150
|
-
assert_equal
|
162
|
+
assert_equal expected_response_with_empty_collection_pagination_links, adapter.serializable_hash
|
151
163
|
end
|
152
164
|
|
153
165
|
def test_last_page_pagination_links_using_kaminari
|
@@ -171,10 +183,11 @@ module ActiveModelSerializers
|
|
171
183
|
def test_raises_descriptive_error_when_serialization_context_unset
|
172
184
|
render_options = { adapter: :json_api }
|
173
185
|
adapter = serializable(using_kaminari, render_options)
|
174
|
-
|
186
|
+
exception_class = ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError
|
187
|
+
|
188
|
+
exception = assert_raises(exception_class) do
|
175
189
|
adapter.as_json
|
176
190
|
end
|
177
|
-
exception_class = ActiveModelSerializers::Adapter::JsonApi::PaginationLinks::MissingSerializationContextError
|
178
191
|
assert_equal exception_class, exception.class
|
179
192
|
assert_match(/CollectionSerializer#paginated\?/, exception.message)
|
180
193
|
end
|
@@ -125,7 +125,7 @@ module ActiveModelSerializers
|
|
125
125
|
src: 'http://example.com/images/productivity.png',
|
126
126
|
author_id: nil,
|
127
127
|
photographer_id: '9',
|
128
|
-
photographer_type: '
|
128
|
+
photographer_type: 'Person',
|
129
129
|
comment_ids: %w(1 2)
|
130
130
|
}
|
131
131
|
assert_equal(expected, parsed_hash)
|
@@ -1,60 +1,192 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
class
|
7
|
-
class
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class JsonApi
|
6
|
+
class TypeTest < ActiveSupport::TestCase
|
7
|
+
class StringTypeSerializer < ActiveModel::Serializer
|
8
|
+
attribute :name
|
9
|
+
type 'profile'
|
10
|
+
end
|
11
|
+
|
12
|
+
class SymbolTypeSerializer < ActiveModel::Serializer
|
13
|
+
attribute :name
|
14
|
+
type :profile
|
15
|
+
end
|
16
|
+
|
17
|
+
setup do
|
18
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_config_plural
|
22
|
+
with_jsonapi_inflection :plural do
|
23
|
+
assert_type(@author, 'authors')
|
11
24
|
end
|
25
|
+
end
|
12
26
|
|
13
|
-
|
14
|
-
|
15
|
-
|
27
|
+
def test_config_singular
|
28
|
+
with_jsonapi_inflection :singular do
|
29
|
+
assert_type(@author, 'author')
|
16
30
|
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_explicit_string_type_value
|
34
|
+
assert_type(@author, 'profile', serializer: StringTypeSerializer)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_explicit_symbol_type_value
|
38
|
+
assert_type(@author, 'profile', serializer: SymbolTypeSerializer)
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
17
42
|
|
18
|
-
|
19
|
-
|
43
|
+
def assert_type(resource, expected_type, opts = {})
|
44
|
+
opts = opts.reverse_merge(adapter: :json_api)
|
45
|
+
hash = serializable(resource, opts).serializable_hash
|
46
|
+
assert_equal(expected_type, hash.fetch(:data).fetch(:type))
|
47
|
+
end
|
48
|
+
end
|
49
|
+
class ResourceIdentifierTest < ActiveSupport::TestCase
|
50
|
+
class WithDefinedTypeSerializer < ActiveModel::Serializer
|
51
|
+
type 'with_defined_types'
|
52
|
+
end
|
53
|
+
|
54
|
+
class WithDefinedIdSerializer < ActiveModel::Serializer
|
55
|
+
def id
|
56
|
+
'special_id'
|
20
57
|
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class FragmentedSerializer < ActiveModel::Serializer
|
61
|
+
cache only: :id
|
21
62
|
|
22
|
-
def
|
23
|
-
|
24
|
-
assert_type(@author, 'authors')
|
25
|
-
end
|
63
|
+
def id
|
64
|
+
'special_id'
|
26
65
|
end
|
66
|
+
end
|
67
|
+
|
68
|
+
setup do
|
69
|
+
@model = Author.new(id: 1, name: 'Steve K.')
|
70
|
+
ActionController::Base.cache_store.clear
|
71
|
+
end
|
27
72
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
end
|
73
|
+
def test_defined_type
|
74
|
+
actual = with_jsonapi_inflection :plural do
|
75
|
+
actual_resource_identifier_object(WithDefinedTypeSerializer, @model)
|
32
76
|
end
|
77
|
+
expected = { id: expected_model_id(@model), type: 'with-defined-types' }
|
78
|
+
assert_equal actual, expected
|
79
|
+
end
|
33
80
|
|
34
|
-
|
35
|
-
|
81
|
+
def test_defined_type_not_inflected
|
82
|
+
actual = with_jsonapi_inflection :singular do
|
83
|
+
actual_resource_identifier_object(WithDefinedTypeSerializer, @model)
|
36
84
|
end
|
85
|
+
expected = { id: expected_model_id(@model), type: 'with-defined-types' }
|
86
|
+
assert_equal actual, expected
|
87
|
+
end
|
37
88
|
|
38
|
-
|
39
|
-
|
89
|
+
def test_singular_type
|
90
|
+
actual = with_jsonapi_inflection :singular do
|
91
|
+
actual_resource_identifier_object(AuthorSerializer, @model)
|
40
92
|
end
|
93
|
+
expected = { id: expected_model_id(@model), type: 'author' }
|
94
|
+
assert_equal actual, expected
|
95
|
+
end
|
41
96
|
|
42
|
-
|
97
|
+
def test_plural_type
|
98
|
+
actual = with_jsonapi_inflection :plural do
|
99
|
+
actual_resource_identifier_object(AuthorSerializer, @model)
|
100
|
+
end
|
101
|
+
expected = { id: expected_model_id(@model), type: 'authors' }
|
102
|
+
assert_equal actual, expected
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_type_with_namespace
|
106
|
+
Object.const_set(:Admin, Module.new)
|
107
|
+
model = Class.new(::Model)
|
108
|
+
Admin.const_set(:PowerUser, model)
|
109
|
+
serializer = Class.new(ActiveModel::Serializer)
|
110
|
+
Admin.const_set(:PowerUserSerializer, serializer)
|
111
|
+
with_namespace_separator '--' do
|
112
|
+
admin_user = Admin::PowerUser.new
|
113
|
+
serializer = Admin::PowerUserSerializer.new(admin_user)
|
114
|
+
expected = {
|
115
|
+
id: admin_user.id,
|
116
|
+
type: 'admin--power-users'
|
117
|
+
}
|
118
|
+
|
119
|
+
identifier = ResourceIdentifier.new(serializer, {})
|
120
|
+
actual = identifier.as_json
|
121
|
+
assert_equal(expected, actual)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_id_defined_on_object
|
126
|
+
actual = actual_resource_identifier_object(AuthorSerializer, @model)
|
127
|
+
expected = { id: @model.id.to_s, type: expected_model_type(@model) }
|
128
|
+
assert_equal actual, expected
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_blank_id
|
132
|
+
model = Author.new(id: nil, name: 'Steve K.')
|
133
|
+
actual = actual_resource_identifier_object(AuthorSerializer, model)
|
134
|
+
expected = { type: expected_model_type(model) }
|
135
|
+
assert_equal actual, expected
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_for_type_with_id
|
139
|
+
id = 1
|
140
|
+
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
|
141
|
+
expected = { id: '1', type: 'admin-users' }
|
142
|
+
assert_equal actual, expected
|
143
|
+
end
|
43
144
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
145
|
+
def test_for_type_with_id_given_blank_id
|
146
|
+
id = ''
|
147
|
+
actual = ResourceIdentifier.for_type_with_id('admin_user', id, {})
|
148
|
+
expected = { type: 'admin-users' }
|
149
|
+
assert_equal actual, expected
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_for_type_with_id_inflected
|
153
|
+
id = 2
|
154
|
+
actual = with_jsonapi_inflection :singular do
|
155
|
+
ResourceIdentifier.for_type_with_id('admin_users', id, {})
|
48
156
|
end
|
157
|
+
expected = { id: '2', type: 'admin-user' }
|
158
|
+
assert_equal actual, expected
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_id_defined_on_serializer
|
162
|
+
actual = actual_resource_identifier_object(WithDefinedIdSerializer, @model)
|
163
|
+
expected = { id: 'special_id', type: expected_model_type(@model) }
|
164
|
+
assert_equal actual, expected
|
165
|
+
end
|
166
|
+
|
167
|
+
def test_id_defined_on_fragmented
|
168
|
+
actual = actual_resource_identifier_object(FragmentedSerializer, @model)
|
169
|
+
expected = { id: 'special_id', type: expected_model_type(@model) }
|
170
|
+
assert_equal actual, expected
|
171
|
+
end
|
172
|
+
|
173
|
+
private
|
174
|
+
|
175
|
+
def actual_resource_identifier_object(serializer_class, model)
|
176
|
+
serializer = serializer_class.new(model)
|
177
|
+
resource_identifier = ResourceIdentifier.new(serializer, nil)
|
178
|
+
resource_identifier.as_json
|
179
|
+
end
|
49
180
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
yield
|
54
|
-
ensure
|
55
|
-
ActiveModelSerializers.config.jsonapi_resource_type = old_inflection
|
181
|
+
def expected_model_type(model, inflection = ActiveModelSerializers.config.jsonapi_resource_type)
|
182
|
+
with_jsonapi_inflection inflection do
|
183
|
+
model.class.model_name.send(inflection)
|
56
184
|
end
|
57
185
|
end
|
186
|
+
|
187
|
+
def expected_model_id(model)
|
188
|
+
model.id.to_s
|
189
|
+
end
|
58
190
|
end
|
59
191
|
end
|
60
192
|
end
|