active_model_serializers 0.9.8 → 0.10.13

Sign up to get free protection for your applications and to get access to all the features.
Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +622 -59
  3. data/MIT-LICENSE +3 -2
  4. data/README.md +194 -846
  5. data/lib/action_controller/serialization.rb +35 -66
  6. data/lib/active_model/serializable_resource.rb +13 -0
  7. data/lib/active_model/serializer/adapter/attributes.rb +17 -0
  8. data/lib/active_model/serializer/adapter/base.rb +20 -0
  9. data/lib/active_model/serializer/adapter/json.rb +17 -0
  10. data/lib/active_model/serializer/adapter/json_api.rb +17 -0
  11. data/lib/active_model/serializer/adapter/null.rb +17 -0
  12. data/lib/active_model/serializer/adapter.rb +26 -0
  13. data/lib/active_model/serializer/array_serializer.rb +14 -0
  14. data/lib/active_model/serializer/association.rb +55 -40
  15. data/lib/active_model/serializer/attribute.rb +27 -0
  16. data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
  17. data/lib/active_model/serializer/collection_serializer.rb +99 -0
  18. data/lib/active_model/serializer/concerns/caching.rb +305 -0
  19. data/lib/active_model/serializer/error_serializer.rb +16 -0
  20. data/lib/active_model/serializer/errors_serializer.rb +34 -0
  21. data/lib/active_model/serializer/field.rb +92 -0
  22. data/lib/active_model/serializer/fieldset.rb +33 -0
  23. data/lib/active_model/serializer/has_many_reflection.rb +12 -0
  24. data/lib/active_model/serializer/has_one_reflection.rb +9 -0
  25. data/lib/active_model/serializer/lazy_association.rb +99 -0
  26. data/lib/active_model/serializer/link.rb +23 -0
  27. data/lib/active_model/serializer/lint.rb +152 -0
  28. data/lib/active_model/serializer/null.rb +19 -0
  29. data/lib/active_model/serializer/reflection.rb +212 -0
  30. data/lib/active_model/serializer/version.rb +3 -1
  31. data/lib/active_model/serializer.rb +363 -254
  32. data/lib/active_model_serializers/adapter/attributes.rb +36 -0
  33. data/lib/active_model_serializers/adapter/base.rb +85 -0
  34. data/lib/active_model_serializers/adapter/json.rb +23 -0
  35. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
  36. data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
  37. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
  38. data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
  39. data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
  40. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +94 -0
  41. data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
  42. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
  43. data/lib/active_model_serializers/adapter/json_api.rb +535 -0
  44. data/lib/active_model_serializers/adapter/null.rb +11 -0
  45. data/lib/active_model_serializers/adapter.rb +100 -0
  46. data/lib/active_model_serializers/callbacks.rb +57 -0
  47. data/lib/active_model_serializers/deprecate.rb +56 -0
  48. data/lib/active_model_serializers/deserialization.rb +17 -0
  49. data/lib/active_model_serializers/json_pointer.rb +16 -0
  50. data/lib/active_model_serializers/logging.rb +124 -0
  51. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  52. data/lib/active_model_serializers/model/caching.rb +1 -0
  53. data/lib/active_model_serializers/model.rb +132 -0
  54. data/lib/active_model_serializers/railtie.rb +52 -0
  55. data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
  56. data/lib/active_model_serializers/serializable_resource.rb +84 -0
  57. data/lib/active_model_serializers/serialization_context.rb +41 -0
  58. data/lib/active_model_serializers/test/schema.rb +140 -0
  59. data/lib/active_model_serializers/test/serializer.rb +127 -0
  60. data/lib/active_model_serializers/test.rb +9 -0
  61. data/lib/active_model_serializers.rb +60 -17
  62. data/lib/generators/rails/USAGE +6 -0
  63. data/lib/{active_model/serializer/generators → generators/rails}/resource_override.rb +3 -4
  64. data/lib/{active_model/serializer/generators/serializer → generators/rails}/serializer_generator.rb +6 -5
  65. data/lib/{active_model/serializer/generators/serializer/templates/serializer.rb → generators/rails/templates/serializer.rb.erb} +0 -0
  66. data/lib/grape/active_model_serializers.rb +18 -0
  67. data/lib/grape/formatters/active_model_serializers.rb +34 -0
  68. data/lib/grape/helpers/active_model_serializers.rb +19 -0
  69. data/lib/tasks/rubocop.rake +55 -0
  70. metadata +272 -155
  71. data/CONTRIBUTING.md +0 -20
  72. data/DESIGN.textile +0 -586
  73. data/lib/action_controller/serialization_test_case.rb +0 -79
  74. data/lib/active_model/array_serializer.rb +0 -68
  75. data/lib/active_model/default_serializer.rb +0 -28
  76. data/lib/active_model/serializable/utils.rb +0 -16
  77. data/lib/active_model/serializable.rb +0 -59
  78. data/lib/active_model/serializer/association/has_many.rb +0 -39
  79. data/lib/active_model/serializer/association/has_one.rb +0 -25
  80. data/lib/active_model/serializer/config.rb +0 -31
  81. data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
  82. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -14
  83. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
  84. data/lib/active_model/serializer/railtie.rb +0 -22
  85. data/lib/active_model/serializer_support.rb +0 -5
  86. data/test/benchmark/app.rb +0 -60
  87. data/test/benchmark/benchmarking_support.rb +0 -67
  88. data/test/benchmark/bm_active_record.rb +0 -41
  89. data/test/benchmark/setup.rb +0 -75
  90. data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
  91. data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
  92. data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
  93. data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
  94. data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
  95. data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
  96. data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
  97. data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
  98. data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
  99. data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
  100. data/test/fixtures/active_record.rb +0 -96
  101. data/test/fixtures/poro.rb +0 -223
  102. data/test/fixtures/template.html.erb +0 -1
  103. data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
  104. data/test/integration/action_controller/serialization_test.rb +0 -287
  105. data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
  106. data/test/integration/active_record/active_record_test.rb +0 -94
  107. data/test/integration/generators/resource_generator_test.rb +0 -26
  108. data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
  109. data/test/integration/generators/serializer_generator_test.rb +0 -41
  110. data/test/test_app.rb +0 -14
  111. data/test/test_helper.rb +0 -24
  112. data/test/tmp/app/assets/javascripts/accounts.js +0 -2
  113. data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
  114. data/test/tmp/app/controllers/accounts_controller.rb +0 -2
  115. data/test/tmp/app/helpers/accounts_helper.rb +0 -2
  116. data/test/tmp/app/serializers/account_serializer.rb +0 -3
  117. data/test/tmp/config/routes.rb +0 -1
  118. data/test/unit/active_model/array_serializer/except_test.rb +0 -18
  119. data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
  120. data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
  121. data/test/unit/active_model/array_serializer/only_test.rb +0 -18
  122. data/test/unit/active_model/array_serializer/options_test.rb +0 -16
  123. data/test/unit/active_model/array_serializer/root_test.rb +0 -102
  124. data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
  125. data/test/unit/active_model/array_serializer/serialization_test.rb +0 -216
  126. data/test/unit/active_model/default_serializer_test.rb +0 -13
  127. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
  128. data/test/unit/active_model/serializer/associations_test.rb +0 -49
  129. data/test/unit/active_model/serializer/attributes_test.rb +0 -57
  130. data/test/unit/active_model/serializer/config_test.rb +0 -91
  131. data/test/unit/active_model/serializer/filter_test.rb +0 -69
  132. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
  133. data/test/unit/active_model/serializer/has_many_test.rb +0 -265
  134. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
  135. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
  136. data/test/unit/active_model/serializer/has_one_test.rb +0 -253
  137. data/test/unit/active_model/serializer/key_format_test.rb +0 -25
  138. data/test/unit/active_model/serializer/meta_test.rb +0 -39
  139. data/test/unit/active_model/serializer/options_test.rb +0 -42
  140. data/test/unit/active_model/serializer/root_test.rb +0 -117
  141. data/test/unit/active_model/serializer/scope_test.rb +0 -49
  142. data/test/unit/active_model/serializer/url_helpers_test.rb +0 -35
  143. data/test/unit/active_model/serilizable_test.rb +0 -50
