active_model_serializers 0.8.3 → 0.10.0.rc2
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 +3 -0
- data/.travis.yml +18 -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 +169 -495
- data/Rakefile +6 -12
- data/active_model_serializers.gemspec +21 -19
- data/lib/action_controller/serialization.rb +36 -27
- data/lib/active_model/serializer/adapter/flatten_json.rb +12 -0
- 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 +50 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +23 -0
- data/lib/active_model/serializer/adapter/json_api.rb +156 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +96 -0
- data/lib/active_model/serializer/array_serializer.rb +35 -0
- data/lib/active_model/serializer/configuration.rb +13 -0
- data/lib/active_model/serializer/fieldset.rb +40 -0
- data/lib/active_model/serializer/railtie.rb +8 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +177 -440
- data/lib/active_model_serializers.rb +10 -86
- data/lib/generators/serializer/USAGE +0 -3
- data/lib/generators/serializer/resource_override.rb +12 -0
- 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 +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json_api_linked_test.rb +179 -0
- data/test/action_controller/rescue_from_test.rb +32 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +7 -11
- data/test/action_controller/serialization_test.rb +383 -0
- data/test/adapter/fragment_cache_test.rb +27 -0
- data/test/adapter/json/belongs_to_test.rb +48 -0
- data/test/adapter/json/collection_test.rb +73 -0
- data/test/adapter/json/has_many_test.rb +36 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +96 -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 +110 -0
- data/test/adapter/json_api/has_one_test.rb +61 -0
- data/test/adapter/json_api/linked_test.rb +283 -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 +31 -63
- data/test/fixtures/poro.rb +230 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/{generators_test.rb → generators/serializer_generator_test.rb} +2 -36
- data/test/serializers/adapter_for_test.rb +50 -0
- data/test/serializers/associations_test.rb +127 -0
- data/test/serializers/attribute_test.rb +38 -0
- data/test/serializers/attributes_test.rb +63 -0
- data/test/serializers/cache_test.rb +138 -0
- data/test/serializers/configuration_test.rb +15 -0
- data/test/serializers/fieldset_test.rb +26 -0
- data/test/serializers/meta_test.rb +107 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +65 -0
- data/test/serializers/urls_test.rb +26 -0
- data/test/test_helper.rb +28 -16
- metadata +109 -43
- 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,8 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
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'
|
5
|
+
require 'active_model/serializer/railtie'
|
83
6
|
|
84
7
|
begin
|
85
8
|
require 'action_controller'
|
@@ -87,9 +10,10 @@ begin
|
|
87
10
|
|
88
11
|
ActiveSupport.on_load(:action_controller) do
|
89
12
|
include ::ActionController::Serialization
|
13
|
+
ActionDispatch::Reloader.to_prepare do
|
14
|
+
ActiveModel::Serializer.serializers_cache.clear
|
15
|
+
end
|
90
16
|
end
|
91
|
-
rescue LoadError
|
92
|
-
# rails
|
17
|
+
rescue LoadError
|
18
|
+
# rails not installed, continuing
|
93
19
|
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,53 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class AdapterSelectorTest < ActionController::TestCase
|
6
|
+
class AdapterSelectorTestController < 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 AdapterSelectorTestController
|
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
|
+
id: assigns(:profile).id.to_s,
|
36
|
+
type: "profiles",
|
37
|
+
attributes: {
|
38
|
+
name: "Name 1",
|
39
|
+
description: "Description 1",
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
assert_equal expected.to_json, response.body
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_render_skipping_adapter
|
48
|
+
get :render_skipping_adapter
|
49
|
+
assert_equal '{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}', response.body
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class ExplicitSerializerTest < ActionController::TestCase
|
6
|
+
class ExplicitSerializerTestController < 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
|
+
|
59
|
+
def render_using_explicit_each_serializer
|
60
|
+
location = Location.new(id: 42, lat: '-23.550520', lng: '-46.633309')
|
61
|
+
place = Place.new(id: 1337, name: 'Amazing Place', locations: [location])
|
62
|
+
|
63
|
+
render json: place, each_serializer: PlaceSerializer
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
tests ExplicitSerializerTestController
|
68
|
+
|
69
|
+
def test_render_using_explicit_serializer
|
70
|
+
get :render_using_explicit_serializer
|
71
|
+
|
72
|
+
assert_equal 'application/json', @response.content_type
|
73
|
+
assert_equal '{"name":"Name 1"}', @response.body
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_render_array_using_explicit_serializer
|
77
|
+
get :render_array_using_explicit_serializer
|
78
|
+
assert_equal 'application/json', @response.content_type
|
79
|
+
|
80
|
+
expected = [
|
81
|
+
{ 'name' => 'Name 1' },
|
82
|
+
{ 'name' => 'Name 2' }
|
83
|
+
]
|
84
|
+
|
85
|
+
assert_equal expected.to_json, @response.body
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_render_array_using_implicit_serializer
|
89
|
+
get :render_array_using_implicit_serializer
|
90
|
+
assert_equal 'application/json', @response.content_type
|
91
|
+
|
92
|
+
expected = [
|
93
|
+
{ 'name' => 'Name 1' },
|
94
|
+
{ 'name' => 'Name 2' }
|
95
|
+
]
|
96
|
+
assert_equal expected.to_json, @response.body
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_render_array_using_explicit_serializer_and_custom_serializers
|
100
|
+
get :render_array_using_explicit_serializer_and_custom_serializers
|
101
|
+
|
102
|
+
expected = [
|
103
|
+
{ "title" => "New Post",
|
104
|
+
"body" => "Body",
|
105
|
+
"id" => assigns(:post).id,
|
106
|
+
"comments" => [{"id" => 1}, {"id" => 2}],
|
107
|
+
"author" => { "id" => assigns(:author).id }
|
108
|
+
}
|
109
|
+
]
|
110
|
+
|
111
|
+
assert_equal expected.to_json, @response.body
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_render_using_explicit_each_serializer
|
115
|
+
get :render_using_explicit_each_serializer
|
116
|
+
|
117
|
+
expected = {
|
118
|
+
id: 1337,
|
119
|
+
name: "Amazing Place",
|
120
|
+
locations: [
|
121
|
+
{
|
122
|
+
id: 42,
|
123
|
+
lat: "-23.550520",
|
124
|
+
lng: "-46.633309",
|
125
|
+
place: "Nowhere" # is a virtual attribute on LocationSerializer
|
126
|
+
}
|
127
|
+
]
|
128
|
+
}
|
129
|
+
|
130
|
+
assert_equal expected.to_json, response.body
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class JsonApiLinkedTest < ActionController::TestCase
|
6
|
+
class JsonApiLinkedTestController < 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 JsonApiLinkedTestController
|
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['attributes']['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
|
+
"attributes" => {
|
105
|
+
"name" => "Steve K."
|
106
|
+
},
|
107
|
+
"relationships" => {
|
108
|
+
"posts" => { "data" => [] },
|
109
|
+
"roles" => { "data" => [{ "type" =>"roles", "id" => "1" }, { "type" =>"roles", "id" => "2" }] },
|
110
|
+
"bio" => { "data" => nil }
|
111
|
+
}
|
112
|
+
}, {
|
113
|
+
"id" => "1",
|
114
|
+
"type" => "roles",
|
115
|
+
"attributes" => {
|
116
|
+
"name" => "admin",
|
117
|
+
"description" => nil,
|
118
|
+
"slug" => "admin-1"
|
119
|
+
},
|
120
|
+
"relationships" => {
|
121
|
+
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
122
|
+
}
|
123
|
+
}, {
|
124
|
+
"id" => "2",
|
125
|
+
"type" => "roles",
|
126
|
+
"attributes" => {
|
127
|
+
"name" => "colab",
|
128
|
+
"description" => nil,
|
129
|
+
"slug" => "colab-2"
|
130
|
+
},
|
131
|
+
"relationships" => {
|
132
|
+
"author" => { "data" => { "type" =>"authors", "id" => "1" } }
|
133
|
+
}
|
134
|
+
}
|
135
|
+
]
|
136
|
+
assert_equal expected_linked, response['included']
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_render_resource_with_nested_include
|
140
|
+
get :render_resource_with_nested_include
|
141
|
+
response = JSON.parse(@response.body)
|
142
|
+
assert response.key? 'included'
|
143
|
+
assert_equal 1, response['included'].size
|
144
|
+
assert_equal 'Anonymous', response['included'].first['attributes']['name']
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_render_collection_without_include
|
148
|
+
get :render_collection_without_include
|
149
|
+
response = JSON.parse(@response.body)
|
150
|
+
refute response.key? 'included'
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_render_collection_with_include
|
154
|
+
get :render_collection_with_include
|
155
|
+
response = JSON.parse(@response.body)
|
156
|
+
assert response.key? 'included'
|
157
|
+
end
|
158
|
+
|
159
|
+
def test_render_resource_with_nested_attributes_even_when_missing_associations
|
160
|
+
get :render_resource_with_missing_nested_has_many_include
|
161
|
+
response = JSON.parse(@response.body)
|
162
|
+
assert response.key? 'included'
|
163
|
+
refute has_type?(response['included'], 'roles')
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_render_collection_with_missing_nested_has_many_include
|
167
|
+
get :render_collection_with_missing_nested_has_many_include
|
168
|
+
response = JSON.parse(@response.body)
|
169
|
+
assert response.key? 'included'
|
170
|
+
assert has_type?(response['included'], 'roles')
|
171
|
+
end
|
172
|
+
|
173
|
+
def has_type?(collection, value)
|
174
|
+
collection.detect { |i| i['type'] == value}
|
175
|
+
end
|
176
|
+
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Serialization
|
5
|
+
class RescueFromTest < ActionController::TestCase
|
6
|
+
class RescueFromTestController < ActionController::Base
|
7
|
+
rescue_from Exception, with: :handle_error
|
8
|
+
|
9
|
+
def render_using_raise_error_serializer
|
10
|
+
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
|
11
|
+
render json: [@profile], serializer: RaiseErrorSerializer
|
12
|
+
end
|
13
|
+
|
14
|
+
def handle_error(exception)
|
15
|
+
render json: { errors: ['Internal Server Error'] }, status: :internal_server_error
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
tests RescueFromTestController
|
20
|
+
|
21
|
+
def test_rescue_from
|
22
|
+
get :render_using_raise_error_serializer
|
23
|
+
|
24
|
+
expected = {
|
25
|
+
errors: ['Internal Server Error']
|
26
|
+
}.to_json
|
27
|
+
|
28
|
+
assert_equal expected, @response.body
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
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":{"id":"1","type":"users","attributes":{"admin?":false}}}', @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":{"id":"1","type":"users","attributes":{"admin?":true}}}', @response.body
|
66
62
|
end
|
67
|
-
end
|
63
|
+
end
|