active_model_serializers_custom 0.10.90
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +35 -0
- data/.rubocop.yml +109 -0
- data/.simplecov +110 -0
- data/.travis.yml +63 -0
- data/CHANGELOG.md +727 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +74 -0
- data/MIT-LICENSE +22 -0
- data/README.md +305 -0
- data/Rakefile +76 -0
- data/active_model_serializers.gemspec +64 -0
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +76 -0
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer.rb +418 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +91 -0
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +90 -0
- data/lib/active_model/serializer/concerns/caching.rb +304 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +7 -0
- data/lib/active_model_serializers.rb +63 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/adapter/attributes.rb +15 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +90 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +52 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +12 -0
- data/lib/generators/rails/serializer_generator.rb +38 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +64 -0
- data/test/action_controller/explicit_serializer_test.rb +137 -0
- data/test/action_controller/json/include_test.rb +248 -0
- data/test/action_controller/json_api/deserialization_test.rb +114 -0
- data/test/action_controller/json_api/errors_test.rb +42 -0
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +204 -0
- data/test/action_controller/json_api/pagination_test.rb +126 -0
- data/test/action_controller/json_api/transform_test.rb +191 -0
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +239 -0
- data/test/action_controller/serialization_scope_name_test.rb +237 -0
- data/test/action_controller/serialization_test.rb +480 -0
- data/test/active_model_serializers/adapter_for_test.rb +210 -0
- data/test/active_model_serializers/json_pointer_test.rb +24 -0
- data/test/active_model_serializers/logging_test.rb +79 -0
- data/test/active_model_serializers/model_test.rb +144 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +70 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +73 -0
- data/test/active_model_serializers/test/schema_test.rb +133 -0
- data/test/active_model_serializers/test/serializer_test.rb +64 -0
- data/test/active_record_test.rb +11 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +102 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +106 -0
- data/test/adapter/json/has_many_test.rb +55 -0
- data/test/adapter/json/transform_test.rb +95 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +98 -0
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +98 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +175 -0
- data/test/adapter/json_api/has_one_test.rb +82 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +35 -0
- data/test/adapter/json_api/linked_test.rb +415 -0
- data/test/adapter/json_api/links_test.rb +112 -0
- data/test/adapter/json_api/pagination_links_test.rb +208 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/relationship_test.rb +399 -0
- data/test/adapter/json_api/resource_meta_test.rb +102 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_api/transform_test.rb +514 -0
- data/test/adapter/json_api/type_test.rb +195 -0
- data/test/adapter/json_test.rb +48 -0
- data/test/adapter/null_test.rb +24 -0
- data/test/adapter/polymorphic_test.rb +220 -0
- data/test/adapter_test.rb +69 -0
- data/test/array_serializer_test.rb +24 -0
- data/test/benchmark/app.rb +67 -0
- data/test/benchmark/benchmarking_support.rb +69 -0
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +121 -0
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +47 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +85 -0
- data/test/benchmark/fixtures.rb +221 -0
- data/test/cache_test.rb +717 -0
- data/test/collection_serializer_test.rb +129 -0
- data/test/fixtures/active_record.rb +115 -0
- data/test/fixtures/poro.rb +227 -0
- data/test/generators/scaffold_controller_generator_test.rb +26 -0
- data/test/generators/serializer_generator_test.rb +77 -0
- data/test/grape_test.rb +198 -0
- data/test/lint_test.rb +51 -0
- data/test/logger_test.rb +22 -0
- data/test/poro_test.rb +11 -0
- data/test/serializable_resource_test.rb +81 -0
- data/test/serializers/association_macros_test.rb +39 -0
- data/test/serializers/associations_test.rb +520 -0
- data/test/serializers/attribute_test.rb +155 -0
- data/test/serializers/attributes_test.rb +54 -0
- data/test/serializers/caching_configuration_test_isolated.rb +172 -0
- data/test/serializers/configuration_test.rb +34 -0
- data/test/serializers/fieldset_test.rb +16 -0
- data/test/serializers/meta_test.rb +204 -0
- data/test/serializers/options_test.rb +34 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +81 -0
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serialization_test.rb +57 -0
- data/test/serializers/serializer_for_test.rb +138 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +86 -0
- data/test/support/rails5_shims.rb +55 -0
- data/test/support/rails_app.rb +40 -0
- 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 +81 -0
- data/test/test_helper.rb +72 -0
- metadata +622 -0
@@ -0,0 +1,26 @@
|
|
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
|
@@ -0,0 +1,77 @@
|
|
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
ADDED
@@ -0,0 +1,198 @@
|
|
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
ADDED
@@ -0,0 +1,51 @@
|
|
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
ADDED
@@ -0,0 +1,22 @@
|
|
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
ADDED
@@ -0,0 +1,81 @@
|
|
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
|