active_model_serializers 0.8.3 → 0.10.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.travis.yml +19 -20
- data/CHANGELOG.md +8 -67
- data/CONTRIBUTING.md +31 -0
- data/Gemfile +14 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +166 -495
- data/Rakefile +6 -12
- data/active_model_serializers.gemspec +21 -19
- data/lib/action_controller/serialization.rb +28 -27
- data/lib/active_model/serializer/adapter/fragment_cache.rb +78 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +15 -0
- data/lib/active_model/serializer/adapter/json.rb +52 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +22 -0
- data/lib/active_model/serializer/adapter/json_api.rb +152 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +87 -0
- data/lib/active_model/serializer/array_serializer.rb +32 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +179 -436
- data/lib/active_model_serializers.rb +9 -86
- data/lib/generators/serializer/USAGE +0 -3
- data/lib/generators/serializer/serializer_generator.rb +1 -6
- data/lib/generators/serializer/templates/serializer.rb +2 -13
- data/test/action_controller/adapter_selector_test.rb +51 -0
- data/test/action_controller/explicit_serializer_test.rb +110 -0
- data/test/action_controller/json_api_linked_test.rb +173 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +7 -11
- data/test/action_controller/serialization_test.rb +365 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +41 -0
- data/test/adapter/json/collection_test.rb +59 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +147 -0
- data/test/adapter/json_api/collection_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 +106 -0
- data/test/adapter/json_api/has_one_test.rb +59 -0
- data/test/adapter/json_api/linked_test.rb +257 -0
- data/test/adapter/json_test.rb +34 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +43 -0
- data/test/array_serializer_test.rb +21 -67
- data/test/fixtures/poro.rb +206 -0
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +106 -0
- data/test/serializers/attribute_test.rb +23 -0
- data/test/serializers/attributes_test.rb +28 -0
- data/test/serializers/cache_test.rb +128 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/{generators_test.rb → serializers/generators_test.rb} +1 -27
- data/test/serializers/meta_test.rb +78 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +56 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +22 -13
- metadata +101 -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_model/serializer/associations.rb +0 -233
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- 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
@@ -1,85 +1,7 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require "active_model/array_serializer"
|
6
|
-
require "active_model/serializer"
|
7
|
-
require "active_model/serializer/associations"
|
8
|
-
require "set"
|
9
|
-
|
10
|
-
if defined?(Rails)
|
11
|
-
module ActiveModel
|
12
|
-
class Railtie < Rails::Railtie
|
13
|
-
generators do |app|
|
14
|
-
app ||= Rails.application # Rails 3.0.x does not yield `app`
|
15
|
-
|
16
|
-
Rails::Generators.configure!(app.config.generators)
|
17
|
-
Rails::Generators.hidden_namespaces.uniq!
|
18
|
-
require_relative "generators/resource_override"
|
19
|
-
end
|
20
|
-
|
21
|
-
initializer "include_routes.active_model_serializer" do |app|
|
22
|
-
ActiveSupport.on_load(:active_model_serializers) do
|
23
|
-
include app.routes.url_helpers
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
initializer "caching.active_model_serializer" do |app|
|
28
|
-
ActiveModel::Serializer.perform_caching = app.config.action_controller.perform_caching
|
29
|
-
ActiveModel::ArraySerializer.perform_caching = app.config.action_controller.perform_caching
|
30
|
-
|
31
|
-
ActiveModel::Serializer.cache = Rails.cache
|
32
|
-
ActiveModel::ArraySerializer.cache = Rails.cache
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
module ActiveModel::SerializerSupport
|
39
|
-
extend ActiveSupport::Concern
|
40
|
-
|
41
|
-
module ClassMethods #:nodoc:
|
42
|
-
if "".respond_to?(:safe_constantize)
|
43
|
-
def active_model_serializer
|
44
|
-
"#{self.name}Serializer".safe_constantize
|
45
|
-
end
|
46
|
-
else
|
47
|
-
def active_model_serializer
|
48
|
-
begin
|
49
|
-
"#{self.name}Serializer".constantize
|
50
|
-
rescue NameError => e
|
51
|
-
raise unless e.message =~ /uninitialized constant/
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# Returns a model serializer for this object considering its namespace.
|
58
|
-
def active_model_serializer
|
59
|
-
self.class.active_model_serializer
|
60
|
-
end
|
61
|
-
|
62
|
-
alias :read_attribute_for_serialization :send
|
63
|
-
end
|
64
|
-
|
65
|
-
module ActiveModel::ArraySerializerSupport
|
66
|
-
def active_model_serializer
|
67
|
-
ActiveModel::ArraySerializer
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
Array.send(:include, ActiveModel::ArraySerializerSupport)
|
72
|
-
Set.send(:include, ActiveModel::ArraySerializerSupport)
|
73
|
-
|
74
|
-
{
|
75
|
-
:active_record => 'ActiveRecord::Relation',
|
76
|
-
:mongoid => 'Mongoid::Criteria'
|
77
|
-
}.each do |orm, rel_class|
|
78
|
-
ActiveSupport.on_load(orm) do
|
79
|
-
include ActiveModel::SerializerSupport
|
80
|
-
rel_class.constantize.send(:include, ActiveModel::ArraySerializerSupport)
|
81
|
-
end
|
82
|
-
end
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_model/serializer/version'
|
3
|
+
require 'active_model/serializer'
|
4
|
+
require 'active_model/serializer/fieldset'
|
83
5
|
|
84
6
|
begin
|
85
7
|
require 'action_controller'
|
@@ -87,9 +9,10 @@ begin
|
|
87
9
|
|
88
10
|
ActiveSupport.on_load(:action_controller) do
|
89
11
|
include ::ActionController::Serialization
|
12
|
+
ActionDispatch::Reloader.to_prepare do
|
13
|
+
ActiveModel::Serializer.serializers_cache.clear
|
14
|
+
end
|
90
15
|
end
|
91
|
-
rescue LoadError
|
92
|
-
# rails
|
16
|
+
rescue LoadError
|
17
|
+
# rails not installed, continuing
|
93
18
|
end
|
94
|
-
|
95
|
-
ActiveSupport.run_load_hooks(:active_model_serializers, ActiveModel::Serializer)
|
@@ -13,9 +13,6 @@ module Rails
|
|
13
13
|
end
|
14
14
|
|
15
15
|
private
|
16
|
-
def generate_id_method
|
17
|
-
RUBY_VERSION =~ /1\.8/
|
18
|
-
end
|
19
16
|
|
20
17
|
def attributes_names
|
21
18
|
[:id] + attributes.select { |attr| !attr.reference? }.map { |a| a.name.to_sym }
|
@@ -28,9 +25,6 @@ module Rails
|
|
28
25
|
def parent_class_name
|
29
26
|
if options[:parent]
|
30
27
|
options[:parent]
|
31
|
-
# Only works on 3.2
|
32
|
-
# elsif (n = Rails::Generators.namespace) && n.const_defined?(:ApplicationSerializer)
|
33
|
-
# "ApplicationSerializer"
|
34
28
|
elsif defined?(::ApplicationSerializer)
|
35
29
|
"ApplicationSerializer"
|
36
30
|
else
|
@@ -40,3 +34,4 @@ module Rails
|
|
40
34
|
end
|
41
35
|
end
|
42
36
|
end
|
37
|
+
|
@@ -1,19 +1,8 @@
|
|
1
1
|
<% module_namespacing do -%>
|
2
2
|
class <%= class_name %>Serializer < <%= parent_class_name %>
|
3
3
|
attributes <%= attributes_names.map(&:inspect).join(", ") %>
|
4
|
+
end
|
4
5
|
<% association_names.each do |attribute| -%>
|
5
|
-
|
6
|
-
<% end -%>
|
7
|
-
<% if generate_id_method %>
|
8
|
-
|
9
|
-
# due to the difference between 1.8 and 1.9 with respect to #id and
|
10
|
-
# #object_id, we recommend that if you wish to serialize id columns, you
|
11
|
-
# do this. Feel free to remove this if you don't feel that it's appropriate.
|
12
|
-
#
|
13
|
-
# For more: https://github.com/rails-api/active_model_serializers/issues/127
|
14
|
-
def id
|
15
|
-
object.read_attribute_for_serialization(:id)
|
16
|
-
end
|
6
|
+
attribute :<%= attribute %>
|
17
7
|
<% end -%>
|
18
|
-
end
|
19
8
|
<% end -%>
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class AdapterSelectorTest < ActionController::TestCase
|
6
|
+
class MyController < ActionController::Base
|
7
|
+
def render_using_default_adapter
|
8
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
9
|
+
render json: @profile
|
10
|
+
end
|
11
|
+
|
12
|
+
def render_using_adapter_override
|
13
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
14
|
+
render json: @profile, adapter: :json_api
|
15
|
+
end
|
16
|
+
|
17
|
+
def render_skipping_adapter
|
18
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
19
|
+
render json: @profile, adapter: false
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
tests MyController
|
24
|
+
|
25
|
+
def test_render_using_default_adapter
|
26
|
+
get :render_using_default_adapter
|
27
|
+
assert_equal '{"name":"Name 1","description":"Description 1"}', response.body
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_render_using_adapter_override
|
31
|
+
get :render_using_adapter_override
|
32
|
+
|
33
|
+
expected = {
|
34
|
+
data: {
|
35
|
+
name: "Name 1",
|
36
|
+
description: "Description 1",
|
37
|
+
id: assigns(:profile).id.to_s,
|
38
|
+
type: "profiles"
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
assert_equal expected.to_json, response.body
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_render_skipping_adapter
|
46
|
+
get :render_skipping_adapter
|
47
|
+
assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class ExplicitSerializerTest < ActionController::TestCase
|
6
|
+
class MyController < ActionController::Base
|
7
|
+
def render_using_explicit_serializer
|
8
|
+
@profile = Profile.new(name: 'Name 1',
|
9
|
+
description: 'Description 1',
|
10
|
+
comments: 'Comments 1')
|
11
|
+
render json: @profile, serializer: ProfilePreviewSerializer
|
12
|
+
end
|
13
|
+
|
14
|
+
def render_array_using_explicit_serializer
|
15
|
+
array = [
|
16
|
+
Profile.new(name: 'Name 1',
|
17
|
+
description: 'Description 1',
|
18
|
+
comments: 'Comments 1'),
|
19
|
+
Profile.new(name: 'Name 2',
|
20
|
+
description: 'Description 2',
|
21
|
+
comments: 'Comments 2')
|
22
|
+
]
|
23
|
+
render json: array,
|
24
|
+
serializer: PaginatedSerializer,
|
25
|
+
each_serializer: ProfilePreviewSerializer
|
26
|
+
end
|
27
|
+
|
28
|
+
def render_array_using_implicit_serializer
|
29
|
+
array = [
|
30
|
+
Profile.new(name: 'Name 1',
|
31
|
+
description: 'Description 1',
|
32
|
+
comments: 'Comments 1'),
|
33
|
+
Profile.new(name: 'Name 2',
|
34
|
+
description: 'Description 2',
|
35
|
+
comments: 'Comments 2')
|
36
|
+
]
|
37
|
+
render json: array,
|
38
|
+
each_serializer: ProfilePreviewSerializer
|
39
|
+
end
|
40
|
+
|
41
|
+
def render_array_using_explicit_serializer_and_custom_serializers
|
42
|
+
@post = Post.new(title: 'New Post', body: 'Body')
|
43
|
+
@author = Author.new(name: 'Jane Blogger')
|
44
|
+
@author.posts = [@post]
|
45
|
+
@post.author = @author
|
46
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
47
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
48
|
+
@post.comments = [@first_comment, @second_comment]
|
49
|
+
@first_comment.post = @post
|
50
|
+
@first_comment.author = nil
|
51
|
+
@second_comment.post = @post
|
52
|
+
@second_comment.author = nil
|
53
|
+
@blog = Blog.new(id: 23, name: 'AMS Blog')
|
54
|
+
@post.blog = @blog
|
55
|
+
|
56
|
+
render json: [@post], each_serializer: PostPreviewSerializer
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
tests MyController
|
61
|
+
|
62
|
+
def test_render_using_explicit_serializer
|
63
|
+
get :render_using_explicit_serializer
|
64
|
+
|
65
|
+
assert_equal 'application/json', @response.content_type
|
66
|
+
assert_equal '{"name":"Name 1"}', @response.body
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_render_array_using_explicit_serializer
|
70
|
+
get :render_array_using_explicit_serializer
|
71
|
+
assert_equal 'application/json', @response.content_type
|
72
|
+
|
73
|
+
expected = {
|
74
|
+
'paginated' => [
|
75
|
+
{ 'name' => 'Name 1' },
|
76
|
+
{ 'name' => 'Name 2' }
|
77
|
+
]
|
78
|
+
}
|
79
|
+
|
80
|
+
assert_equal expected.to_json, @response.body
|
81
|
+
end
|
82
|
+
|
83
|
+
def test_render_array_using_explicit_serializer
|
84
|
+
get :render_array_using_implicit_serializer
|
85
|
+
assert_equal 'application/json', @response.content_type
|
86
|
+
|
87
|
+
expected = [
|
88
|
+
{ 'name' => 'Name 1' },
|
89
|
+
{ 'name' => 'Name 2' }
|
90
|
+
]
|
91
|
+
assert_equal expected.to_json, @response.body
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_render_array_using_explicit_serializer_and_custom_serializers
|
95
|
+
get :render_array_using_explicit_serializer_and_custom_serializers
|
96
|
+
|
97
|
+
expected = [
|
98
|
+
{ "title" => "New Post",
|
99
|
+
"body" => "Body",
|
100
|
+
"id" => assigns(:post).id,
|
101
|
+
"comments" => [{"id" => 1}, {"id" => 2}],
|
102
|
+
"author" => { "id" => assigns(:author).id }
|
103
|
+
}
|
104
|
+
]
|
105
|
+
|
106
|
+
assert_equal expected.to_json, @response.body
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApiLinkedTest < ActionController::TestCase
|
6
|
+
class MyController < ActionController::Base
|
7
|
+
def setup_post
|
8
|
+
ActionController::Base.cache_store.clear
|
9
|
+
@role1 = Role.new(id: 1, name: 'admin')
|
10
|
+
@role2 = Role.new(id: 2, name: 'colab')
|
11
|
+
@author = Author.new(id: 1, name: 'Steve K.')
|
12
|
+
@author.posts = []
|
13
|
+
@author.bio = nil
|
14
|
+
@author.roles = [@role1, @role2]
|
15
|
+
@role1.author = @author
|
16
|
+
@role2.author = @author
|
17
|
+
@author2 = Author.new(id: 2, name: 'Anonymous')
|
18
|
+
@author2.posts = []
|
19
|
+
@author2.bio = nil
|
20
|
+
@author2.roles = []
|
21
|
+
@post = Post.new(id: 1, title: 'New Post', body: 'Body')
|
22
|
+
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
|
23
|
+
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
|
24
|
+
@post.comments = [@first_comment, @second_comment]
|
25
|
+
@post.author = @author
|
26
|
+
@first_comment.post = @post
|
27
|
+
@first_comment.author = @author2
|
28
|
+
@second_comment.post = @post
|
29
|
+
@second_comment.author = nil
|
30
|
+
@post2 = Post.new(id: 2, title: "Another Post", body: "Body")
|
31
|
+
@post2.author = @author
|
32
|
+
@post2.comments = []
|
33
|
+
@blog = Blog.new(id: 1, name: "My Blog!!")
|
34
|
+
@post.blog = @blog
|
35
|
+
@post2.blog = @blog
|
36
|
+
end
|
37
|
+
|
38
|
+
def render_resource_without_include
|
39
|
+
setup_post
|
40
|
+
render json: @post, adapter: :json_api
|
41
|
+
end
|
42
|
+
|
43
|
+
def render_resource_with_include
|
44
|
+
setup_post
|
45
|
+
render json: @post, include: 'author', adapter: :json_api
|
46
|
+
end
|
47
|
+
|
48
|
+
def render_resource_with_nested_include
|
49
|
+
setup_post
|
50
|
+
render json: @post, include: 'comments.author', adapter: :json_api
|
51
|
+
end
|
52
|
+
|
53
|
+
def render_resource_with_nested_has_many_include
|
54
|
+
setup_post
|
55
|
+
render json: @post, include: ['author', 'author.roles'], adapter: :json_api
|
56
|
+
end
|
57
|
+
|
58
|
+
def render_resource_with_missing_nested_has_many_include
|
59
|
+
setup_post
|
60
|
+
@post.author = @author2 # author2 has no roles.
|
61
|
+
render json: @post, include: 'author,author.roles', adapter: :json_api
|
62
|
+
end
|
63
|
+
|
64
|
+
def render_collection_with_missing_nested_has_many_include
|
65
|
+
setup_post
|
66
|
+
@post.author = @author2
|
67
|
+
render json: [@post, @post2], include: 'author,author.roles', adapter: :json_api
|
68
|
+
end
|
69
|
+
|
70
|
+
def render_collection_without_include
|
71
|
+
setup_post
|
72
|
+
render json: [@post], adapter: :json_api
|
73
|
+
end
|
74
|
+
|
75
|
+
def render_collection_with_include
|
76
|
+
setup_post
|
77
|
+
render json: [@post], include: ['author', 'comments'], adapter: :json_api
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
tests MyController
|
82
|
+
|
83
|
+
def test_render_resource_without_include
|
84
|
+
get :render_resource_without_include
|
85
|
+
response = JSON.parse(@response.body)
|
86
|
+
refute response.key? 'included'
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_render_resource_with_include
|
90
|
+
get :render_resource_with_include
|
91
|
+
response = JSON.parse(@response.body)
|
92
|
+
assert response.key? 'included'
|
93
|
+
assert_equal 1, response['included'].size
|
94
|
+
assert_equal 'Steve K.', response['included'].first['name']
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_render_resource_with_nested_has_many_include
|
98
|
+
get :render_resource_with_nested_has_many_include
|
99
|
+
response = JSON.parse(@response.body)
|
100
|
+
expected_linked = [
|
101
|
+
{
|
102
|
+
"id" => "1",
|
103
|
+
"type" => "authors",
|
104
|
+
"name" => "Steve K.",
|
105
|
+
"links" => {
|
106
|
+
"posts" => { "linkage" => [] },
|
107
|
+
"roles" => { "linkage" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
108
|
+
"bio" => { "linkage" => nil }
|
109
|
+
}
|
110
|
+
}, {
|
111
|
+
"id" => "1",
|
112
|
+
"type" => "roles",
|
113
|
+
"name" => "admin",
|
114
|
+
"description" => nil,
|
115
|
+
"slug" => "admin-1",
|
116
|
+
"links" => {
|
117
|
+
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
118
|
+
}
|
119
|
+
}, {
|
120
|
+
"id" => "2",
|
121
|
+
"type" => "roles",
|
122
|
+
"name" => "colab",
|
123
|
+
"description" => nil,
|
124
|
+
"slug" => "colab-2",
|
125
|
+
"links" => {
|
126
|
+
"author" => { "linkage" => { "type" =>"authors", "id" => "1" } }
|
127
|
+
}
|
128
|
+
}
|
129
|
+
]
|
130
|
+
assert_equal expected_linked, response['included']
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_render_resource_with_nested_include
|
134
|
+
get :render_resource_with_nested_include
|
135
|
+
response = JSON.parse(@response.body)
|
136
|
+
assert response.key? 'included'
|
137
|
+
assert_equal 1, response['included'].size
|
138
|
+
assert_equal 'Anonymous', response['included'].first['name']
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_render_collection_without_include
|
142
|
+
get :render_collection_without_include
|
143
|
+
response = JSON.parse(@response.body)
|
144
|
+
refute response.key? 'included'
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_render_collection_with_include
|
148
|
+
get :render_collection_with_include
|
149
|
+
response = JSON.parse(@response.body)
|
150
|
+
assert response.key? 'included'
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
154
|
+
get :render_resource_with_missing_nested_has_many_include
|
155
|
+
response = JSON.parse(@response.body)
|
156
|
+
assert response.key? 'included'
|
157
|
+
refute has_type?(response['included'], 'roles')
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_render_collection_with_missing_nested_has_many_include
|
161
|
+
get :render_collection_with_missing_nested_has_many_include
|
162
|
+
response = JSON.parse(@response.body)
|
163
|
+
assert response.key? 'included'
|
164
|
+
assert has_type?(response['included'], 'roles')
|
165
|
+
end
|
166
|
+
|
167
|
+
def has_type?(collection, value)
|
168
|
+
collection.detect { |i| i['type'] == value}
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb}
RENAMED
@@ -2,8 +2,6 @@ require 'test_helper'
|
|
2
2
|
require 'pathname'
|
3
3
|
|
4
4
|
class DefaultScopeNameTest < ActionController::TestCase
|
5
|
-
TestUser = Struct.new(:name, :admin)
|
6
|
-
|
7
5
|
class UserSerializer < ActiveModel::Serializer
|
8
6
|
attributes :admin?
|
9
7
|
def admin?
|
@@ -17,11 +15,11 @@ class DefaultScopeNameTest < ActionController::TestCase
|
|
17
15
|
before_filter { request.format = :json }
|
18
16
|
|
19
17
|
def current_user
|
20
|
-
|
18
|
+
User.new(id: 1, name: 'Pete', admin: false)
|
21
19
|
end
|
22
20
|
|
23
21
|
def render_new_user
|
24
|
-
render :
|
22
|
+
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: UserSerializer, adapter: :json_api
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
@@ -29,13 +27,11 @@ class DefaultScopeNameTest < ActionController::TestCase
|
|
29
27
|
|
30
28
|
def test_default_scope_name
|
31
29
|
get :render_new_user
|
32
|
-
assert_equal '{"
|
30
|
+
assert_equal '{"data":{"admin?":false,"id":"1","type":"users"}}', @response.body
|
33
31
|
end
|
34
32
|
end
|
35
33
|
|
36
34
|
class SerializationScopeNameTest < ActionController::TestCase
|
37
|
-
TestUser = Struct.new(:name, :admin)
|
38
|
-
|
39
35
|
class AdminUserSerializer < ActiveModel::Serializer
|
40
36
|
attributes :admin?
|
41
37
|
def admin?
|
@@ -50,11 +46,11 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|
50
46
|
before_filter { request.format = :json }
|
51
47
|
|
52
48
|
def current_admin
|
53
|
-
|
49
|
+
User.new(id: 2, name: 'Bob', admin: true)
|
54
50
|
end
|
55
51
|
|
56
52
|
def render_new_user
|
57
|
-
render :
|
53
|
+
render json: User.new(id: 1, name: 'Pete', admin: false), serializer: AdminUserSerializer, adapter: :json_api
|
58
54
|
end
|
59
55
|
end
|
60
56
|
|
@@ -62,6 +58,6 @@ class SerializationScopeNameTest < ActionController::TestCase
|
|
62
58
|
|
63
59
|
def test_override_scope_name_with_controller
|
64
60
|
get :render_new_user
|
65
|
-
assert_equal '{"
|
61
|
+
assert_equal '{"data":{"admin?":true,"id":"1","type":"users"}}', @response.body
|
66
62
|
end
|
67
|
-
end
|
63
|
+
end
|