active_model_serializers 0.10.9 → 0.10.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -2
- data/lib/action_controller/serialization.rb +9 -1
- data/lib/active_model/serializer.rb +10 -2
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers/adapter/attributes.rb +21 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
- metadata +12 -259
- data/.github/ISSUE_TEMPLATE.md +0 -29
- data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
- data/.gitignore +0 -35
- data/.rubocop.yml +0 -109
- data/.simplecov +0 -110
- data/.travis.yml +0 -63
- data/CODE_OF_CONDUCT.md +0 -74
- data/CONTRIBUTING.md +0 -105
- data/Gemfile +0 -74
- data/Rakefile +0 -76
- data/active_model_serializers.gemspec +0 -64
- data/appveyor.yml +0 -28
- data/bin/bench +0 -171
- data/bin/bench_regression +0 -316
- data/bin/rubocop +0 -38
- data/bin/serve_benchmark +0 -39
- data/docs/README.md +0 -41
- data/docs/STYLE.md +0 -58
- data/docs/general/adapters.md +0 -269
- data/docs/general/caching.md +0 -58
- data/docs/general/configuration_options.md +0 -185
- 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 -21
- data/docs/general/rendering.md +0 -293
- data/docs/general/serializers.md +0 -495
- 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 -140
- data/docs/howto/add_root_key.md +0 -62
- data/docs/howto/grape_integration.md +0 -42
- data/docs/howto/outside_controller_use.md +0 -66
- data/docs/howto/passing_arbitrary_options.md +0 -27
- data/docs/howto/serialize_poro.md +0 -73
- 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 -147
- 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/test/action_controller/adapter_selector_test.rb +0 -64
- data/test/action_controller/explicit_serializer_test.rb +0 -137
- data/test/action_controller/json/include_test.rb +0 -248
- data/test/action_controller/json_api/deserialization_test.rb +0 -114
- data/test/action_controller/json_api/errors_test.rb +0 -42
- data/test/action_controller/json_api/fields_test.rb +0 -68
- data/test/action_controller/json_api/linked_test.rb +0 -204
- data/test/action_controller/json_api/pagination_test.rb +0 -126
- data/test/action_controller/json_api/transform_test.rb +0 -191
- data/test/action_controller/lookup_proc_test.rb +0 -51
- data/test/action_controller/namespace_lookup_test.rb +0 -234
- data/test/action_controller/serialization_scope_name_test.rb +0 -237
- data/test/action_controller/serialization_test.rb +0 -480
- data/test/active_model_serializers/adapter_for_test.rb +0 -210
- data/test/active_model_serializers/json_pointer_test.rb +0 -24
- data/test/active_model_serializers/logging_test.rb +0 -79
- data/test/active_model_serializers/model_test.rb +0 -144
- data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
- data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
- data/test/active_model_serializers/test/schema_test.rb +0 -133
- data/test/active_model_serializers/test/serializer_test.rb +0 -64
- data/test/active_record_test.rb +0 -11
- data/test/adapter/attributes_test.rb +0 -42
- data/test/adapter/deprecation_test.rb +0 -102
- data/test/adapter/json/belongs_to_test.rb +0 -47
- data/test/adapter/json/collection_test.rb +0 -106
- data/test/adapter/json/has_many_test.rb +0 -55
- data/test/adapter/json/transform_test.rb +0 -95
- data/test/adapter/json_api/belongs_to_test.rb +0 -157
- data/test/adapter/json_api/collection_test.rb +0 -98
- data/test/adapter/json_api/errors_test.rb +0 -78
- data/test/adapter/json_api/fields_test.rb +0 -98
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
- data/test/adapter/json_api/has_many_test.rb +0 -175
- data/test/adapter/json_api/has_one_test.rb +0 -82
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
- data/test/adapter/json_api/json_api_test.rb +0 -35
- data/test/adapter/json_api/linked_test.rb +0 -415
- data/test/adapter/json_api/links_test.rb +0 -112
- data/test/adapter/json_api/pagination_links_test.rb +0 -208
- data/test/adapter/json_api/parse_test.rb +0 -139
- data/test/adapter/json_api/relationship_test.rb +0 -399
- data/test/adapter/json_api/resource_meta_test.rb +0 -102
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
- data/test/adapter/json_api/transform_test.rb +0 -514
- data/test/adapter/json_api/type_test.rb +0 -195
- data/test/adapter/json_test.rb +0 -48
- data/test/adapter/null_test.rb +0 -24
- data/test/adapter/polymorphic_test.rb +0 -220
- data/test/adapter_test.rb +0 -69
- data/test/array_serializer_test.rb +0 -24
- data/test/benchmark/app.rb +0 -67
- data/test/benchmark/benchmarking_support.rb +0 -69
- data/test/benchmark/bm_active_record.rb +0 -83
- data/test/benchmark/bm_adapter.rb +0 -40
- data/test/benchmark/bm_caching.rb +0 -121
- data/test/benchmark/bm_lookup_chain.rb +0 -85
- data/test/benchmark/bm_transform.rb +0 -47
- data/test/benchmark/config.ru +0 -3
- data/test/benchmark/controllers.rb +0 -85
- data/test/benchmark/fixtures.rb +0 -221
- data/test/cache_test.rb +0 -717
- data/test/collection_serializer_test.rb +0 -129
- data/test/fixtures/active_record.rb +0 -115
- data/test/fixtures/poro.rb +0 -227
- data/test/generators/scaffold_controller_generator_test.rb +0 -26
- data/test/generators/serializer_generator_test.rb +0 -77
- data/test/grape_test.rb +0 -198
- data/test/lint_test.rb +0 -51
- data/test/logger_test.rb +0 -22
- data/test/poro_test.rb +0 -11
- data/test/serializable_resource_test.rb +0 -81
- data/test/serializers/association_macros_test.rb +0 -39
- data/test/serializers/associations_test.rb +0 -520
- data/test/serializers/attribute_test.rb +0 -155
- data/test/serializers/attributes_test.rb +0 -54
- data/test/serializers/caching_configuration_test_isolated.rb +0 -172
- data/test/serializers/configuration_test.rb +0 -34
- data/test/serializers/fieldset_test.rb +0 -16
- data/test/serializers/meta_test.rb +0 -204
- data/test/serializers/options_test.rb +0 -34
- data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
- data/test/serializers/reflection_test.rb +0 -481
- data/test/serializers/root_test.rb +0 -23
- data/test/serializers/serialization_test.rb +0 -57
- data/test/serializers/serializer_for_test.rb +0 -138
- data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
- data/test/support/isolated_unit.rb +0 -86
- data/test/support/rails5_shims.rb +0 -55
- data/test/support/rails_app.rb +0 -40
- 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 -81
- data/test/test_helper.rb +0 -72
@@ -1,26 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
require 'generators/rails/resource_override'
|
5
|
-
|
6
|
-
class ResourceGeneratorTest < Rails::Generators::TestCase
|
7
|
-
destination File.expand_path('../../../tmp/generators', __FILE__)
|
8
|
-
setup :prepare_destination, :copy_routes
|
9
|
-
|
10
|
-
tests Rails::Generators::ResourceGenerator
|
11
|
-
arguments %w(account)
|
12
|
-
|
13
|
-
def test_serializer_file_is_generated
|
14
|
-
run_generator
|
15
|
-
|
16
|
-
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def copy_routes
|
22
|
-
config_dir = File.join(destination_root, 'config')
|
23
|
-
FileUtils.mkdir_p(config_dir)
|
24
|
-
File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw {}')
|
25
|
-
end
|
26
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
require 'generators/rails/resource_override'
|
5
|
-
require 'generators/rails/serializer_generator'
|
6
|
-
|
7
|
-
class SerializerGeneratorTest < Rails::Generators::TestCase
|
8
|
-
destination File.expand_path('../../../tmp/generators', __FILE__)
|
9
|
-
setup :prepare_destination
|
10
|
-
|
11
|
-
tests Rails::Generators::SerializerGenerator
|
12
|
-
arguments %w(account name:string description:text business:references)
|
13
|
-
|
14
|
-
def test_generates_a_serializer
|
15
|
-
run_generator
|
16
|
-
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_generates_a_namespaced_serializer
|
20
|
-
run_generator ['admin/account']
|
21
|
-
assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_uses_application_serializer_if_one_exists
|
25
|
-
stub_safe_constantize(expected: 'ApplicationSerializer') do
|
26
|
-
run_generator
|
27
|
-
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_uses_given_parent
|
32
|
-
Object.const_set(:ApplicationSerializer, Class.new)
|
33
|
-
run_generator ['Account', '--parent=MySerializer']
|
34
|
-
assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/
|
35
|
-
ensure
|
36
|
-
Object.send :remove_const, :ApplicationSerializer
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_generates_attributes_and_associations
|
40
|
-
run_generator
|
41
|
-
assert_file 'app/serializers/account_serializer.rb' do |serializer|
|
42
|
-
assert_match(/^ attributes :id, :name, :description$/, serializer)
|
43
|
-
assert_match(/^ has_one :business$/, serializer)
|
44
|
-
assert_match(/^end\n*\z/, serializer)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_with_no_attributes_does_not_add_extra_space
|
49
|
-
run_generator ['account']
|
50
|
-
assert_file 'app/serializers/account_serializer.rb' do |content|
|
51
|
-
if RUBY_PLATFORM =~ /mingw/
|
52
|
-
assert_no_match(/\r\n\r\nend/, content)
|
53
|
-
else
|
54
|
-
assert_no_match(/\n\nend/, content)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
private
|
60
|
-
|
61
|
-
def stub_safe_constantize(expected:)
|
62
|
-
String.class_eval do
|
63
|
-
alias_method :old, :safe_constantize
|
64
|
-
end
|
65
|
-
String.send(:define_method, :safe_constantize) do
|
66
|
-
Class if self == expected
|
67
|
-
end
|
68
|
-
|
69
|
-
yield
|
70
|
-
ensure
|
71
|
-
String.class_eval do
|
72
|
-
undef_method :safe_constantize
|
73
|
-
alias_method :safe_constantize, :old
|
74
|
-
undef_method :old
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
data/test/grape_test.rb
DELETED
@@ -1,198 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
TestHelper.silence_warnings do
|
5
|
-
require 'grape'
|
6
|
-
end
|
7
|
-
require 'grape/active_model_serializers'
|
8
|
-
require 'kaminari'
|
9
|
-
require 'kaminari/hooks'
|
10
|
-
::Kaminari::Hooks.init
|
11
|
-
|
12
|
-
module ActiveModelSerializers
|
13
|
-
class GrapeTest < ActiveSupport::TestCase
|
14
|
-
include Rack::Test::Methods
|
15
|
-
module Models
|
16
|
-
def self.model1
|
17
|
-
ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.model2
|
21
|
-
ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.all
|
25
|
-
@all ||=
|
26
|
-
begin
|
27
|
-
model1.save!
|
28
|
-
model2.save!
|
29
|
-
ARModels::Post.all
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.reset_all
|
34
|
-
ARModels::Post.delete_all
|
35
|
-
@all = nil
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.collection_per
|
39
|
-
2
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.collection
|
43
|
-
@collection ||=
|
44
|
-
begin
|
45
|
-
Kaminari.paginate_array(
|
46
|
-
[
|
47
|
-
Profile.new(id: 1, name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
48
|
-
Profile.new(id: 2, name: 'Name 2', description: 'Description 2', comments: 'Comments 2'),
|
49
|
-
Profile.new(id: 3, name: 'Name 3', description: 'Description 3', comments: 'Comments 3'),
|
50
|
-
Profile.new(id: 4, name: 'Name 4', description: 'Description 4', comments: 'Comments 4'),
|
51
|
-
Profile.new(id: 5, name: 'Name 5', description: 'Description 5', comments: 'Comments 5')
|
52
|
-
]
|
53
|
-
).page(1).per(collection_per)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
class GrapeTest < Grape::API
|
59
|
-
format :json
|
60
|
-
TestHelper.silence_warnings do
|
61
|
-
include Grape::ActiveModelSerializers
|
62
|
-
end
|
63
|
-
|
64
|
-
def self.resources(*)
|
65
|
-
TestHelper.silence_warnings do
|
66
|
-
super
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
resources :grape do
|
71
|
-
get '/render' do
|
72
|
-
render Models.model1
|
73
|
-
end
|
74
|
-
|
75
|
-
get '/render_with_json_api' do
|
76
|
-
post = Models.model1
|
77
|
-
render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
|
78
|
-
end
|
79
|
-
|
80
|
-
get '/render_array_with_json_api' do
|
81
|
-
posts = Models.all
|
82
|
-
render posts, adapter: :json_api
|
83
|
-
end
|
84
|
-
|
85
|
-
get '/render_collection_with_json_api' do
|
86
|
-
posts = Models.collection
|
87
|
-
render posts, adapter: :json_api
|
88
|
-
end
|
89
|
-
|
90
|
-
get '/render_with_implicit_formatter' do
|
91
|
-
Models.model1
|
92
|
-
end
|
93
|
-
|
94
|
-
get '/render_array_with_implicit_formatter' do
|
95
|
-
Models.all
|
96
|
-
end
|
97
|
-
|
98
|
-
get '/render_collection_with_implicit_formatter' do
|
99
|
-
Models.collection
|
100
|
-
end
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def app
|
105
|
-
Grape::Middleware::Globals.new(GrapeTest.new)
|
106
|
-
end
|
107
|
-
|
108
|
-
extend Minitest::Assertions
|
109
|
-
def self.run_one_method(*)
|
110
|
-
_, stderr = capture_io do
|
111
|
-
super
|
112
|
-
end
|
113
|
-
fail Minitest::Assertion, stderr if stderr !~ /grape/
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_formatter_returns_json
|
117
|
-
get '/grape/render'
|
118
|
-
|
119
|
-
post = Models.model1
|
120
|
-
serializable_resource = serializable(post)
|
121
|
-
|
122
|
-
assert last_response.ok?
|
123
|
-
assert_equal serializable_resource.to_json, last_response.body
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_render_helper_passes_through_options_correctly
|
127
|
-
get '/grape/render_with_json_api'
|
128
|
-
|
129
|
-
post = Models.model1
|
130
|
-
serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
|
131
|
-
|
132
|
-
assert last_response.ok?
|
133
|
-
assert_equal serializable_resource.to_json, last_response.body
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_formatter_handles_arrays
|
137
|
-
get '/grape/render_array_with_json_api'
|
138
|
-
|
139
|
-
posts = Models.all
|
140
|
-
serializable_resource = serializable(posts, adapter: :json_api)
|
141
|
-
|
142
|
-
assert last_response.ok?
|
143
|
-
assert_equal serializable_resource.to_json, last_response.body
|
144
|
-
ensure
|
145
|
-
Models.reset_all
|
146
|
-
end
|
147
|
-
|
148
|
-
def test_formatter_handles_collections
|
149
|
-
get '/grape/render_collection_with_json_api'
|
150
|
-
assert last_response.ok?
|
151
|
-
|
152
|
-
representation = JSON.parse(last_response.body)
|
153
|
-
assert representation.include?('data')
|
154
|
-
assert representation['data'].count == Models.collection_per
|
155
|
-
assert representation.include?('links')
|
156
|
-
assert representation['links'].count > 0
|
157
|
-
end
|
158
|
-
|
159
|
-
def test_implicit_formatter
|
160
|
-
post = Models.model1
|
161
|
-
serializable_resource = serializable(post, adapter: :json_api)
|
162
|
-
|
163
|
-
with_adapter :json_api do
|
164
|
-
get '/grape/render_with_implicit_formatter'
|
165
|
-
end
|
166
|
-
|
167
|
-
assert last_response.ok?
|
168
|
-
assert_equal serializable_resource.to_json, last_response.body
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_implicit_formatter_handles_arrays
|
172
|
-
posts = Models.all
|
173
|
-
serializable_resource = serializable(posts, adapter: :json_api)
|
174
|
-
|
175
|
-
with_adapter :json_api do
|
176
|
-
get '/grape/render_array_with_implicit_formatter'
|
177
|
-
end
|
178
|
-
|
179
|
-
assert last_response.ok?
|
180
|
-
assert_equal serializable_resource.to_json, last_response.body
|
181
|
-
ensure
|
182
|
-
Models.reset_all
|
183
|
-
end
|
184
|
-
|
185
|
-
def test_implicit_formatter_handles_collections
|
186
|
-
with_adapter :json_api do
|
187
|
-
get '/grape/render_collection_with_implicit_formatter'
|
188
|
-
end
|
189
|
-
|
190
|
-
representation = JSON.parse(last_response.body)
|
191
|
-
assert last_response.ok?
|
192
|
-
assert representation.include?('data')
|
193
|
-
assert representation['data'].count == Models.collection_per
|
194
|
-
assert representation.include?('links')
|
195
|
-
assert representation['links'].count > 0
|
196
|
-
end
|
197
|
-
end
|
198
|
-
end
|
data/test/lint_test.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModel
|
6
|
-
class Serializer
|
7
|
-
class LintTest < ActiveSupport::TestCase
|
8
|
-
include ActiveModel::Serializer::Lint::Tests
|
9
|
-
|
10
|
-
class CompliantResource
|
11
|
-
def serializable_hash(options = nil)
|
12
|
-
end
|
13
|
-
|
14
|
-
def read_attribute_for_serialization(name)
|
15
|
-
end
|
16
|
-
|
17
|
-
def as_json(options = nil)
|
18
|
-
end
|
19
|
-
|
20
|
-
def to_json(options = nil)
|
21
|
-
end
|
22
|
-
|
23
|
-
def cache_key
|
24
|
-
end
|
25
|
-
|
26
|
-
def id
|
27
|
-
end
|
28
|
-
|
29
|
-
def updated_at
|
30
|
-
end
|
31
|
-
|
32
|
-
def errors
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.human_attribute_name(_, _ = {})
|
36
|
-
end
|
37
|
-
|
38
|
-
def self.lookup_ancestors
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.model_name
|
42
|
-
@_model_name ||= ActiveModel::Name.new(self)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def setup
|
47
|
-
@resource = CompliantResource.new
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
data/test/logger_test.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModelSerializers
|
6
|
-
class LoggerTest < ActiveSupport::TestCase
|
7
|
-
def test_logger_is_set_to_action_controller_logger_when_initializer_runs
|
8
|
-
assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_logger_can_be_set
|
12
|
-
original_logger = ActiveModelSerializers.logger
|
13
|
-
logger = Logger.new(STDOUT)
|
14
|
-
|
15
|
-
ActiveModelSerializers.logger = logger
|
16
|
-
|
17
|
-
assert_equal ActiveModelSerializers.logger, logger
|
18
|
-
ensure
|
19
|
-
ActiveModelSerializers.logger = original_logger
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
data/test/poro_test.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
module ActiveModelSerializers
|
6
|
-
class SerializableResourceTest < ActiveSupport::TestCase
|
7
|
-
def setup
|
8
|
-
@resource = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
9
|
-
@serializer = ProfileSerializer.new(@resource)
|
10
|
-
@adapter = ActiveModelSerializers::Adapter.create(@serializer)
|
11
|
-
@serializable_resource = SerializableResource.new(@resource)
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_deprecation
|
15
|
-
assert_output(nil, /deprecated/) do
|
16
|
-
deprecated_serializable_resource = ActiveModel::SerializableResource.new(@resource)
|
17
|
-
assert_equal(@serializable_resource.as_json, deprecated_serializable_resource.as_json)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_serializable_resource_delegates_serializable_hash_to_the_adapter
|
22
|
-
options = nil
|
23
|
-
assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_serializable_resource_delegates_to_json_to_the_adapter
|
27
|
-
options = nil
|
28
|
-
assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_serializable_resource_delegates_as_json_to_the_adapter
|
32
|
-
options = nil
|
33
|
-
assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_use_adapter_with_adapter_option
|
37
|
-
assert SerializableResource.new(@resource, adapter: 'json').use_adapter?
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_use_adapter_with_adapter_option_as_false
|
41
|
-
refute SerializableResource.new(@resource, adapter: false).use_adapter?
|
42
|
-
end
|
43
|
-
|
44
|
-
class SerializableResourceErrorsTest < Minitest::Test
|
45
|
-
def test_serializable_resource_with_errors
|
46
|
-
options = nil
|
47
|
-
resource = ModelWithErrors.new
|
48
|
-
resource.errors.add(:name, 'must be awesome')
|
49
|
-
serializable_resource = ActiveModelSerializers::SerializableResource.new(
|
50
|
-
resource, serializer: ActiveModel::Serializer::ErrorSerializer,
|
51
|
-
adapter: :json_api
|
52
|
-
)
|
53
|
-
expected_response_document = {
|
54
|
-
errors: [
|
55
|
-
{ source: { pointer: '/data/attributes/name' }, detail: 'must be awesome' }
|
56
|
-
]
|
57
|
-
}
|
58
|
-
assert_equal serializable_resource.as_json(options), expected_response_document
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_serializable_resource_with_collection_containing_errors
|
62
|
-
options = nil
|
63
|
-
resources = []
|
64
|
-
resources << resource = ModelWithErrors.new
|
65
|
-
resource.errors.add(:title, 'must be amazing')
|
66
|
-
resources << ModelWithErrors.new
|
67
|
-
serializable_resource = SerializableResource.new(
|
68
|
-
resources, serializer: ActiveModel::Serializer::ErrorsSerializer,
|
69
|
-
each_serializer: ActiveModel::Serializer::ErrorSerializer,
|
70
|
-
adapter: :json_api
|
71
|
-
)
|
72
|
-
expected_response_document = {
|
73
|
-
errors: [
|
74
|
-
{ source: { pointer: '/data/attributes/title' }, detail: 'must be amazing' }
|
75
|
-
]
|
76
|
-
}
|
77
|
-
assert_equal serializable_resource.as_json(options), expected_response_document
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|