@@ -1,96 +0,0 @@
1
- require 'active_record'
2
-
3
- ActiveRecord::Base.establish_connection(
4
- :adapter => 'sqlite3',
5
- :database => ':memory:'
6
- )
7
-
8
- ActiveRecord::Schema.define do
9
- create_table :ar_posts, force: true do |t|
10
- t.string :title
11
- t.text :body
12
- t.belongs_to :ar_section, index: true
13
- t.timestamps
14
- end
15
-
16
- create_table :ar_comments, force: true do |t|
17
- t.text :body
18
- t.belongs_to :ar_post, index: true
19
- t.timestamps
20
- end
21
-
22
- create_table :ar_tags, force: true do |t|
23
- t.string :name
24
- end
25
-
26
- create_table :ar_sections, force: true do |t|
27
- t.string :name
28
- end
29
-
30
- create_table :ar_posts_tags, force: true do |t|
31
- t.references :ar_post, :ar_tag, index: true
32
- end
33
-
34
- create_table :ar_comments_tags, force: true do |t|
35
- t.references :ar_comment, :ar_tag, index: true
36
- end
37
- end
38
-
39
- class ARPost < ActiveRecord::Base
40
- has_many :ar_comments, class_name: 'ARComment'
41
- has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_posts_tags
42
- belongs_to :ar_section, class_name: 'ARSection'
43
- end
44
-
45
- class ARComment < ActiveRecord::Base
46
- belongs_to :ar_post, class_name: 'ARPost'
47
- has_and_belongs_to_many :ar_tags, class_name: 'ARTag', join_table: :ar_comments_tags
48
- end
49
-
50
- class ARTag < ActiveRecord::Base
51
- end
52
-
53
- class ARSection < ActiveRecord::Base
54
- end
55
-
56
- class ARPostSerializer < ActiveModel::Serializer
57
- attributes :title, :body
58
-
59
- has_many :ar_comments, :ar_tags
60
- has_one :ar_section
61
- end
62
-
63
- class ARCommentSerializer < ActiveModel::Serializer
64
- attributes :body
65
-
66
- has_many :ar_tags
67
- end
68
-
69
- class ARTagSerializer < ActiveModel::Serializer
70
- attributes :name
71
- end
72
-
73
- class ARSectionSerializer < ActiveModel::Serializer
74
- attributes 'name'
75
- end
76
-
77
- class AREmbeddedSerializer < ActiveModel::Serializer
78
- has_many :ar_tags, :ar_comments
79
- end
80
-
81
- ARPost.create(title: 'New post',
82
- body: 'A body!!!',
83
- ar_section: ARSection.create(name: 'ruby')).tap do |post|
84
-
85
- short_tag = post.ar_tags.create(name: 'short')
86
- whiny_tag = post.ar_tags.create(name: 'whiny')
87
- happy_tag = ARTag.create(name: 'happy')
88
-
89
- post.ar_comments.create(body: 'what a dumb post').tap do |comment|
90
- comment.ar_tags.concat happy_tag, whiny_tag
91
- end
92
-
93
- post.ar_comments.create(body: 'i liked it').tap do |comment|
94
- comment.ar_tags.concat happy_tag, short_tag
95
- end
96
- end
@@ -1,223 +0,0 @@
1
- class Model
2
- def initialize(hash = {})
3
- @attributes = hash
4
- end
5
-
6
- def read_attribute_for_serialization(name)
7
- if name == :id || name == 'id'
8
- object_id
9
- elsif respond_to?(name)
10
- send name
11
- else
12
- @attributes[name]
13
- end
14
- end
15
- end
16
-
17
-
18
- ###
19
- ## Models
20
- ###
21
- class User < Model
22
- def profile
23
- @profile ||= Profile.new(name: 'N1', description: 'D1')
24
- end
25
- end
26
-
27
- class UserInfo < Model
28
- def user
29
- @user ||= User.new(name: 'N1', email: 'E1')
30
- end
31
- end
32
-
33
- class Profile < Model
34
- end
35
-
36
- class Category < Model
37
- def posts
38
- @posts ||= [Post.new(title: 'T1', body: 'B1'),
39
- Post.new(title: 'T2', body: 'B2')]
40
- end
41
- end
42
-
43
- class Post < Model
44
- def comments
45
- @comments ||= [Comment.new(content: 'C1'),
46
- Comment.new(content: 'C2')]
47
- end
48
- end
49
-
50
- class SpecialPost < Post
51
- def special_comment
52
- @special_comment ||= Comment.new(content: 'special')
53
- end
54
- end
55
-
56
- class Type < Model
57
- end
58
-
59
- class SelfReferencingUser < Model
60
- def type
61
- @type ||= Type.new(name: 'N1')
62
- end
63
- def parent
64
- @parent ||= SelfReferencingUserParent.new(name: 'N1')
65
- end
66
- end
67
-
68
- class SelfReferencingUserParent < Model
69
- def type
70
- @type ||= Type.new(name: 'N2')
71
- end
72
- def parent
73
- end
74
- end
75
-
76
- class Comment < Model
77
- end
78
-
79
- class WebLog < Model
80
- end
81
-
82
- class Interview < Model
83
- def attachment
84
- @attachment ||= Image.new(url: 'U1')
85
- end
86
- end
87
-
88
- class Mail < Model
89
- def attachments
90
- @attachments ||= [Image.new(url: 'U1'),
91
- Video.new(html: 'H1')]
92
- end
93
- end
94
-
95
- class Image < Model
96
- end
97
-
98
- class Video < Model
99
- end
100
-
101
- ###
102
- ## Serializers
103
- ###
104
- class UserSerializer < ActiveModel::Serializer
105
- attributes :name, :email
106
-
107
- has_one :profile
108
- end
109
-
110
- class TypeSerializer < ActiveModel::Serializer
111
- attributes :name
112
- end
113
-
114
- class SelfReferencingUserParentSerializer < ActiveModel::Serializer
115
- attributes :name
116
- has_one :type, serializer: TypeSerializer, embed: :ids, include: true
117
- end
118
-
119
- class SelfReferencingUserSerializer < ActiveModel::Serializer
120
- attributes :name
121
-
122
- has_one :type, serializer: TypeSerializer, embed: :ids, include: true
123
- has_one :parent, serializer: SelfReferencingUserSerializer, embed: :ids, include: true
124
- end
125
-
126
- class UserInfoSerializer < ActiveModel::Serializer
127
- has_one :user, serializer: UserSerializer
128
- end
129
-
130
- class ProfileSerializer < ActiveModel::Serializer
131
- def description
132
- description = object.read_attribute_for_serialization(:description)
133
- scope ? "#{description} - #{scope}" : description
134
- end
135
-
136
- attributes :name, :description
137
- end
138
-
139
- class CategorySerializer < ActiveModel::Serializer
140
- attributes :name
141
-
142
- has_many :posts
143
- end
144
-
145
- class PostSerializer < ActiveModel::Serializer
146
- attributes :title, :body
147
-
148
- def title
149
- keyword = serialization_options[:highlight_keyword]
150
- title = object.read_attribute_for_serialization(:title)
151
- title = title.gsub(keyword,"'#{keyword}'") if keyword
152
- title
153
- end
154
-
155
- has_many :comments
156
- end
157
-
158
- class SpecialPostSerializer < ActiveModel::Serializer
159
- attributes :title, :body
160
- has_many :comments, root: :comments, embed_in_root: true, embed: :ids
161
- has_one :special_comment, root: :comments, embed_in_root: true, embed: :ids
162
- end
163
-
164
- class CommentSerializer < ActiveModel::Serializer
165
- attributes :content
166
- end
167
-
168
- class WebLogSerializer < ActiveModel::Serializer
169
- attributes :name, :display_name
170
- end
171
-
172
- class WebLogLowerCamelSerializer < WebLogSerializer
173
- format_keys :lower_camel
174
- end
175
-
176
- class InterviewSerializer < ActiveModel::Serializer
177
- attributes :text
178
-
179
- has_one :attachment, polymorphic: true
180
- end
181
-
182
- class MailSerializer < ActiveModel::Serializer
183
- attributes :body
184
-
185
- has_many :attachments, polymorphic: true
186
- end
187
-
188
- class ImageSerializer < ActiveModel::Serializer
189
- attributes :url
190
- end
191
-
192
- class VideoSerializer < ActiveModel::Serializer
193
- attributes :html
194
- end
195
-
196
- class ShortProfileSerializer < ::ProfileSerializer; end
197
-
198
- module TestNamespace
199
- class ProfileSerializer < ::ProfileSerializer; end
200
- class UserSerializer < ::UserSerializer; end
201
- end
202
-
203
- ActiveModel::Serializer.setup do |config|
204
- config.default_key_type = :name
205
- end
206
-
207
- class NameKeyUserSerializer < ActiveModel::Serializer
208
- attributes :name, :email
209
-
210
- has_one :profile
211
- end
212
-
213
- class NameKeyPostSerializer < ActiveModel::Serializer
214
- attributes :title, :body
215
-
216
- has_many :comments
217
- end
218
-
219
- ActiveModel::Serializer.setup do |config|
220
- config.default_key_type = nil
221
- end
222
-
223
-
@@ -1 +0,0 @@
1
- <p>Hello.</p>
@@ -1,105 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActionController
4
- module Serialization
5
- class NamespacedSerializationTest < ActionController::TestCase
6
- class TestNamespace::MyController < ActionController::Base
7
- def render_profile_with_namespace
8
- render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
9
- end
10
-
11
- def render_profiles_with_namespace
12
- render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
13
- end
14
-
15
- def render_comment
16
- render json: Comment.new(content: 'Comment 1')
17
- end
18
-
19
- def render_comments
20
- render json: [Comment.new(content: 'Comment 1')]
21
- end
22
-
23
- def render_hash
24
- render json: {message: 'not found'}, status: 404
25
- end
26
- end
27
-
28
- tests TestNamespace::MyController
29
-
30
- def test_render_profile_with_namespace
31
- get :render_profile_with_namespace
32
- assert_serializer TestNamespace::ProfileSerializer
33
- end
34
-
35
- def test_render_profiles_with_namespace
36
- get :render_profiles_with_namespace
37
- assert_serializer TestNamespace::ProfileSerializer
38
- end
39
-
40
- def test_fallback_to_a_version_without_namespace
41
- get :render_comment
42
- assert_serializer CommentSerializer
43
- end
44
-
45
- def test_array_fallback_to_a_version_without_namespace
46
- get :render_comments
47
- assert_serializer CommentSerializer
48
- end
49
-
50
- def test_render_hash_regression
51
- get :render_hash
52
- assert_equal JSON.parse(response.body), {'message' => 'not found'}
53
- end
54
- end
55
-
56
- class OptionNamespacedSerializationTest < ActionController::TestCase
57
- class MyController < ActionController::Base
58
- def default_serializer_options
59
- {
60
- namespace: TestNamespace
61
- }
62
- end
63
-
64
- def render_profile_with_namespace_option
65
- render json: Profile.new({ name: 'Name 1', description: 'Description 1'})
66
- end
67
-
68
- def render_profiles_with_namespace_option
69
- render json: [Profile.new({ name: 'Name 1', description: 'Description 1'})]
70
- end
71
-
72
- def render_comment
73
- render json: Comment.new(content: 'Comment 1')
74
- end
75
-
76
- def render_comments
77
- render json: [Comment.new(content: 'Comment 1')]
78
- end
79
- end
80
-
81
- tests MyController
82
-
83
- def test_render_profile_with_namespace_option
84
- get :render_profile_with_namespace_option
85
- assert_serializer TestNamespace::ProfileSerializer
86
- end
87
-
88
- def test_render_profiles_with_namespace_option
89
- get :render_profiles_with_namespace_option
90
- assert_serializer TestNamespace::ProfileSerializer
91
- end
92
-
93
- def test_fallback_to_a_version_without_namespace
94
- get :render_comment
95
- assert_serializer CommentSerializer
96
- end
97
-
98
- def test_array_fallback_to_a_version_without_namespace
99
- get :render_comments
100
- assert_serializer CommentSerializer
101
- end
102
- end
103
-
104
- end
105
- end
@@ -1,287 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActionController
4
- module Serialization
5
- class ImplicitSerializerTest < ActionController::TestCase
6
- class MyController < ActionController::Base
7
- def render_using_implicit_serializer
8
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
- end
10
- end
11
-
12
- tests MyController
13
-
14
- def test_render_using_implicit_serializer
15
- get :render_using_implicit_serializer
16
- assert_equal 'application/json', @response.content_type
17
- assert_equal '{"profile":{"name":"Name 1","description":"Description 1"}}', @response.body
18
- end
19
- end
20
-
21
- class ImplicitSerializerScopeTest < ActionController::TestCase
22
- class MyController < ActionController::Base
23
- def render_using_implicit_serializer_and_scope
24
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
25
- end
26
-
27
- private
28
-
29
- def current_user
30
- 'current_user'
31
- end
32
- end
33
-
34
- tests MyController
35
-
36
- def test_render_using_implicit_serializer_and_scope
37
- get :render_using_implicit_serializer_and_scope
38
- assert_equal 'application/json', @response.content_type
39
- assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body
40
- end
41
- end
42
-
43
- class DefaultOptionsForSerializerScopeTest < ActionController::TestCase
44
- class MyController < ActionController::Base
45
- def default_serializer_options
46
- { scope: current_admin }
47
- end
48
-
49
- def render_using_scope_set_in_default_serializer_options
50
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
51
- end
52
-
53
- private
54
-
55
- def current_user
56
- 'current_user'
57
- end
58
-
59
- def current_admin
60
- 'current_admin'
61
- end
62
- end
63
-
64
- tests MyController
65
-
66
- def test_render_using_scope_set_in_default_serializer_options
67
- get :render_using_scope_set_in_default_serializer_options
68
- assert_equal 'application/json', @response.content_type
69
- assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
70
- end
71
- end
72
-
73
- class ExplicitSerializerScopeTest < ActionController::TestCase
74
- class MyController < ActionController::Base
75
- def render_using_implicit_serializer_and_explicit_scope
76
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }), scope: current_admin
77
- end
78
-
79
- private
80
-
81
- def current_user
82
- 'current_user'
83
- end
84
-
85
- def current_admin
86
- 'current_admin'
87
- end
88
- end
89
-
90
- tests MyController
91
-
92
- def test_render_using_implicit_serializer_and_explicit_scope
93
- get :render_using_implicit_serializer_and_explicit_scope
94
- assert_equal 'application/json', @response.content_type
95
- assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
96
- end
97
- end
98
-
99
- class OverridingSerializationScopeTest < ActionController::TestCase
100
- class MyController < ActionController::Base
101
- def render_overriding_serialization_scope
102
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
103
- end
104
-
105
- private
106
-
107
- def current_user
108
- 'current_user'
109
- end
110
-
111
- def serialization_scope
112
- 'current_admin'
113
- end
114
- end
115
-
116
- tests MyController
117
-
118
- def test_render_overriding_serialization_scope
119
- get :render_overriding_serialization_scope
120
- assert_equal 'application/json', @response.content_type
121
- assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_admin"}}', @response.body
122
- end
123
- end
124
-
125
- class CallingSerializationScopeTest < ActionController::TestCase
126
- class MyController < ActionController::Base
127
- def render_calling_serialization_scope
128
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
129
- end
130
-
131
- private
132
-
133
- def current_user
134
- 'current_user'
135
- end
136
-
137
- serialization_scope :current_user
138
- end
139
-
140
- tests MyController
141
-
142
- def test_render_calling_serialization_scope
143
- get :render_calling_serialization_scope
144
- assert_equal 'application/json', @response.content_type
145
- assert_equal '{"profile":{"name":"Name 1","description":"Description 1 - current_user"}}', @response.body
146
- end
147
- end
148
-
149
- class JSONDumpSerializerTest < ActionController::TestCase
150
- class MyController < ActionController::Base
151
- def render_using_json_dump
152
- render json: JSON.dump(hello: 'world')
153
- end
154
- end
155
-
156
- tests MyController
157
-
158
- def test_render_using_json_dump
159
- get :render_using_json_dump
160
- assert_equal 'application/json', @response.content_type
161
- assert_equal '{"hello":"world"}', @response.body
162
- end
163
- end
164
-
165
- class RailsSerializerTest < ActionController::TestCase
166
- class MyController < ActionController::Base
167
- def render_using_rails_behavior
168
- render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })], serializer: false
169
- end
170
- end
171
-
172
- tests MyController
173
-
174
- def test_render_using_rails_behavior
175
- get :render_using_rails_behavior
176
- assert_equal 'application/json', @response.content_type
177
- assert_equal '[{"attributes":{"name":"Name 1","description":"Description 1","comments":"Comments 1"}}]', @response.body
178
- end
179
- end
180
-
181
- class ArraySerializerTest < ActionController::TestCase
182
- class MyController < ActionController::Base
183
- def render_array
184
- render json: [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })]
185
- end
186
- end
187
-
188
- tests MyController
189
-
190
- def test_render_array
191
- get :render_array
192
- assert_equal 'application/json', @response.content_type
193
- assert_equal '{"my":[{"name":"Name 1","description":"Description 1"}]}', @response.body
194
- end
195
- end
196
-
197
- class LowerCamelArraySerializerTest < ActionController::TestCase
198
- class WebLogController < ActionController::Base
199
- def render_array
200
- render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}), WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})], each_serializer: WebLogLowerCamelSerializer
201
- end
202
- end
203
-
204
- tests WebLogController
205
-
206
- def test_render_array
207
- get :render_array
208
- assert_equal 'application/json', @response.content_type
209
- assert_equal '{"webLog":[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]}', @response.body
210
- end
211
- end
212
-
213
- class LowerCamelWoRootSerializerTest < ActionController::TestCase
214
- class WebLogController < ActionController::Base
215
- def render_without_root
216
- render json: WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
217
- root: false,
218
- serializer: WebLogLowerCamelSerializer
219
- end
220
- end
221
-
222
- tests WebLogController
223
-
224
- def test_render_without_root
225
- get :render_without_root
226
- assert_equal 'application/json', @response.content_type
227
- assert_equal '{"name":"Name 1","displayName":"Display Name 1"}', @response.body
228
- end
229
- end
230
-
231
- class LowerCamelArrayWoRootSerializerTest < ActionController::TestCase
232
- class WebLogController < ActionController::Base
233
- def render_array_without_root
234
- render json: [WebLog.new({name: 'Name 1', display_name: 'Display Name 1'}),
235
- WebLog.new({name: 'Name 2', display_name: 'Display Name 2'})],
236
- root: false,
237
- each_serializer: WebLogLowerCamelSerializer
238
- end
239
- end
240
-
241
- tests WebLogController
242
-
243
- def test_render_array_without_root
244
- get :render_array_without_root
245
- assert_equal 'application/json', @response.content_type
246
- assert_equal '[{"name":"Name 1","displayName":"Display Name 1"},{"name":"Name 2","displayName":"Display Name 2"}]', @response.body
247
- end
248
- end
249
-
250
- class ArrayEmbedingSerializerTest < ActionController::TestCase
251
- def setup
252
- super
253
- @association = UserSerializer._associations[:profile]
254
- @old_association = @association.dup
255
- end
256
-
257
- def teardown
258
- super
259
- UserSerializer._associations[:profile] = @old_association
260
- end
261
-
262
- class MyController < ActionController::Base
263
- def initialize(*)
264
- super
265
- @user = User.new({ name: 'Name 1', email: 'mail@server.com', gender: 'M' })
266
- end
267
- attr_reader :user
268
-
269
- def render_array_embeding_in_root
270
- render json: [@user]
271
- end
272
- end
273
-
274
- tests MyController
275
-
276
- def test_render_array_embeding_in_root
277
- @association.embed = :ids
278
- @association.embed_in_root = true
279
-
280
- get :render_array_embeding_in_root
281
- assert_equal 'application/json', @response.content_type
282
-
283
- assert_equal("{\"my\":[{\"name\":\"Name 1\",\"email\":\"mail@server.com\",\"profile_id\":#{@controller.user.profile.object_id}}],\"profiles\":[{\"name\":\"N1\",\"description\":\"D1\"}]}", @response.body)
284
- end
285
- end
286
- end
287
- end