active_model_serializers 0.10.0 → 0.10.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -4
- data/.travis.yml +17 -5
- data/CHANGELOG.md +112 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +166 -26
- data/Rakefile +3 -3
- data/active_model_serializers.gemspec +21 -24
- data/appveyor.yml +9 -3
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +4 -2
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +44 -20
- data/docs/general/serializers.md +100 -11
- data/docs/howto/add_pagination_links.md +15 -16
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +64 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +19 -4
- data/lib/active_model/serializer/belongs_to_reflection.rb +0 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{associations.rb → concerns/associations.rb} +13 -11
- data/lib/active_model/serializer/{attributes.rb → concerns/attributes.rb} +1 -1
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +72 -113
- data/lib/active_model/serializer/{configuration.rb → concerns/configuration.rb} +25 -1
- data/lib/active_model/serializer/{links.rb → concerns/links.rb} +0 -0
- data/lib/active_model/serializer/{meta.rb → concerns/meta.rb} +0 -0
- data/lib/active_model/serializer/{type.rb → concerns/type.rb} +0 -0
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/has_many_reflection.rb +0 -3
- data/lib/active_model/serializer/has_one_reflection.rb +0 -3
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +37 -21
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +76 -37
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +39 -39
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +30 -19
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +23 -9
- data/lib/active_model_serializers/adapter/json_api.rb +44 -43
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +108 -28
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/active_model_serializers.rb +15 -0
- data/lib/generators/rails/resource_override.rb +1 -1
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/test/action_controller/adapter_selector_test.rb +14 -5
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +8 -9
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/json_api/transform_test.rb +11 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +12 -6
- data/test/action_controller/serialization_test.rb +12 -9
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/model_test.rb +137 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/active_model_serializers/test/schema_test.rb +3 -2
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/has_many_test.rb +10 -2
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +12 -3
- data/test/adapter/json_api/has_many_test.rb +49 -20
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +263 -253
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +21 -10
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +235 -69
- data/test/collection_serializer_test.rb +25 -12
- data/test/fixtures/active_record.rb +45 -10
- data/test/fixtures/poro.rb +124 -181
- data/test/generators/serializer_generator_test.rb +23 -5
- data/test/grape_test.rb +170 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +132 -36
- data/test/serializers/attribute_test.rb +5 -3
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/caching_configuration_test_isolated.rb +6 -6
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/options_test.rb +17 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serialization_test.rb +2 -2
- data/test/serializers/serializer_for_test.rb +12 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +13 -0
- metadata +104 -38
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -28,8 +28,8 @@ module ActiveModel
|
|
28
28
|
|
29
29
|
class SerializerTest < ActiveSupport::TestCase
|
30
30
|
module ResourceNamespace
|
31
|
-
Post
|
32
|
-
Comment
|
31
|
+
class Post < ::Model; end
|
32
|
+
class Comment < ::Model; end
|
33
33
|
|
34
34
|
class PostSerializer < ActiveModel::Serializer
|
35
35
|
class CommentSerializer < ActiveModel::Serializer
|
@@ -41,10 +41,12 @@ module ActiveModel
|
|
41
41
|
end
|
42
42
|
|
43
43
|
class CustomProfile
|
44
|
-
def serializer_class
|
44
|
+
def serializer_class
|
45
|
+
ProfileSerializer
|
46
|
+
end
|
45
47
|
end
|
46
48
|
|
47
|
-
Tweet
|
49
|
+
class Tweet < ::Model; end
|
48
50
|
TweetSerializer = Class.new
|
49
51
|
|
50
52
|
def setup
|
@@ -57,7 +59,7 @@ module ActiveModel
|
|
57
59
|
|
58
60
|
def test_serializer_for_non_ams_serializer
|
59
61
|
serializer = ActiveModel::Serializer.serializer_for(@tweet)
|
60
|
-
|
62
|
+
assert_nil serializer
|
61
63
|
end
|
62
64
|
|
63
65
|
def test_serializer_for_existing_serializer
|
@@ -69,12 +71,12 @@ module ActiveModel
|
|
69
71
|
serializer = with_serializer_lookup_disabled do
|
70
72
|
ActiveModel::Serializer.serializer_for(@profile)
|
71
73
|
end
|
72
|
-
|
74
|
+
assert_nil serializer
|
73
75
|
end
|
74
76
|
|
75
77
|
def test_serializer_for_not_existing_serializer
|
76
78
|
serializer = ActiveModel::Serializer.serializer_for(@model)
|
77
|
-
|
79
|
+
assert_nil serializer
|
78
80
|
end
|
79
81
|
|
80
82
|
def test_serializer_inherited_serializer
|
@@ -86,7 +88,7 @@ module ActiveModel
|
|
86
88
|
serializer = with_serializer_lookup_disabled do
|
87
89
|
ActiveModel::Serializer.serializer_for(@my_profile)
|
88
90
|
end
|
89
|
-
|
91
|
+
assert_nil serializer
|
90
92
|
end
|
91
93
|
|
92
94
|
def test_serializer_custom_serializer
|
@@ -112,7 +114,7 @@ module ActiveModel
|
|
112
114
|
serializer = with_serializer_lookup_disabled do
|
113
115
|
ActiveModel::Serializer.serializer_for(post)
|
114
116
|
end
|
115
|
-
|
117
|
+
assert_nil serializer
|
116
118
|
end
|
117
119
|
|
118
120
|
def test_serializer_for_nested_resource
|
@@ -126,7 +128,7 @@ module ActiveModel
|
|
126
128
|
serializer = with_serializer_lookup_disabled do
|
127
129
|
ResourceNamespace::PostSerializer.serializer_for(comment)
|
128
130
|
end
|
129
|
-
|
131
|
+
assert_nil serializer
|
130
132
|
end
|
131
133
|
end
|
132
134
|
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActiveModel
|
4
|
+
class Serializer
|
5
|
+
class SerializerForWithNamespaceTest < ActiveSupport::TestCase
|
6
|
+
class Book < ::Model
|
7
|
+
attributes :title, :author_name
|
8
|
+
associations :publisher, :pages
|
9
|
+
end
|
10
|
+
class Page < ::Model; attributes :number, :text end
|
11
|
+
class Publisher < ::Model; attributes :name end
|
12
|
+
|
13
|
+
module Api
|
14
|
+
module V3
|
15
|
+
class BookSerializer < ActiveModel::Serializer
|
16
|
+
attributes :title, :author_name
|
17
|
+
|
18
|
+
has_many :pages
|
19
|
+
belongs_to :publisher
|
20
|
+
end
|
21
|
+
|
22
|
+
class PageSerializer < ActiveModel::Serializer
|
23
|
+
attributes :number, :text
|
24
|
+
end
|
25
|
+
|
26
|
+
class PublisherSerializer < ActiveModel::Serializer
|
27
|
+
attributes :name
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class BookSerializer < ActiveModel::Serializer
|
33
|
+
attributes :title, :author_name
|
34
|
+
end
|
35
|
+
test 'resource without a namespace' do
|
36
|
+
book = Book.new(title: 'A Post', author_name: 'hello')
|
37
|
+
|
38
|
+
# TODO: this should be able to pull up this serializer without explicitly specifying the serializer
|
39
|
+
# currently, with no options, it still uses the Api::V3 serializer
|
40
|
+
result = ActiveModelSerializers::SerializableResource.new(book, serializer: BookSerializer).serializable_hash
|
41
|
+
|
42
|
+
expected = { title: 'A Post', author_name: 'hello' }
|
43
|
+
assert_equal expected, result
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'resource with namespace' do
|
47
|
+
book = Book.new(title: 'A Post', author_name: 'hi')
|
48
|
+
|
49
|
+
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
50
|
+
|
51
|
+
expected = { title: 'A Post', author_name: 'hi', pages: nil, publisher: nil }
|
52
|
+
assert_equal expected, result
|
53
|
+
end
|
54
|
+
|
55
|
+
test 'has_many with nested serializer under the namespace' do
|
56
|
+
page = Page.new(number: 1, text: 'hello')
|
57
|
+
book = Book.new(title: 'A Post', author_name: 'hi', pages: [page])
|
58
|
+
|
59
|
+
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
60
|
+
|
61
|
+
expected = {
|
62
|
+
title: 'A Post', author_name: 'hi',
|
63
|
+
publisher: nil,
|
64
|
+
pages: [{
|
65
|
+
number: 1, text: 'hello'
|
66
|
+
}]
|
67
|
+
}
|
68
|
+
assert_equal expected, result
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'belongs_to with nested serializer under the namespace' do
|
72
|
+
publisher = Publisher.new(name: 'Disney')
|
73
|
+
book = Book.new(title: 'A Post', author_name: 'hi', publisher: publisher)
|
74
|
+
|
75
|
+
result = ActiveModelSerializers::SerializableResource.new(book, namespace: Api::V3).serializable_hash
|
76
|
+
|
77
|
+
expected = {
|
78
|
+
title: 'A Post', author_name: 'hi',
|
79
|
+
pages: nil,
|
80
|
+
publisher: {
|
81
|
+
name: 'Disney'
|
82
|
+
}
|
83
|
+
}
|
84
|
+
assert_equal expected, result
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -41,6 +41,7 @@ require 'active_support/core_ext/string/access'
|
|
41
41
|
|
42
42
|
# These files do not require any others and are needed
|
43
43
|
# to run the tests
|
44
|
+
require 'active_support/testing/autorun'
|
44
45
|
require 'active_support/testing/isolation'
|
45
46
|
|
46
47
|
module TestHelpers
|
@@ -74,6 +75,8 @@ module TestHelpers
|
|
74
75
|
end
|
75
76
|
end
|
76
77
|
|
77
|
-
|
78
|
-
|
78
|
+
module ActiveSupport
|
79
|
+
class TestCase
|
80
|
+
include TestHelpers::Generation
|
81
|
+
end
|
79
82
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module Rails5Shims
|
2
2
|
module ControllerTests
|
3
3
|
# https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb
|
4
|
-
REQUEST_KWARGS = [:params, :session, :flash, :method, :body, :xhr].freeze
|
4
|
+
REQUEST_KWARGS = [:params, :headers, :session, :flash, :method, :body, :xhr].freeze
|
5
5
|
|
6
6
|
def get(path, *args)
|
7
7
|
fold_kwargs!(args)
|
@@ -30,7 +30,12 @@ module Rails5Shims
|
|
30
30
|
return unless hash.respond_to?(:key)
|
31
31
|
Rails5Shims::ControllerTests::REQUEST_KWARGS.each do |kwarg|
|
32
32
|
next unless hash.key?(kwarg)
|
33
|
-
|
33
|
+
value = hash.delete(kwarg)
|
34
|
+
if value.is_a? String
|
35
|
+
args.insert(0, value)
|
36
|
+
else
|
37
|
+
hash.merge! value
|
38
|
+
end
|
34
39
|
end
|
35
40
|
end
|
36
41
|
|
@@ -44,4 +49,5 @@ module Rails5Shims
|
|
44
49
|
end
|
45
50
|
if Rails::VERSION::MAJOR < 5
|
46
51
|
ActionController::TestCase.send :include, Rails5Shims::ControllerTests
|
52
|
+
ActionDispatch::IntegrationTest.send :include, Rails5Shims::ControllerTests
|
47
53
|
end
|
data/test/support/rails_app.rb
CHANGED
@@ -6,6 +6,8 @@ module ActiveModelSerializers
|
|
6
6
|
config.active_support.test_order = :random
|
7
7
|
config.action_controller.perform_caching = true
|
8
8
|
config.action_controller.cache_store = :memory_store
|
9
|
+
|
10
|
+
config.filter_parameters += [:password]
|
9
11
|
end
|
10
12
|
|
11
13
|
app.routes.default_url_options = { host: 'example.com' }
|
@@ -22,15 +24,6 @@ ActionController::TestCase.class_eval do
|
|
22
24
|
def setup
|
23
25
|
@routes = Routes
|
24
26
|
end
|
25
|
-
|
26
|
-
# For Rails5
|
27
|
-
# https://github.com/rails/rails/commit/ca83436d1b3b6cedd1eca2259f65661e69b01909#diff-b9bbf56e85d3fe1999f16317f2751e76L17
|
28
|
-
def assigns(key = nil)
|
29
|
-
warn "DEPRECATION: Calling 'assigns(#{key})' from #{caller[0]}"
|
30
|
-
assigns = {}.with_indifferent_access
|
31
|
-
@controller.view_assigns.each { |k, v| assigns.regular_writer(k, v) }
|
32
|
-
key.nil? ? assigns : assigns[key]
|
33
|
-
end
|
34
27
|
end
|
35
28
|
|
36
29
|
# ActiveRecord::Migrator.migrations_paths = [File.expand_path("../../test/dummy/db/migrate", __FILE__)]
|
@@ -9,6 +9,22 @@ module SerializationTesting
|
|
9
9
|
ActiveModelSerializers::SerializableResource.new(obj).to_json
|
10
10
|
end
|
11
11
|
|
12
|
+
def with_namespace_separator(separator)
|
13
|
+
original_separator = ActiveModelSerializers.config.jsonapi_namespace_separator
|
14
|
+
ActiveModelSerializers.config.jsonapi_namespace_separator = separator
|
15
|
+
yield
|
16
|
+
ensure
|
17
|
+
ActiveModelSerializers.config.jsonapi_namespace_separator = original_separator
|
18
|
+
end
|
19
|
+
|
20
|
+
def with_prepended_lookup(lookup_proc)
|
21
|
+
original_lookup = ActiveModelSerializers.config.serializer_lookup_cahin
|
22
|
+
ActiveModelSerializers.config.serializer_lookup_chain.unshift lookup_proc
|
23
|
+
yield
|
24
|
+
ensure
|
25
|
+
ActiveModelSerializers.config.serializer_lookup_cahin = original_lookup
|
26
|
+
end
|
27
|
+
|
12
28
|
# Aliased as :with_configured_adapter to clarify that
|
13
29
|
# this method tests the configured adapter.
|
14
30
|
# When not testing configuration, it may be preferable
|
@@ -44,10 +60,12 @@ module SerializationTesting
|
|
44
60
|
end
|
45
61
|
end
|
46
62
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
63
|
+
module Minitest
|
64
|
+
class Test
|
65
|
+
def before_setup
|
66
|
+
ActionController::Base.cache_store.clear
|
67
|
+
end
|
51
68
|
|
52
|
-
|
69
|
+
include SerializationTesting
|
70
|
+
end
|
53
71
|
end
|
data/test/test_helper.rb
CHANGED
@@ -9,6 +9,7 @@ rescue LoadError
|
|
9
9
|
STDERR.puts 'Running without SimpleCov'
|
10
10
|
end
|
11
11
|
|
12
|
+
require 'pry'
|
12
13
|
require 'timecop'
|
13
14
|
require 'rails'
|
14
15
|
require 'action_controller'
|
@@ -39,6 +40,18 @@ require 'minitest'
|
|
39
40
|
require 'minitest/autorun'
|
40
41
|
Minitest.backtrace_filter = Minitest::BacktraceFilter.new
|
41
42
|
|
43
|
+
module TestHelper
|
44
|
+
module_function
|
45
|
+
|
46
|
+
def silence_warnings
|
47
|
+
original_verbose = $VERBOSE
|
48
|
+
$VERBOSE = nil
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
$VERBOSE = original_verbose
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
42
55
|
require 'support/rails_app'
|
43
56
|
|
44
57
|
# require "rails/test_help"
|