active_model_serializers 0.10.0.rc2 → 0.10.0.rc3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.rubocop.yml +82 -0
- data/.rubocop_todo.yml +315 -0
- data/.simplecov +99 -0
- data/.travis.yml +8 -0
- data/CHANGELOG.md +9 -3
- data/Gemfile +39 -8
- data/README.md +55 -31
- data/Rakefile +29 -2
- data/active_model_serializers.gemspec +37 -13
- data/appveyor.yml +25 -0
- data/docs/README.md +29 -0
- data/docs/general/adapters.md +110 -0
- data/docs/general/configuration_options.md +11 -0
- data/docs/general/getting_started.md +73 -0
- data/docs/howto/add_pagination_links.md +112 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +42 -0
- data/lib/action_controller/serialization.rb +24 -33
- data/lib/active_model/serializable_resource.rb +70 -0
- data/lib/active_model/serializer.rb +50 -131
- data/lib/active_model/serializer/adapter.rb +84 -21
- data/lib/active_model/serializer/adapter/flatten_json.rb +9 -9
- data/lib/active_model/serializer/adapter/fragment_cache.rb +10 -13
- data/lib/active_model/serializer/adapter/json.rb +25 -28
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +2 -12
- data/lib/active_model/serializer/adapter/json_api.rb +100 -98
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +4 -14
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +50 -0
- data/lib/active_model/serializer/adapter/null.rb +2 -8
- data/lib/active_model/serializer/array_serializer.rb +22 -17
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +97 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/configuration.rb +1 -0
- data/lib/active_model/serializer/fieldset.rb +7 -7
- 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/lint.rb +129 -0
- data/lib/active_model/serializer/railtie.rb +7 -0
- data/lib/active_model/serializer/reflection.rb +74 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/utils.rb +35 -0
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +28 -14
- data/lib/generators/serializer/serializer_generator.rb +7 -7
- data/lib/generators/serializer/templates/{serializer.rb → serializer.rb.erb} +2 -2
- data/lib/tasks/rubocop.rake +0 -0
- data/test/action_controller/adapter_selector_test.rb +3 -3
- data/test/action_controller/explicit_serializer_test.rb +9 -9
- data/test/action_controller/json_api/linked_test.rb +179 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/action_controller/serialization_scope_name_test.rb +10 -6
- data/test/action_controller/serialization_test.rb +149 -112
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +11 -1
- data/test/adapter/json/belongs_to_test.rb +4 -5
- data/test/adapter/json/collection_test.rb +30 -21
- data/test/adapter/json/has_many_test.rb +20 -9
- data/test/adapter/json_api/belongs_to_test.rb +38 -38
- data/test/adapter/json_api/collection_test.rb +22 -23
- data/test/adapter/json_api/has_many_embed_ids_test.rb +2 -2
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +4 -4
- data/test/adapter/json_api/has_many_test.rb +54 -19
- data/test/adapter/json_api/has_one_test.rb +28 -8
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +75 -75
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/resource_type_config_test.rb +59 -0
- data/test/adapter/json_test.rb +18 -5
- data/test/adapter_test.rb +10 -11
- data/test/array_serializer_test.rb +63 -5
- data/test/capture_warnings.rb +65 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +60 -29
- data/test/generators/scaffold_controller_generator_test.rb +1 -2
- data/test/generators/serializer_generator_test.rb +17 -12
- data/test/lint_test.rb +37 -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 +123 -3
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +70 -47
- data/test/serializers/attribute_test.rb +28 -4
- data/test/serializers/attributes_test.rb +8 -14
- data/test/serializers/cache_test.rb +58 -31
- data/test/serializers/fieldset_test.rb +3 -4
- data/test/serializers/meta_test.rb +42 -28
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +1 -1
- data/test/support/rails_app.rb +21 -0
- data/test/support/serialization_testing.rb +13 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +5 -0
- data/test/test_helper.rb +41 -29
- data/test/utils/include_args_to_hash_test.rb +79 -0
- metadata +123 -17
- data/test/action_controller/json_api_linked_test.rb +0 -179
- data/test/action_controller/rescue_from_test.rb +0 -32
- data/test/serializers/urls_test.rb +0 -26
@@ -0,0 +1,116 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'will_paginate/array'
|
3
|
+
require 'kaminari'
|
4
|
+
require 'kaminari/hooks'
|
5
|
+
::Kaminari::Hooks.init
|
6
|
+
|
7
|
+
module ActionController
|
8
|
+
module Serialization
|
9
|
+
class JsonApi
|
10
|
+
class PaginationTest < ActionController::TestCase
|
11
|
+
KAMINARI_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_kaminari'
|
12
|
+
WILL_PAGINATE_URI = 'http://test.host/action_controller/serialization/json_api/pagination_test/pagination_test/render_pagination_using_will_paginate'
|
13
|
+
|
14
|
+
class PaginationTestController < ActionController::Base
|
15
|
+
def setup
|
16
|
+
@array = [
|
17
|
+
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
18
|
+
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' }),
|
19
|
+
Profile.new({ name: 'Name 3', description: 'Description 3', comments: 'Comments 3' })
|
20
|
+
]
|
21
|
+
end
|
22
|
+
|
23
|
+
def using_kaminari
|
24
|
+
setup
|
25
|
+
Kaminari.paginate_array(@array).page(params[:page][:number]).per(params[:page][:size])
|
26
|
+
end
|
27
|
+
|
28
|
+
def using_will_paginate
|
29
|
+
setup
|
30
|
+
@array.paginate(page: params[:page][:number], per_page: params[:page][:size])
|
31
|
+
end
|
32
|
+
|
33
|
+
def render_pagination_using_kaminari
|
34
|
+
render json: using_kaminari, adapter: :json_api
|
35
|
+
end
|
36
|
+
|
37
|
+
def render_pagination_using_will_paginate
|
38
|
+
render json: using_will_paginate, adapter: :json_api
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_array_without_pagination_links
|
42
|
+
setup
|
43
|
+
render json: @array, adapter: :json_api
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
tests PaginationTestController
|
48
|
+
|
49
|
+
def test_render_pagination_links_with_will_paginate
|
50
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
51
|
+
'first' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
52
|
+
'prev' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
53
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
54
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
55
|
+
|
56
|
+
get :render_pagination_using_will_paginate, page: { number: 2, size: 1 }
|
57
|
+
response = JSON.parse(@response.body)
|
58
|
+
assert_equal expected_links, response['links']
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_render_only_last_and_next_pagination_links
|
62
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2",
|
63
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2",
|
64
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2" }
|
65
|
+
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }
|
66
|
+
response = JSON.parse(@response.body)
|
67
|
+
assert_equal expected_links, response['links']
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_render_pagination_links_with_kaminari
|
71
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1",
|
72
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
73
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
74
|
+
'next' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
75
|
+
'last' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1" }
|
76
|
+
get :render_pagination_using_kaminari, page: { number: 2, size: 1 }
|
77
|
+
response = JSON.parse(@response.body)
|
78
|
+
assert_equal expected_links, response['links']
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_render_only_prev_and_first_pagination_links
|
82
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1",
|
83
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1",
|
84
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1" }
|
85
|
+
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }
|
86
|
+
response = JSON.parse(@response.body)
|
87
|
+
assert_equal expected_links, response['links']
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_render_only_last_and_next_pagination_links_with_additional_params
|
91
|
+
expected_links = { 'self' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=2&teste=additional",
|
92
|
+
'next' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional",
|
93
|
+
'last' => "#{WILL_PAGINATE_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=2&teste=additional" }
|
94
|
+
get :render_pagination_using_will_paginate, page: { number: 1, size: 2 }, teste: 'additional'
|
95
|
+
response = JSON.parse(@response.body)
|
96
|
+
assert_equal expected_links, response['links']
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_render_only_prev_and_first_pagination_links_with_additional_params
|
100
|
+
expected_links = { 'self' => "#{KAMINARI_URI}?page%5Bnumber%5D=3&page%5Bsize%5D=1&teste=additional",
|
101
|
+
'first' => "#{KAMINARI_URI}?page%5Bnumber%5D=1&page%5Bsize%5D=1&teste=additional",
|
102
|
+
'prev' => "#{KAMINARI_URI}?page%5Bnumber%5D=2&page%5Bsize%5D=1&teste=additional" }
|
103
|
+
get :render_pagination_using_kaminari, page: { number: 3, size: 1 }, teste: 'additional'
|
104
|
+
response = JSON.parse(@response.body)
|
105
|
+
assert_equal expected_links, response['links']
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_array_without_pagination_links
|
109
|
+
get :render_array_without_pagination_links, page: { number: 2, size: 1 }
|
110
|
+
response = JSON.parse(@response.body)
|
111
|
+
refute response.key? 'links'
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
@@ -4,8 +4,10 @@ require 'pathname'
|
|
4
4
|
class DefaultScopeNameTest < ActionController::TestCase
|
5
5
|
class UserSerializer < ActiveModel::Serializer
|
6
6
|
attributes :admin?
|
7
|
-
|
8
|
-
|
7
|
+
ActiveModelSerializers.silence_warnings do
|
8
|
+
def admin?
|
9
|
+
current_user.admin
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
|
@@ -23,7 +25,7 @@ class DefaultScopeNameTest < ActionController::TestCase
|
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
26
|
-
|
28
|
+
tests UserTestController
|
27
29
|
|
28
30
|
def test_default_scope_name
|
29
31
|
get :render_new_user
|
@@ -34,8 +36,10 @@ end
|
|
34
36
|
class SerializationScopeNameTest < ActionController::TestCase
|
35
37
|
class AdminUserSerializer < ActiveModel::Serializer
|
36
38
|
attributes :admin?
|
37
|
-
|
38
|
-
|
39
|
+
ActiveModelSerializers.silence_warnings do
|
40
|
+
def admin?
|
41
|
+
current_admin.admin
|
42
|
+
end
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
@@ -60,4 +64,4 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|
60
64
|
get :render_new_user
|
61
65
|
assert_equal '{"data":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
|
62
66
|
end
|
63
|
-
end
|
67
|
+
end
|
@@ -3,73 +3,51 @@ require 'test_helper'
|
|
3
3
|
module ActionController
|
4
4
|
module Serialization
|
5
5
|
class ImplicitSerializerTest < ActionController::TestCase
|
6
|
+
include ActiveSupport::Testing::Stream
|
6
7
|
class ImplicitSerializationTestController < ActionController::Base
|
7
8
|
def render_using_implicit_serializer
|
8
|
-
@profile = Profile.new(
|
9
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
9
10
|
render json: @profile
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
15
|
-
render json: @profile, root: "custom_root"
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def render_using_custom_root_and_meta
|
20
|
-
with_adapter ActiveModel::Serializer::Adapter::Json do
|
21
|
-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
22
|
-
render json: @profile, root: "custom_root", meta: { total: 10 }
|
23
|
-
end
|
13
|
+
def render_using_default_adapter_root
|
14
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
15
|
+
render json: @profile
|
24
16
|
end
|
25
17
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
30
|
-
render json: @profile
|
31
|
-
end
|
18
|
+
def render_array_using_custom_root
|
19
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
20
|
+
render json: [@profile], root: 'custom_root'
|
32
21
|
end
|
33
22
|
|
34
|
-
def
|
35
|
-
|
36
|
-
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
37
|
-
render json: @profile, root: "profile", adapter: :json_api
|
23
|
+
def render_array_that_is_empty_using_custom_root
|
24
|
+
render json: [], root: 'custom_root'
|
38
25
|
end
|
39
26
|
|
40
|
-
def
|
41
|
-
|
42
|
-
|
43
|
-
Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
|
44
|
-
Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })
|
45
|
-
]
|
46
|
-
render json: array, root: "custom_root", meta: { total: 10 }
|
47
|
-
end
|
27
|
+
def render_object_using_custom_root
|
28
|
+
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
29
|
+
render json: @profile, root: 'custom_root'
|
48
30
|
end
|
49
31
|
|
50
32
|
def render_array_using_implicit_serializer
|
51
33
|
array = [
|
52
|
-
Profile.new(
|
53
|
-
Profile.new(
|
34
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1'),
|
35
|
+
Profile.new(name: 'Name 2', description: 'Description 2', comments: 'Comments 2')
|
54
36
|
]
|
55
37
|
render json: array
|
56
38
|
end
|
57
39
|
|
58
40
|
def render_array_using_implicit_serializer_and_meta
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
]
|
64
|
-
|
65
|
-
render json: @profiles, meta: { total: 10 }
|
66
|
-
end
|
41
|
+
@profiles = [
|
42
|
+
Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
|
43
|
+
]
|
44
|
+
render json: @profiles, meta: { total: 10 }
|
67
45
|
end
|
68
46
|
|
69
47
|
def render_object_with_cache_enabled
|
70
|
-
@comment = Comment.new(
|
48
|
+
@comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
71
49
|
@author = Author.new(id: 1, name: 'Joao Moura.')
|
72
|
-
@post = Post.new(
|
50
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [@comment], author: @author)
|
73
51
|
|
74
52
|
generate_cached_serializer(@post)
|
75
53
|
|
@@ -77,17 +55,25 @@ module ActionController
|
|
77
55
|
render json: @post
|
78
56
|
end
|
79
57
|
|
58
|
+
def render_json_object_without_serializer
|
59
|
+
render json: { error: 'Result is Invalid' }
|
60
|
+
end
|
61
|
+
|
62
|
+
def render_json_array_object_without_serializer
|
63
|
+
render json: [{ error: 'Result is Invalid' }]
|
64
|
+
end
|
65
|
+
|
80
66
|
def update_and_render_object_with_cache_enabled
|
81
|
-
@post.updated_at =
|
67
|
+
@post.updated_at = Time.now
|
82
68
|
|
83
69
|
generate_cached_serializer(@post)
|
84
70
|
render json: @post
|
85
71
|
end
|
86
72
|
|
87
73
|
def render_object_expired_with_cache_enabled
|
88
|
-
comment = Comment.new(
|
74
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
89
75
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
90
|
-
post = Post.new(
|
76
|
+
post = Post.new(id: 1, title: 'New Post', body: 'Body', comments: [comment], author: author)
|
91
77
|
|
92
78
|
generate_cached_serializer(post)
|
93
79
|
|
@@ -97,16 +83,16 @@ module ActionController
|
|
97
83
|
end
|
98
84
|
|
99
85
|
def render_changed_object_with_cache_enabled
|
100
|
-
comment = Comment.new(
|
86
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
101
87
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
102
|
-
post = Post.new(
|
88
|
+
post = Post.new(id: 1, title: 'ZOMG a New Post', body: 'Body', comments: [comment], author: author)
|
103
89
|
|
104
90
|
render json: post
|
105
91
|
end
|
106
92
|
|
107
93
|
def render_fragment_changed_object_with_only_cache_enabled
|
108
94
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
109
|
-
role = Role.new(
|
95
|
+
role = Role.new(id: 42, name: 'ZOMG A ROLE', description: 'DESCRIPTION HERE', author: author)
|
110
96
|
|
111
97
|
generate_cached_serializer(role)
|
112
98
|
role.name = 'lol'
|
@@ -117,7 +103,7 @@ module ActionController
|
|
117
103
|
|
118
104
|
def render_fragment_changed_object_with_except_cache_enabled
|
119
105
|
author = Author.new(id: 1, name: 'Joao Moura.')
|
120
|
-
bio = Bio.new(
|
106
|
+
bio = Bio.new(id: 42, content: 'ZOMG A ROLE', rating: 5, author: author)
|
121
107
|
|
122
108
|
generate_cached_serializer(bio)
|
123
109
|
bio.content = 'lol'
|
@@ -127,25 +113,21 @@ module ActionController
|
|
127
113
|
end
|
128
114
|
|
129
115
|
def render_fragment_changed_object_with_relationship
|
130
|
-
comment = Comment.new(
|
131
|
-
|
132
|
-
|
133
|
-
post2 = Post.new({ id: 1, title: 'New Post2', body: 'Body2', comments: [comment], author: author })
|
134
|
-
like = Like.new({ id: 1, post: post, time: 3.days.ago })
|
116
|
+
comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
117
|
+
comment2 = Comment.new(id: 1, body: 'ZOMG AN UPDATED-BUT-NOT-CACHE-EXPIRED COMMENT')
|
118
|
+
like = Like.new(id: 1, likeable: comment, time: 3.days.ago)
|
135
119
|
|
136
120
|
generate_cached_serializer(like)
|
137
|
-
like.
|
138
|
-
like.time =
|
121
|
+
like.likable = comment2
|
122
|
+
like.time = Time.zone.now.to_s
|
139
123
|
|
140
124
|
render json: like
|
141
125
|
end
|
142
126
|
|
143
127
|
private
|
128
|
+
|
144
129
|
def generate_cached_serializer(obj)
|
145
|
-
|
146
|
-
serializer = serializer_class.new(obj)
|
147
|
-
adapter = ActiveModel::Serializer.adapter.new(serializer)
|
148
|
-
adapter.to_json
|
130
|
+
ActiveModel::SerializableResource.new(obj).to_json
|
149
131
|
end
|
150
132
|
|
151
133
|
def with_adapter(adapter)
|
@@ -165,8 +147,8 @@ module ActionController
|
|
165
147
|
get :render_using_implicit_serializer
|
166
148
|
|
167
149
|
expected = {
|
168
|
-
name:
|
169
|
-
description:
|
150
|
+
name: 'Name 1',
|
151
|
+
description: 'Description 1'
|
170
152
|
}
|
171
153
|
|
172
154
|
assert_equal 'application/json', @response.content_type
|
@@ -174,15 +156,16 @@ module ActionController
|
|
174
156
|
end
|
175
157
|
|
176
158
|
def test_render_using_default_root
|
177
|
-
|
178
|
-
|
159
|
+
with_adapter :json_api do
|
160
|
+
get :render_using_default_adapter_root
|
161
|
+
end
|
179
162
|
expected = {
|
180
163
|
data: {
|
181
164
|
id: assigns(:profile).id.to_s,
|
182
|
-
type:
|
165
|
+
type: 'profiles',
|
183
166
|
attributes: {
|
184
|
-
name:
|
185
|
-
description:
|
167
|
+
name: 'Name 1',
|
168
|
+
description: 'Description 1'
|
186
169
|
}
|
187
170
|
}
|
188
171
|
}
|
@@ -191,24 +174,51 @@ module ActionController
|
|
191
174
|
assert_equal expected.to_json, @response.body
|
192
175
|
end
|
193
176
|
|
194
|
-
def
|
195
|
-
|
177
|
+
def test_render_array_using_custom_root
|
178
|
+
with_adapter :json do
|
179
|
+
get :render_array_using_custom_root
|
180
|
+
end
|
181
|
+
expected = { custom_roots: [{ name: 'Name 1', description: 'Description 1' }] }
|
182
|
+
assert_equal 'application/json', @response.content_type
|
183
|
+
assert_equal expected.to_json, @response.body
|
184
|
+
end
|
196
185
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
186
|
+
def test_render_array_that_is_empty_using_custom_root
|
187
|
+
with_adapter :json do
|
188
|
+
get :render_array_that_is_empty_using_custom_root
|
189
|
+
end
|
190
|
+
|
191
|
+
expected = { custom_roots: [] }
|
192
|
+
assert_equal 'application/json', @response.content_type
|
193
|
+
assert_equal expected.to_json, @response.body
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_render_object_using_custom_root
|
197
|
+
with_adapter :json do
|
198
|
+
get :render_object_using_custom_root
|
199
|
+
end
|
207
200
|
|
201
|
+
expected = { custom_root: { name: 'Name 1', description: 'Description 1' } }
|
208
202
|
assert_equal 'application/json', @response.content_type
|
209
203
|
assert_equal expected.to_json, @response.body
|
210
204
|
end
|
211
205
|
|
206
|
+
def test_render_json_object_without_serializer
|
207
|
+
get :render_json_object_without_serializer
|
208
|
+
|
209
|
+
assert_equal 'application/json', @response.content_type
|
210
|
+
expected_body = { error: 'Result is Invalid' }
|
211
|
+
assert_equal expected_body.to_json, @response.body
|
212
|
+
end
|
213
|
+
|
214
|
+
def test_render_json_array_object_without_serializer
|
215
|
+
get :render_json_array_object_without_serializer
|
216
|
+
|
217
|
+
assert_equal 'application/json', @response.content_type
|
218
|
+
expected_body = [{ error: 'Result is Invalid' }]
|
219
|
+
assert_equal expected_body.to_json, @response.body
|
220
|
+
end
|
221
|
+
|
212
222
|
def test_render_array_using_implicit_serializer
|
213
223
|
get :render_array_using_implicit_serializer
|
214
224
|
assert_equal 'application/json', @response.content_type
|
@@ -216,11 +226,11 @@ module ActionController
|
|
216
226
|
expected = [
|
217
227
|
{
|
218
228
|
name: 'Name 1',
|
219
|
-
description: 'Description 1'
|
229
|
+
description: 'Description 1'
|
220
230
|
},
|
221
231
|
{
|
222
232
|
name: 'Name 2',
|
223
|
-
description: 'Description 2'
|
233
|
+
description: 'Description 2'
|
224
234
|
}
|
225
235
|
]
|
226
236
|
|
@@ -228,16 +238,17 @@ module ActionController
|
|
228
238
|
end
|
229
239
|
|
230
240
|
def test_render_array_using_implicit_serializer_and_meta
|
231
|
-
|
232
|
-
|
241
|
+
with_adapter :json_api do
|
242
|
+
get :render_array_using_implicit_serializer_and_meta
|
243
|
+
end
|
233
244
|
expected = {
|
234
245
|
data: [
|
235
246
|
{
|
236
247
|
id: assigns(:profiles).first.id.to_s,
|
237
|
-
type:
|
248
|
+
type: 'profiles',
|
238
249
|
attributes: {
|
239
|
-
name:
|
240
|
-
description:
|
250
|
+
name: 'Name 1',
|
251
|
+
description: 'Description 1'
|
241
252
|
}
|
242
253
|
}
|
243
254
|
],
|
@@ -251,9 +262,6 @@ module ActionController
|
|
251
262
|
end
|
252
263
|
|
253
264
|
def test_render_with_cache_enable
|
254
|
-
ActionController::Base.cache_store.clear
|
255
|
-
get :render_object_with_cache_enabled
|
256
|
-
|
257
265
|
expected = {
|
258
266
|
id: 1,
|
259
267
|
title: 'New Post',
|
@@ -273,11 +281,16 @@ module ActionController
|
|
273
281
|
}
|
274
282
|
}
|
275
283
|
|
276
|
-
|
277
|
-
|
284
|
+
ActionController::Base.cache_store.clear
|
285
|
+
Timecop.freeze(Time.zone.now) do
|
286
|
+
get :render_object_with_cache_enabled
|
278
287
|
|
279
|
-
|
280
|
-
|
288
|
+
assert_equal 'application/json', @response.content_type
|
289
|
+
assert_equal expected.to_json, @response.body
|
290
|
+
|
291
|
+
get :render_changed_object_with_cache_enabled
|
292
|
+
assert_equal expected.to_json, @response.body
|
293
|
+
end
|
281
294
|
|
282
295
|
ActionController::Base.cache_store.clear
|
283
296
|
get :render_changed_object_with_cache_enabled
|
@@ -317,8 +330,8 @@ module ActionController
|
|
317
330
|
response = JSON.parse(@response.body)
|
318
331
|
|
319
332
|
assert_equal 'application/json', @response.content_type
|
320
|
-
assert_equal 'ZOMG A ROLE', response[
|
321
|
-
assert_equal 'HUEHUEBRBR', response[
|
333
|
+
assert_equal 'ZOMG A ROLE', response['name']
|
334
|
+
assert_equal 'HUEHUEBRBR', response['description']
|
322
335
|
end
|
323
336
|
|
324
337
|
def test_render_with_fragment_except_cache_enable
|
@@ -327,27 +340,29 @@ module ActionController
|
|
327
340
|
response = JSON.parse(@response.body)
|
328
341
|
|
329
342
|
assert_equal 'application/json', @response.content_type
|
330
|
-
assert_equal 5, response[
|
331
|
-
assert_equal 'lol', response[
|
343
|
+
assert_equal 5, response['rating']
|
344
|
+
assert_equal 'lol', response['content']
|
332
345
|
end
|
333
346
|
|
334
347
|
def test_render_fragment_changed_object_with_relationship
|
335
348
|
ActionController::Base.cache_store.clear
|
336
|
-
get :render_fragment_changed_object_with_relationship
|
337
|
-
response = JSON.parse(@response.body)
|
338
349
|
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
350
|
+
Timecop.freeze(Time.zone.now) do
|
351
|
+
get :render_fragment_changed_object_with_relationship
|
352
|
+
response = JSON.parse(@response.body)
|
353
|
+
|
354
|
+
expected_return = {
|
355
|
+
'id' => 1,
|
356
|
+
'time' => Time.zone.now.to_s,
|
357
|
+
'likeable' => {
|
358
|
+
'id' => 1,
|
359
|
+
'body' => 'ZOMG A COMMENT'
|
360
|
+
}
|
346
361
|
}
|
347
|
-
}
|
348
362
|
|
349
|
-
|
350
|
-
|
363
|
+
assert_equal 'application/json', @response.content_type
|
364
|
+
assert_equal expected_return, response
|
365
|
+
end
|
351
366
|
end
|
352
367
|
|
353
368
|
def test_cache_expiration_on_update
|
@@ -364,8 +379,8 @@ module ActionController
|
|
364
379
|
body: 'ZOMG A COMMENT' }
|
365
380
|
],
|
366
381
|
blog: {
|
367
|
-
id:999,
|
368
|
-
name:
|
382
|
+
id: 999,
|
383
|
+
name: 'Custom blog'
|
369
384
|
},
|
370
385
|
author: {
|
371
386
|
id: 1,
|
@@ -378,6 +393,28 @@ module ActionController
|
|
378
393
|
assert_equal 'application/json', @response.content_type
|
379
394
|
assert_equal expected.to_json, @response.body
|
380
395
|
end
|
396
|
+
|
397
|
+
def test_warn_overridding_use_adapter_as_falsy_on_controller_instance
|
398
|
+
controller = Class.new(ImplicitSerializationTestController) {
|
399
|
+
def use_adapter?
|
400
|
+
false
|
401
|
+
end
|
402
|
+
}.new
|
403
|
+
assert_match(/adapter: false/, (capture(:stderr) {
|
404
|
+
controller.get_serializer(Profile.new)
|
405
|
+
}))
|
406
|
+
end
|
407
|
+
|
408
|
+
def test_dont_warn_overridding_use_adapter_as_truthy_on_controller_instance
|
409
|
+
controller = Class.new(ImplicitSerializationTestController) {
|
410
|
+
def use_adapter?
|
411
|
+
true
|
412
|
+
end
|
413
|
+
}.new
|
414
|
+
assert_equal '', (capture(:stderr) {
|
415
|
+
controller.get_serializer(Profile.new)
|
416
|
+
})
|
417
|
+
end
|
381
418
|
end
|
382
419
|
end
|
383
420
|
end
|