active_model_serializers 0.8.3 → 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 +6 -0
- data/.rubocop.yml +86 -0
- data/.rubocop_todo.yml +240 -0
- data/.simplecov +111 -0
- data/.travis.yml +33 -22
- data/CHANGELOG.md +358 -6
- data/CONTRIBUTING.md +220 -0
- data/Gemfile +46 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +81 -591
- data/Rakefile +68 -11
- data/active_model_serializers.gemspec +57 -23
- data/appveyor.yml +27 -0
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +35 -0
- data/docs/general/adapters.md +162 -0
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +27 -0
- data/docs/general/getting_started.md +98 -0
- 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 +121 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +58 -0
- 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/schema.json +366 -0
- data/docs/jsonapi/schema.md +140 -0
- data/lib/action_controller/serialization.rb +41 -37
- data/lib/active_model/serializable_resource.rb +72 -0
- 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 +111 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
- data/lib/active_model/serializer/adapter/json_api.rb +223 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +91 -0
- data/lib/active_model/serializer/array_serializer.rb +9 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +87 -220
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +28 -0
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- 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 +142 -0
- data/lib/active_model/serializer/reflection.rb +91 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +99 -479
- 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/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +20 -92
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -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 +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- 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 +196 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
- data/test/action_controller/serialization_test.rb +435 -0
- 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 +9 -0
- data/test/adapter/fragment_cache_test.rb +38 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +92 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +97 -0
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +394 -0
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +71 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +36 -73
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +229 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +57 -0
- 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 +40 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +166 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +267 -0
- data/test/serializers/attribute_test.rb +123 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/cache_test.rb +209 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +130 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +134 -0
- 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 +25 -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 +53 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +19 -0
- data/test/test_helper.rb +55 -24
- metadata +358 -42
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class LoggingTest < ActiveSupport::TestCase
|
6
|
+
class TestLogger < ActiveSupport::Logger
|
7
|
+
def initialize
|
8
|
+
@file = StringIO.new
|
9
|
+
super(@file)
|
10
|
+
end
|
11
|
+
|
12
|
+
def messages
|
13
|
+
@file.rewind
|
14
|
+
@file.read
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup
|
19
|
+
@author = Author.new(name: 'Steve K.')
|
20
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
21
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
22
|
+
@post.comments = [@comment]
|
23
|
+
@comment.post = @post
|
24
|
+
@post.author = @author
|
25
|
+
@author.posts = [@post]
|
26
|
+
@post_serializer = PostSerializer.new(@post, custom_options: true)
|
27
|
+
|
28
|
+
@old_logger = ActiveModelSerializers.logger
|
29
|
+
@logger = ActiveSupport::TaggedLogging.new(TestLogger.new)
|
30
|
+
logger @logger
|
31
|
+
end
|
32
|
+
|
33
|
+
def teardown
|
34
|
+
logger @old_logger
|
35
|
+
end
|
36
|
+
|
37
|
+
def logger(logger)
|
38
|
+
ActiveModelSerializers.logger = logger
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_uses_ams_as_tag
|
42
|
+
ActiveModel::SerializableResource.new(@post).serializable_hash
|
43
|
+
assert_match(/\[active_model_serializers\]/, @logger.messages)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_logs_when_call_serializable_hash
|
47
|
+
ActiveModel::SerializableResource.new(@post).serializable_hash
|
48
|
+
assert_match(/Rendered/, @logger.messages)
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_logs_when_call_as_json
|
52
|
+
ActiveModel::SerializableResource.new(@post).as_json
|
53
|
+
assert_match(/Rendered/, @logger.messages)
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_logs_when_call_to_json
|
57
|
+
ActiveModel::SerializableResource.new(@post).to_json
|
58
|
+
assert_match(/Rendered/, @logger.messages)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_logs_correct_serializer
|
62
|
+
ActiveModel::SerializableResource.new(@post).serializable_hash
|
63
|
+
assert_match(/PostSerializer/, @logger.messages)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_logs_correct_adapter
|
67
|
+
ActiveModel::SerializableResource.new(@post).serializable_hash
|
68
|
+
assert_match(/ActiveModel::Serializer::Adapter::Attributes/, @logger.messages)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_logs_the_duration
|
72
|
+
ActiveModel::SerializableResource.new(@post).serializable_hash
|
73
|
+
assert_match(/\(\d+\.\d+ms\)/, @logger.messages)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# Execute this test in isolation
|
2
|
+
require 'support/isolated_unit'
|
3
|
+
|
4
|
+
class RailtieTest < ActiveSupport::TestCase
|
5
|
+
include ActiveSupport::Testing::Isolation
|
6
|
+
|
7
|
+
class WithRails < RailtieTest
|
8
|
+
setup do
|
9
|
+
require 'rails'
|
10
|
+
require 'active_model_serializers'
|
11
|
+
make_basic_app
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'mixes ActionController::Serialization into ActionController::Base' do
|
15
|
+
assert ActionController.const_defined?(:Serialization),
|
16
|
+
"ActionController::Serialization should be defined, but isn't"
|
17
|
+
assert ::ActionController::Base.included_modules.include?(::ActionController::Serialization),
|
18
|
+
"ActionController::Serialization should be included in ActionController::Base, but isn't"
|
19
|
+
end
|
20
|
+
|
21
|
+
test 'sets the ActiveModelSerializers.logger to Rails.logger' do
|
22
|
+
refute_nil Rails.logger
|
23
|
+
refute_nil ActiveModelSerializers.logger
|
24
|
+
assert_equal Rails.logger, ActiveModelSerializers.logger
|
25
|
+
end
|
26
|
+
|
27
|
+
test 'it is configured for caching' do
|
28
|
+
assert_equal ActionController::Base.cache_store, ActiveModelSerializers.config.cache_store
|
29
|
+
assert_equal Rails.configuration.action_controller.perform_caching, ActiveModelSerializers.config.perform_caching
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class WithoutRails < RailtieTest
|
34
|
+
setup do
|
35
|
+
require 'active_model_serializers'
|
36
|
+
make_basic_app
|
37
|
+
end
|
38
|
+
|
39
|
+
test 'does not mix ActionController::Serialization into ActionController::Base' do
|
40
|
+
refute ActionController.const_defined?(:Serialization),
|
41
|
+
'ActionController::Serialization should not be defined, but is'
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'has its own logger at ActiveModelSerializers.logger' do
|
45
|
+
refute_nil Rails.logger
|
46
|
+
refute_nil ActiveModelSerializers.logger
|
47
|
+
refute_equal Rails.logger, ActiveModelSerializers.logger
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'it is not configured for caching' do
|
51
|
+
refute_nil ActionController::Base.cache_store
|
52
|
+
assert_nil ActiveModelSerializers.config.cache_store
|
53
|
+
refute Rails.configuration.action_controller.perform_caching
|
54
|
+
refute ActiveModelSerializers.config.perform_caching
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -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
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
module ActiveModel
|
3
|
+
class Serializer
|
4
|
+
module Adapter
|
5
|
+
class FragmentCacheTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@spam = Spam::UnrelatedLink.new(id: 'spam-id-1')
|
9
|
+
@author = Author.new(name: 'Joao M. D. Moura')
|
10
|
+
@role = Role.new(name: 'Great Author', description: nil)
|
11
|
+
@role.author = [@author]
|
12
|
+
@role_serializer = RoleSerializer.new(@role)
|
13
|
+
@spam_serializer = Spam::UnrelatedLinkSerializer.new(@spam)
|
14
|
+
@role_hash = FragmentCache.new(RoleSerializer.adapter.new(@role_serializer), @role_serializer, {})
|
15
|
+
@spam_hash = FragmentCache.new(Spam::UnrelatedLinkSerializer.adapter.new(@spam_serializer), @spam_serializer, {})
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_fragment_fetch_with_virtual_attributes
|
19
|
+
expected_result = {
|
20
|
+
id: @role.id,
|
21
|
+
description: @role.description,
|
22
|
+
slug: "#{@role.name}-#{@role.id}",
|
23
|
+
name: @role.name
|
24
|
+
}
|
25
|
+
assert_equal(@role_hash.fetch, expected_result)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_fragment_fetch_with_namespaced_object
|
29
|
+
expected_result = {
|
30
|
+
id: @spam.id
|
31
|
+
}
|
32
|
+
assert_equal(@spam_hash.fetch, expected_result)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Json
|
7
|
+
class BelongsToTest < ActiveSupport::TestCase
|
8
|
+
def setup
|
9
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
10
|
+
@anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
|
11
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
12
|
+
@post.comments = [@comment]
|
13
|
+
@anonymous_post.comments = []
|
14
|
+
@comment.post = @post
|
15
|
+
@comment.author = nil
|
16
|
+
@anonymous_post.author = nil
|
17
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
18
|
+
@post.blog = @blog
|
19
|
+
@anonymous_post.blog = nil
|
20
|
+
|
21
|
+
@serializer = CommentSerializer.new(@comment)
|
22
|
+
@adapter = ActiveModel::Serializer::Adapter::Json.new(@serializer)
|
23
|
+
ActionController::Base.cache_store.clear
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_includes_post
|
27
|
+
assert_equal({ id: 42, title: 'New Post', body: 'Body' }, @adapter.serializable_hash[:comment][:post])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_include_nil_author
|
31
|
+
serializer = PostSerializer.new(@anonymous_post)
|
32
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
33
|
+
|
34
|
+
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], blog: { id: 999, name: 'Custom blog' }, author: nil } }, adapter.serializable_hash)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_include_nil_author_with_specified_serializer
|
38
|
+
serializer = PostPreviewSerializer.new(@anonymous_post)
|
39
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
40
|
+
|
41
|
+
assert_equal({ post: { title: 'Hello!!', body: 'Hello, world!!', id: 43, comments: [], author: nil } }, adapter.serializable_hash)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Json
|
7
|
+
class Collection < ActiveSupport::TestCase
|
8
|
+
def setup
|
9
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
10
|
+
@first_post = Post.new(id: 1, title: 'Hello!!', body: 'Hello, world!!')
|
11
|
+
@second_post = Post.new(id: 2, title: 'New Post', body: 'Body')
|
12
|
+
@first_post.comments = []
|
13
|
+
@second_post.comments = []
|
14
|
+
@first_post.author = @author
|
15
|
+
@second_post.author = @author
|
16
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
17
|
+
@first_post.blog = @blog
|
18
|
+
@second_post.blog = nil
|
19
|
+
|
20
|
+
ActionController::Base.cache_store.clear
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_with_serializer_option
|
24
|
+
@blog.special_attribute = 'Special'
|
25
|
+
@blog.articles = [@first_post, @second_post]
|
26
|
+
serializer = CollectionSerializer.new([@blog], serializer: CustomBlogSerializer)
|
27
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
28
|
+
|
29
|
+
expected = { blogs: [{
|
30
|
+
id: 1,
|
31
|
+
special_attribute: 'Special',
|
32
|
+
articles: [{ id: 1, title: 'Hello!!', body: 'Hello, world!!' }, { id: 2, title: 'New Post', body: 'Body' }]
|
33
|
+
}] }
|
34
|
+
assert_equal expected, adapter.serializable_hash
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_include_multiple_posts
|
38
|
+
serializer = CollectionSerializer.new([@first_post, @second_post])
|
39
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
40
|
+
|
41
|
+
expected = { posts: [{
|
42
|
+
title: 'Hello!!',
|
43
|
+
body: 'Hello, world!!',
|
44
|
+
id: 1,
|
45
|
+
comments: [],
|
46
|
+
author: {
|
47
|
+
id: 1,
|
48
|
+
name: 'Steve K.'
|
49
|
+
},
|
50
|
+
blog: {
|
51
|
+
id: 999,
|
52
|
+
name: 'Custom blog'
|
53
|
+
}
|
54
|
+
}, {
|
55
|
+
title: 'New Post',
|
56
|
+
body: 'Body',
|
57
|
+
id: 2,
|
58
|
+
comments: [],
|
59
|
+
author: {
|
60
|
+
id: 1,
|
61
|
+
name: 'Steve K.'
|
62
|
+
},
|
63
|
+
blog: {
|
64
|
+
id: 999,
|
65
|
+
name: 'Custom blog'
|
66
|
+
}
|
67
|
+
}] }
|
68
|
+
assert_equal expected, adapter.serializable_hash
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_root_is_underscored
|
72
|
+
virtual_value = VirtualValue.new(id: 1)
|
73
|
+
serializer = CollectionSerializer.new([virtual_value])
|
74
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
75
|
+
|
76
|
+
assert_equal 1, adapter.serializable_hash[:virtual_values].length
|
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
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
module Adapter
|
6
|
+
class Json
|
7
|
+
class HasManyTestTest < ActiveSupport::TestCase
|
8
|
+
def setup
|
9
|
+
ActionController::Base.cache_store.clear
|
10
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
11
|
+
@post = Post.new(id: 42, title: 'New Post', body: 'Body')
|
12
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
13
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
14
|
+
@post.comments = [@first_comment, @second_comment]
|
15
|
+
@post.author = @author
|
16
|
+
@first_comment.post = @post
|
17
|
+
@second_comment.post = @post
|
18
|
+
@blog = Blog.new(id: 1, name: 'My Blog!!')
|
19
|
+
@post.blog = @blog
|
20
|
+
@tag = Tag.new(id: 1, name: '#hash_tag')
|
21
|
+
@post.tags = [@tag]
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_has_many
|
25
|
+
serializer = PostSerializer.new(@post)
|
26
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
27
|
+
assert_equal([
|
28
|
+
{ id: 1, body: 'ZOMG A COMMENT' },
|
29
|
+
{ id: 2, body: 'ZOMG ANOTHER COMMENT' }
|
30
|
+
], adapter.serializable_hash[:post][:comments])
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_has_many_with_no_serializer
|
34
|
+
serializer = PostWithTagsSerializer.new(@post)
|
35
|
+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
|
36
|
+
assert_equal({
|
37
|
+
id: 42,
|
38
|
+
tags: [
|
39
|
+
{ 'id' => 1, 'name' => '#hash_tag' }
|
40
|
+
]
|
41
|
+
}.to_json, adapter.serializable_hash[:post].to_json)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|