active_model_serializers 0.8.3 → 0.10.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE.md +29 -0
  3. data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
  4. data/.gitignore +7 -0
  5. data/.rubocop.yml +104 -0
  6. data/.rubocop_todo.yml +167 -0
  7. data/.simplecov +110 -0
  8. data/.travis.yml +46 -23
  9. data/CHANGELOG.md +442 -6
  10. data/CONTRIBUTING.md +95 -0
  11. data/Gemfile +51 -1
  12. data/{MIT-LICENSE.txt → MIT-LICENSE} +3 -2
  13. data/README.md +102 -590
  14. data/Rakefile +93 -8
  15. data/active_model_serializers.gemspec +65 -23
  16. data/appveyor.yml +28 -0
  17. data/bin/bench +171 -0
  18. data/bin/bench_regression +316 -0
  19. data/bin/serve_benchmark +39 -0
  20. data/docs/ARCHITECTURE.md +126 -0
  21. data/docs/README.md +39 -0
  22. data/docs/STYLE.md +58 -0
  23. data/docs/general/adapters.md +245 -0
  24. data/docs/general/caching.md +52 -0
  25. data/docs/general/configuration_options.md +100 -0
  26. data/docs/general/deserialization.md +100 -0
  27. data/docs/general/getting_started.md +133 -0
  28. data/docs/general/instrumentation.md +40 -0
  29. data/docs/general/key_transforms.md +40 -0
  30. data/docs/general/logging.md +14 -0
  31. data/docs/general/rendering.md +255 -0
  32. data/docs/general/serializers.md +339 -0
  33. data/docs/how-open-source-maintained.jpg +0 -0
  34. data/docs/howto/add_pagination_links.md +139 -0
  35. data/docs/howto/add_root_key.md +51 -0
  36. data/docs/howto/outside_controller_use.md +58 -0
  37. data/docs/howto/passing_arbitrary_options.md +27 -0
  38. data/docs/howto/test.md +152 -0
  39. data/docs/integrations/ember-and-json-api.md +112 -0
  40. data/docs/integrations/grape.md +19 -0
  41. data/docs/jsonapi/errors.md +56 -0
  42. data/docs/jsonapi/schema/schema.json +366 -0
  43. data/docs/jsonapi/schema.md +151 -0
  44. data/docs/rfcs/0000-namespace.md +106 -0
  45. data/docs/rfcs/template.md +15 -0
  46. data/lib/action_controller/serialization.rb +31 -36
  47. data/lib/active_model/serializable_resource.rb +11 -0
  48. data/lib/active_model/serializer/adapter/attributes.rb +15 -0
  49. data/lib/active_model/serializer/adapter/base.rb +16 -0
  50. data/lib/active_model/serializer/adapter/json.rb +15 -0
  51. data/lib/active_model/serializer/adapter/json_api.rb +15 -0
  52. data/lib/active_model/serializer/adapter/null.rb +15 -0
  53. data/lib/active_model/serializer/adapter.rb +24 -0
  54. data/lib/active_model/serializer/array_serializer.rb +9 -0
  55. data/lib/active_model/serializer/association.rb +19 -0
  56. data/lib/active_model/serializer/associations.rb +87 -220
  57. data/lib/active_model/serializer/attribute.rb +25 -0
  58. data/lib/active_model/serializer/attributes.rb +82 -0
  59. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  60. data/lib/active_model/serializer/caching.rb +151 -0
  61. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  62. data/lib/active_model/serializer/collection_serializer.rb +64 -0
  63. data/lib/active_model/serializer/configuration.rb +35 -0
  64. data/lib/active_model/serializer/error_serializer.rb +10 -0
  65. data/lib/active_model/serializer/errors_serializer.rb +27 -0
  66. data/lib/active_model/serializer/field.rb +56 -0
  67. data/lib/active_model/serializer/fieldset.rb +31 -0
  68. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  69. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  70. data/lib/active_model/serializer/include_tree.rb +111 -0
  71. data/lib/active_model/serializer/links.rb +35 -0
  72. data/lib/active_model/serializer/lint.rb +156 -0
  73. data/lib/active_model/serializer/meta.rb +29 -0
  74. data/lib/active_model/serializer/null.rb +17 -0
  75. data/lib/active_model/serializer/reflection.rb +147 -0
  76. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  77. data/lib/active_model/serializer/type.rb +25 -0
  78. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  79. data/lib/active_model/serializer.rb +156 -481
  80. data/lib/active_model_serializers/adapter/attributes.rb +94 -0
  81. data/lib/active_model_serializers/adapter/base.rb +90 -0
  82. data/lib/active_model_serializers/adapter/json.rb +11 -0
  83. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
  84. data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
  85. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
  86. data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
  87. data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
  88. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
  89. data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
  90. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
  91. data/lib/active_model_serializers/adapter/json_api.rb +513 -0
  92. data/lib/active_model_serializers/adapter/null.rb +10 -0
  93. data/lib/active_model_serializers/adapter.rb +92 -0
  94. data/lib/active_model_serializers/cached_serializer.rb +87 -0
  95. data/lib/active_model_serializers/callbacks.rb +55 -0
  96. data/lib/active_model_serializers/deprecate.rb +55 -0
  97. data/lib/active_model_serializers/deserialization.rb +13 -0
  98. data/lib/active_model_serializers/fragment_cache.rb +118 -0
  99. data/lib/active_model_serializers/json_pointer.rb +14 -0
  100. data/lib/active_model_serializers/key_transform.rb +70 -0
  101. data/lib/active_model_serializers/logging.rb +122 -0
  102. data/lib/active_model_serializers/model.rb +49 -0
  103. data/lib/active_model_serializers/railtie.rb +46 -0
  104. data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
  105. data/lib/active_model_serializers/serializable_resource.rb +81 -0
  106. data/lib/active_model_serializers/serialization_context.rb +32 -0
  107. data/lib/active_model_serializers/test/schema.rb +103 -0
  108. data/lib/active_model_serializers/test/serializer.rb +125 -0
  109. data/lib/active_model_serializers/test.rb +7 -0
  110. data/lib/active_model_serializers.rb +34 -89
  111. data/lib/generators/rails/USAGE +6 -0
  112. data/lib/generators/rails/resource_override.rb +10 -0
  113. data/lib/generators/rails/serializer_generator.rb +36 -0
  114. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  115. data/lib/grape/active_model_serializers.rb +14 -0
  116. data/lib/grape/formatters/active_model_serializers.rb +15 -0
  117. data/lib/grape/helpers/active_model_serializers.rb +16 -0
  118. data/test/action_controller/adapter_selector_test.rb +53 -0
  119. data/test/action_controller/explicit_serializer_test.rb +134 -0
  120. data/test/action_controller/json/include_test.rb +167 -0
  121. data/test/action_controller/json_api/deserialization_test.rb +112 -0
  122. data/test/action_controller/json_api/errors_test.rb +41 -0
  123. data/test/action_controller/json_api/linked_test.rb +197 -0
  124. data/test/action_controller/json_api/pagination_test.rb +116 -0
  125. data/test/action_controller/json_api/transform_test.rb +180 -0
  126. data/test/action_controller/serialization_scope_name_test.rb +229 -0
  127. data/test/action_controller/serialization_test.rb +467 -0
  128. data/test/active_model_serializers/adapter_for_test.rb +208 -0
  129. data/test/active_model_serializers/cached_serializer_test.rb +80 -0
  130. data/test/active_model_serializers/fragment_cache_test.rb +34 -0
  131. data/test/active_model_serializers/json_pointer_test.rb +20 -0
  132. data/test/active_model_serializers/key_transform_test.rb +263 -0
  133. data/test/active_model_serializers/logging_test.rb +77 -0
  134. data/test/active_model_serializers/model_test.rb +9 -0
  135. data/test/active_model_serializers/railtie_test_isolated.rb +63 -0
  136. data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
  137. data/test/active_model_serializers/test/schema_test.rb +128 -0
  138. data/test/active_model_serializers/test/serializer_test.rb +63 -0
  139. data/test/active_record_test.rb +9 -0
  140. data/test/adapter/deprecation_test.rb +100 -0
  141. data/test/adapter/json/belongs_to_test.rb +45 -0
  142. data/test/adapter/json/collection_test.rb +90 -0
  143. data/test/adapter/json/has_many_test.rb +45 -0
  144. data/test/adapter/json/transform_test.rb +93 -0
  145. data/test/adapter/json_api/belongs_to_test.rb +155 -0
  146. data/test/adapter/json_api/collection_test.rb +95 -0
  147. data/test/adapter/json_api/errors_test.rb +78 -0
  148. data/test/adapter/json_api/fields_test.rb +87 -0
  149. data/test/adapter/json_api/has_many_embed_ids_test.rb +43 -0
  150. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +96 -0
  151. data/test/adapter/json_api/has_many_test.rb +143 -0
  152. data/test/adapter/json_api/has_one_test.rb +79 -0
  153. data/test/adapter/json_api/json_api_test.rb +35 -0
  154. data/test/adapter/json_api/linked_test.rb +392 -0
  155. data/test/adapter/json_api/links_test.rb +93 -0
  156. data/test/adapter/json_api/pagination_links_test.rb +148 -0
  157. data/test/adapter/json_api/parse_test.rb +137 -0
  158. data/test/adapter/json_api/relationship_test.rb +161 -0
  159. data/test/adapter/json_api/relationships_test.rb +199 -0
  160. data/test/adapter/json_api/resource_identifier_test.rb +85 -0
  161. data/test/adapter/json_api/resource_meta_test.rb +100 -0
  162. data/test/adapter/json_api/toplevel_jsonapi_test.rb +82 -0
  163. data/test/adapter/json_api/transform_test.rb +500 -0
  164. data/test/adapter/json_api/type_test.rb +61 -0
  165. data/test/adapter/json_test.rb +45 -0
  166. data/test/adapter/null_test.rb +23 -0
  167. data/test/adapter/polymorphic_test.rb +72 -0
  168. data/test/adapter_test.rb +40 -0
  169. data/test/array_serializer_test.rb +35 -73
  170. data/test/benchmark/app.rb +65 -0
  171. data/test/benchmark/benchmarking_support.rb +67 -0
  172. data/test/benchmark/bm_caching.rb +117 -0
  173. data/test/benchmark/bm_transform.rb +34 -0
  174. data/test/benchmark/config.ru +3 -0
  175. data/test/benchmark/controllers.rb +77 -0
  176. data/test/benchmark/fixtures.rb +167 -0
  177. data/test/cache_test.rb +388 -0
  178. data/test/collection_serializer_test.rb +110 -0
  179. data/test/fixtures/active_record.rb +68 -0
  180. data/test/fixtures/poro.rb +254 -0
  181. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  182. data/test/generators/serializer_generator_test.rb +57 -0
  183. data/test/grape_test.rb +82 -0
  184. data/test/include_tree/from_include_args_test.rb +26 -0
  185. data/test/include_tree/from_string_test.rb +94 -0
  186. data/test/include_tree/include_args_to_hash_test.rb +64 -0
  187. data/test/lint_test.rb +49 -0
  188. data/test/logger_test.rb +18 -0
  189. data/test/poro_test.rb +9 -0
  190. data/test/serializable_resource_test.rb +83 -0
  191. data/test/serializers/association_macros_test.rb +36 -0
  192. data/test/serializers/associations_test.rb +267 -0
  193. data/test/serializers/attribute_test.rb +123 -0
  194. data/test/serializers/attributes_test.rb +52 -0
  195. data/test/serializers/caching_configuration_test_isolated.rb +170 -0
  196. data/test/serializers/configuration_test.rb +32 -0
  197. data/test/serializers/fieldset_test.rb +14 -0
  198. data/test/serializers/meta_test.rb +198 -0
  199. data/test/serializers/options_test.rb +21 -0
  200. data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
  201. data/test/serializers/root_test.rb +21 -0
  202. data/test/serializers/serialization_test.rb +55 -0
  203. data/test/serializers/serializer_for_test.rb +134 -0
  204. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  205. data/test/support/isolated_unit.rb +80 -0
  206. data/test/support/rails5_shims.rb +47 -0
  207. data/test/support/rails_app.rb +45 -0
  208. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  209. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  210. data/test/support/schemas/custom/show.json +7 -0
  211. data/test/support/schemas/hyper_schema.json +93 -0
  212. data/test/support/schemas/render_using_json_api.json +43 -0
  213. data/test/support/schemas/simple_json_pointers.json +10 -0
  214. data/test/support/serialization_testing.rb +53 -0
  215. data/test/test_helper.rb +51 -24
  216. metadata +456 -45
  217. data/DESIGN.textile +0 -586
  218. data/Gemfile.edge +0 -9
  219. data/bench/perf.rb +0 -43
  220. data/cruft.md +0 -19
  221. data/lib/active_model/array_serializer.rb +0 -104
  222. data/lib/active_record/serializer_override.rb +0 -16
  223. data/lib/generators/resource_override.rb +0 -13
  224. data/lib/generators/serializer/USAGE +0 -9
  225. data/lib/generators/serializer/serializer_generator.rb +0 -42
  226. data/lib/generators/serializer/templates/serializer.rb +0 -19
  227. data/test/association_test.rb +0 -592
  228. data/test/caching_test.rb +0 -96
  229. data/test/generators_test.rb +0 -85
  230. data/test/no_serialization_scope_test.rb +0 -34
  231. data/test/serialization_scope_name_test.rb +0 -67
  232. data/test/serialization_test.rb +0 -392
  233. data/test/serializer_support_test.rb +0 -51
  234. data/test/serializer_test.rb +0 -1465
  235. data/test/test_fakes.rb +0 -217
@@ -1,1465 +0,0 @@
1
- require "test_helper"
2
- require "test_fakes"
3
-
4
- class SerializerTest < ActiveModel::TestCase
5
- def test_scope_works_correct
6
- serializer = ActiveModel::Serializer.new :foo, :scope => :bar
7
- assert_equal serializer.scope, :bar
8
- end
9
-
10
- def test_attributes
11
- user = User.new
12
- user_serializer = DefaultUserSerializer.new(user, {})
13
-
14
- hash = user_serializer.as_json
15
-
16
- assert_equal({
17
- :default_user => { :first_name => "Jose", :last_name => "Valim" }
18
- }, hash)
19
- end
20
-
21
- def test_attributes_method
22
- user = User.new
23
- user_serializer = UserSerializer.new(user, :scope => {})
24
-
25
- hash = user_serializer.as_json
26
-
27
- assert_equal({
28
- :user => { :first_name => "Jose", :last_name => "Valim", :ok => true }
29
- }, hash)
30
- end
31
-
32
- def test_attributes_method_specifying_keys
33
- user = User.new
34
- user_serializer = UserAttributesWithKeySerializer.new(user, :scope => {})
35
-
36
- hash = user_serializer.as_json
37
-
38
- assert_equal({
39
- :user_attributes_with_key => { :f_name => "Jose", :l_name => "Valim", :ok => true }
40
- }, hash)
41
- end
42
-
43
- def test_attributes_method_specifying_some_keys
44
- user = User.new
45
- user_serializer = UserAttributesWithSomeKeySerializer.new(user, :scope => {})
46
-
47
- hash = user_serializer.as_json
48
-
49
- assert_equal({
50
- :user_attributes_with_some_key => { :first_name => "Jose", :l_name => "Valim", :ok => true }
51
- }, hash)
52
- end
53
-
54
- def test_attributes_method_with_unsymbolizable_key
55
- user = User.new
56
- user_serializer = UserAttributesWithUnsymbolizableKeySerializer.new(user, :scope => {})
57
-
58
- hash = user_serializer.as_json
59
-
60
- assert_equal({
61
- :user_attributes_with_unsymbolizable_key => { :first_name => "Jose", :"last-name" => "Valim", :ok => true }
62
- }, hash)
63
- end
64
-
65
- def test_attribute_method_with_name_as_serializer_prefix
66
- object = SomeObject.new("something")
67
- object_serializer = SomeSerializer.new(object, {})
68
-
69
- hash = object_serializer.as_json
70
-
71
- assert_equal({
72
- :some => { :some => "something" }
73
- }, hash)
74
- end
75
-
76
- def test_serializer_receives_scope
77
- user = User.new
78
- user_serializer = UserSerializer.new(user, :scope => {:scope => true})
79
-
80
- hash = user_serializer.as_json
81
-
82
- assert_equal({
83
- :user => {
84
- :first_name => "Jose",
85
- :last_name => "Valim",
86
- :ok => true,
87
- :scope => true
88
- }
89
- }, hash)
90
- end
91
-
92
- def test_serializer_receives_url_options
93
- user = User.new
94
- user_serializer = UserSerializer.new(user, :url_options => { :host => "test.local" })
95
- assert_equal({ :host => "test.local" }, user_serializer.url_options)
96
- end
97
-
98
- def test_serializer_returns_empty_hash_without_url_options
99
- user = User.new
100
- user_serializer = UserSerializer.new(user)
101
- assert_equal({}, user_serializer.url_options)
102
- end
103
-
104
- def test_pretty_accessors
105
- user = User.new
106
- user.superuser = true
107
- user_serializer = MyUserSerializer.new(user)
108
-
109
- hash = user_serializer.as_json
110
-
111
- assert_equal({
112
- :my_user => {
113
- :first_name => "Jose", :last_name => "Valim", :super_user => true
114
- }
115
- }, hash)
116
- end
117
-
118
- def test_has_many
119
- user = User.new
120
-
121
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
122
- comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
123
- post.comments = comments
124
-
125
- post_serializer = PostSerializer.new(post, :scope => user)
126
-
127
- assert_equal({
128
- :post => {
129
- :title => "New Post",
130
- :body => "Body of new post",
131
- :comments => [
132
- { :title => "Comment1" },
133
- { :title => "Comment2" }
134
- ]
135
- }
136
- }, post_serializer.as_json)
137
- end
138
-
139
- def test_conditionally_included_associations
140
- user = User.new
141
-
142
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
143
- comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
144
- post.comments = comments
145
-
146
- post_serializer = PostWithConditionalCommentsSerializer.new(post, :scope => user)
147
-
148
- # comments enabled
149
- post.comments_disabled = false
150
- assert_equal({
151
- :post => {
152
- :title => "New Post",
153
- :body => "Body of new post",
154
- :comments => [
155
- { :title => "Comment1" },
156
- { :title => "Comment2" }
157
- ]
158
- }
159
- }, post_serializer.as_json)
160
-
161
- # comments disabled
162
- post.comments_disabled = true
163
- assert_equal({
164
- :post => {
165
- :title => "New Post",
166
- :body => "Body of new post"
167
- }
168
- }, post_serializer.as_json)
169
- end
170
-
171
- def test_conditionally_included_associations_and_attributes
172
- user = User.new
173
-
174
- post = Post.new(:title => "New Post", :body => "Body of new post", :author => 'Sausage King', :email => "tenderlove@tenderlove.com")
175
- comments = [Comment.new(:title => "Comment1"), Comment.new(:title => "Comment2")]
176
- post.comments = comments
177
-
178
- post_serializer = PostWithMultipleConditionalsSerializer.new(post, :scope => user)
179
-
180
- # comments enabled
181
- post.comments_disabled = false
182
- assert_equal({
183
- :post => {
184
- :title => "New Post",
185
- :body => "Body of new post",
186
- :comments => [
187
- { :title => "Comment1" },
188
- { :title => "Comment2" }
189
- ]
190
- }
191
- }, post_serializer.as_json)
192
-
193
- # comments disabled
194
- post.comments_disabled = true
195
- assert_equal({
196
- :post => {
197
- :title => "New Post",
198
- :body => "Body of new post"
199
- }
200
- }, post_serializer.as_json)
201
-
202
- # superuser - should see author
203
- user.superuser = true
204
- assert_equal({
205
- :post => {
206
- :title => "New Post",
207
- :body => "Body of new post",
208
- :author => "Sausage King"
209
- }
210
- }, post_serializer.as_json)
211
- end
212
-
213
- def test_has_one
214
- user = User.new
215
- blog = Blog.new
216
- blog.author = user
217
-
218
- json = BlogSerializer.new(blog, :scope => user).as_json
219
- assert_equal({
220
- :blog => {
221
- :author => {
222
- :first_name => "Jose",
223
- :last_name => "Valim"
224
- }
225
- }
226
- }, json)
227
- end
228
-
229
- def test_overridden_associations
230
- author_serializer = Class.new(ActiveModel::Serializer) do
231
- attributes :first_name
232
- end
233
-
234
- blog_serializer = Class.new(ActiveModel::Serializer) do
235
- def person
236
- object.author
237
- end
238
-
239
- has_one :person, :serializer => author_serializer
240
- end
241
-
242
- user = User.new
243
- blog = Blog.new
244
- blog.author = user
245
-
246
- json = blog_serializer.new(blog, :scope => user).as_json
247
- assert_equal({
248
- :person => {
249
- :first_name => "Jose"
250
- }
251
- }, json)
252
- end
253
-
254
- def post_serializer
255
- Class.new(ActiveModel::Serializer) do
256
- attributes :title, :body
257
- has_many :comments, :serializer => CommentSerializer
258
- has_one :author, :serializer => DefaultUserSerializer
259
- end
260
- end
261
-
262
- def test_associations_with_nil_association
263
- user = User.new
264
- blog = Blog.new
265
-
266
- json = BlogSerializer.new(blog, :scope => user).as_json
267
- assert_equal({
268
- :blog => { :author => nil }
269
- }, json)
270
-
271
- serializer = Class.new(BlogSerializer) do
272
- root :blog
273
- end
274
-
275
- json = serializer.new(blog, :scope => user).as_json
276
- assert_equal({ :blog => { :author => nil } }, json)
277
- end
278
-
279
- def test_custom_root
280
- user = User.new
281
- blog = Blog.new
282
-
283
- serializer = Class.new(BlogSerializer) do
284
- root :my_blog
285
- end
286
-
287
- assert_equal({ :my_blog => { :author => nil } }, serializer.new(blog, :scope => user).as_json)
288
- end
289
-
290
- def test_nil_root_object
291
- user = User.new
292
- blog = nil
293
-
294
- serializer = Class.new(BlogSerializer) do
295
- root false
296
- end
297
-
298
- assert_equal(nil, serializer.new(blog, :scope => user).as_json)
299
- end
300
-
301
- def test_custom_root_with_nil_root_object
302
- user = User.new
303
- blog = nil
304
-
305
- serializer = Class.new(BlogSerializer) do
306
- root :my_blog
307
- end
308
-
309
- assert_equal({ :my_blog => nil }, serializer.new(blog, :scope => user).as_json)
310
- end
311
-
312
- def test_false_root
313
- user = User.new
314
- blog = Blog.new
315
-
316
- serializer = Class.new(BlogSerializer) do
317
- root false
318
- end
319
-
320
- another_serializer = Class.new(BlogSerializer) do
321
- self.root = false
322
- end
323
-
324
- assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
325
- assert_equal({ :author => nil }, another_serializer.new(blog, :scope => user).as_json)
326
-
327
- # test inherited false root
328
- serializer = Class.new(serializer)
329
- assert_equal({ :author => nil }, serializer.new(blog, :scope => user).as_json)
330
- end
331
-
332
- def test_true_root
333
- blog = Blog.new
334
-
335
- assert_equal({
336
- :blog_with_root => {
337
- :author => nil,
338
- }
339
- }, BlogWithRootSerializer.new(blog).as_json)
340
- end
341
-
342
- def test_root_false_on_load_active_model_serializers
343
- begin
344
- ActiveSupport.on_load(:active_model_serializers) do
345
- self.root = false
346
- end
347
-
348
- blog = Blog.new
349
- serializer = BlogSerializer.new(blog)
350
-
351
- assert_equal({ :author => nil }, serializer.as_json)
352
- ensure
353
- ActiveSupport.on_load(:active_model_serializers) do
354
- self.root = nil
355
- end
356
- end
357
- end
358
-
359
- def test_embed_ids
360
- serializer = post_serializer
361
-
362
- serializer.class_eval do
363
- root :post
364
- embed :ids
365
- end
366
-
367
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
368
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
369
- post.comments = comments
370
-
371
- serializer = serializer.new(post)
372
-
373
- assert_equal({
374
- :post => {
375
- :title => "New Post",
376
- :body => "Body of new post",
377
- :comment_ids => [1, 2],
378
- :author_id => nil
379
- }
380
- }, serializer.as_json)
381
- end
382
-
383
- def test_embed_ids_include_true
384
- serializer_class = post_serializer
385
-
386
- serializer_class.class_eval do
387
- root :post
388
- embed :ids, :include => true
389
- end
390
-
391
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
392
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
393
- post.comments = comments
394
-
395
- serializer = serializer_class.new(post)
396
-
397
- assert_equal({
398
- :post => {
399
- :title => "New Post",
400
- :body => "Body of new post",
401
- :comment_ids => [1, 2],
402
- :author_id => nil
403
- },
404
- :comments => [
405
- { :title => "Comment1" },
406
- { :title => "Comment2" }
407
- ],
408
- :authors => []
409
- }, serializer.as_json)
410
-
411
- post.author = User.new(:id => 1)
412
-
413
- serializer = serializer_class.new(post)
414
-
415
- assert_equal({
416
- :post => {
417
- :title => "New Post",
418
- :body => "Body of new post",
419
- :comment_ids => [1, 2],
420
- :author_id => 1
421
- },
422
- :comments => [
423
- { :title => "Comment1" },
424
- { :title => "Comment2" }
425
- ],
426
- :authors => [{ :first_name => "Jose", :last_name => "Valim" }]
427
- }, serializer.as_json)
428
- end
429
-
430
- def test_methods_take_priority_over_associations
431
- post_serializer = Class.new(ActiveModel::Serializer) do
432
- attributes :title
433
- has_many :comments
434
- embed :ids
435
-
436
- def comments
437
- object.comments[0,1]
438
- end
439
- end
440
-
441
- post = Post.new(:title => "My Post")
442
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
443
- post.comments = comments
444
-
445
- post.class_eval do
446
- define_method :comment_ids, lambda {
447
- self.comments.map { |c| c.read_attribute_for_serialization(:id) }
448
- }
449
- end
450
- json = post_serializer.new(post).as_json
451
- assert_equal({
452
- :title => "My Post",
453
- :comment_ids => [1]
454
- }, json)
455
- end
456
-
457
- def test_embed_objects
458
- serializer = post_serializer
459
-
460
- serializer.class_eval do
461
- root :post
462
- embed :objects
463
- end
464
-
465
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
466
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
467
- post.comments = comments
468
-
469
- serializer = serializer.new(post)
470
-
471
- assert_equal({
472
- :post => {
473
- :title => "New Post",
474
- :body => "Body of new post",
475
- :author => nil,
476
- :comments => [
477
- { :title => "Comment1" },
478
- { :title => "Comment2" }
479
- ]
480
- }
481
- }, serializer.as_json)
482
- end
483
-
484
- def test_sets_can_be_serialized
485
- post1 = Post.new(:title => "Post1", :author => "Author1", :id => 1)
486
- post2 = Post.new(:title => "Post2", :author => "Author2", :id => 2)
487
-
488
- set = Set.new
489
- set << post1
490
- set << post2
491
-
492
- serializer = set.active_model_serializer.new set, :each_serializer => CustomPostSerializer
493
-
494
- as_json = serializer.as_json
495
- assert_equal 2, as_json.size
496
- assert as_json.include?({ :title => "Post1" })
497
- assert as_json.include?({ :title => "Post2" })
498
- end
499
-
500
- def test_associations_with_as
501
- posts = [
502
- Post.new(:title => 'First Post', :body => 'text'),
503
- Post.new(:title => 'Second Post', :body => 'text')
504
- ]
505
- user = User.new
506
-
507
- custom_blog = CustomBlog.new
508
- custom_blog.public_posts = posts
509
- custom_blog.public_user = user
510
-
511
- serializer = CustomBlogSerializer.new(custom_blog, :scope => { :scope => true })
512
-
513
- assert_equal({
514
- :custom_blog => {
515
- :posts => [
516
- {:title => 'First Post', :body => 'text', :comments => []},
517
- {:title => 'Second Post', :body => 'text', :comments => []}
518
- ],
519
- :user => {
520
- :first_name => "Jose",
521
- :last_name => "Valim", :ok => true,
522
- :scope => true
523
- }
524
- }
525
- }, serializer.as_json)
526
- end
527
-
528
- def test_implicity_detection_for_association_serializers
529
- implicit_serializer = Class.new(ActiveModel::Serializer) do
530
- root :custom_blog
531
- const_set(:UserSerializer, UserSerializer)
532
- const_set(:PostSerializer, PostSerializer)
533
-
534
- has_many :public_posts, :key => :posts
535
- has_one :public_user, :key => :user
536
- end
537
-
538
- posts = [
539
- Post.new(:title => 'First Post', :body => 'text', :comments => []),
540
- Post.new(:title => 'Second Post', :body => 'text', :comments => [])
541
- ]
542
- user = User.new
543
-
544
- custom_blog = CustomBlog.new
545
- custom_blog.public_posts = posts
546
- custom_blog.public_user = user
547
-
548
- serializer = implicit_serializer.new(custom_blog, :scope => { :scope => true })
549
-
550
- assert_equal({
551
- :custom_blog => {
552
- :posts => [
553
- {:title => 'First Post', :body => 'text', :comments => []},
554
- {:title => 'Second Post', :body => 'text', :comments => []}
555
- ],
556
- :user => {
557
- :first_name => "Jose",
558
- :last_name => "Valim", :ok => true,
559
- :scope => true
560
- }
561
- }
562
- }, serializer.as_json)
563
- end
564
-
565
- def test_attribute_key
566
- serializer_class = Class.new(ActiveModel::Serializer) do
567
- root :user
568
-
569
- attribute :first_name, :key => :firstName
570
- attribute :last_name, :key => :lastName
571
- attribute :password
572
- end
573
-
574
- serializer = serializer_class.new(User.new)
575
-
576
- assert_equal({
577
- :user => {
578
- :firstName => "Jose",
579
- :lastName => "Valim",
580
- :password => "oh noes yugive my password"
581
- }
582
- }, serializer.as_json)
583
- end
584
-
585
- def setup_model
586
- Class.new do
587
- class << self
588
- def columns_hash
589
- { "name" => Struct.new(:type).new(:string), "age" => Struct.new(:type).new(:integer) }
590
- end
591
-
592
- def reflect_on_association(name)
593
- case name
594
- when :posts
595
- Struct.new(:macro, :name).new(:has_many, :posts)
596
- when :parent
597
- Struct.new(:macro, :name).new(:belongs_to, :parent)
598
- end
599
- end
600
- end
601
- end
602
- end
603
-
604
- def test_schema
605
- model = setup_model
606
-
607
- serializer = Class.new(ActiveModel::Serializer) do
608
- class << self; self; end.class_eval do
609
- define_method(:model_class) do model end
610
- end
611
-
612
- # Computed attributes (not real columns or associations).
613
- def can_edit; end
614
- def can_view; end
615
- def drafts; end
616
-
617
- attributes :name, :age, {:can_edit => :boolean}, :can_view
618
- has_many :posts, :serializer => Class.new
619
- has_many :drafts, :serializer => Class.new
620
- has_one :parent, :serializer => Class.new
621
- end
622
-
623
- assert_equal serializer.schema, {
624
- :attributes => { :name => :string, :age => :integer, :can_edit => :boolean, :can_view => nil },
625
- :associations => {
626
- :posts => { :has_many => :posts },
627
- :drafts => nil,
628
- :parent => { :belongs_to => :parent }
629
- }
630
- }
631
- end
632
-
633
- def test_schema_with_as
634
- model = setup_model
635
-
636
- serializer = Class.new(ActiveModel::Serializer) do
637
- class << self; self; end.class_eval do
638
- define_method(:model_class) do model end
639
- end
640
-
641
- attributes :name, :age
642
- has_many :posts, :key => :my_posts, :serializer => Class.new
643
- has_one :parent, :key => :my_parent, :serializer => Class.new
644
- end
645
-
646
- assert_equal serializer.schema, {
647
- :attributes => { :name => :string, :age => :integer },
648
- :associations => {
649
- :my_posts => { :has_many => :posts },
650
- :my_parent => { :belongs_to => :parent }
651
- }
652
- }
653
- end
654
-
655
- def test_embed_id_for_has_one
656
- author_serializer = Class.new(ActiveModel::Serializer)
657
-
658
- serializer_class = Class.new(ActiveModel::Serializer) do
659
- embed :ids
660
- root :post
661
-
662
- attributes :title, :body
663
- has_one :author, :serializer => author_serializer
664
- end
665
-
666
- post_class = Class.new(Model) do
667
- attr_accessor :author
668
- end
669
-
670
- author_class = Class.new(Model)
671
-
672
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
673
- author = author_class.new(:id => 5)
674
- post.author = author
675
-
676
- hash = serializer_class.new(post)
677
-
678
- assert_equal({
679
- :post => {
680
- :title => "New Post",
681
- :body => "It's a new post!",
682
- :author_id => 5
683
- }
684
- }, hash.as_json)
685
- end
686
-
687
- def test_embed_objects_for_has_one
688
- author_serializer = Class.new(ActiveModel::Serializer) do
689
- attributes :id, :name
690
- end
691
-
692
- serializer_class = Class.new(ActiveModel::Serializer) do
693
- root :post
694
-
695
- attributes :title, :body
696
- has_one :author, :serializer => author_serializer
697
- end
698
-
699
- post_class = Class.new(Model) do
700
- attr_accessor :author
701
- end
702
-
703
- author_class = Class.new(Model)
704
-
705
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
706
- author = author_class.new(:id => 5, :name => "Tom Dale")
707
- post.author = author
708
-
709
- hash = serializer_class.new(post)
710
-
711
- assert_equal({
712
- :post => {
713
- :title => "New Post",
714
- :body => "It's a new post!",
715
- :author => { :id => 5, :name => "Tom Dale" }
716
- }
717
- }, hash.as_json)
718
- end
719
-
720
- def test_root_provided_in_options
721
- author_serializer = Class.new(ActiveModel::Serializer) do
722
- attributes :id, :name
723
- end
724
-
725
- serializer_class = Class.new(ActiveModel::Serializer) do
726
- root :post
727
-
728
- attributes :title, :body
729
- has_one :author, :serializer => author_serializer
730
- end
731
-
732
- post_class = Class.new(Model) do
733
- attr_accessor :author
734
- end
735
-
736
- author_class = Class.new(Model)
737
-
738
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
739
- author = author_class.new(:id => 5, :name => "Tom Dale")
740
- post.author = author
741
-
742
- assert_equal({
743
- :blog_post => {
744
- :title => "New Post",
745
- :body => "It's a new post!",
746
- :author => { :id => 5, :name => "Tom Dale" }
747
- }
748
- }, serializer_class.new(post, :root => :blog_post).as_json)
749
-
750
- assert_equal({
751
- :title => "New Post",
752
- :body => "It's a new post!",
753
- :author => { :id => 5, :name => "Tom Dale" }
754
- }, serializer_class.new(post, :root => false).as_json)
755
-
756
- assert_equal({
757
- :blog_post => {
758
- :title => "New Post",
759
- :body => "It's a new post!",
760
- :author => { :id => 5, :name => "Tom Dale" }
761
- }
762
- }, serializer_class.new(post).as_json(:root => :blog_post))
763
-
764
- assert_equal({
765
- :title => "New Post",
766
- :body => "It's a new post!",
767
- :author => { :id => 5, :name => "Tom Dale" }
768
- }, serializer_class.new(post).as_json(:root => false))
769
- end
770
-
771
- def test_serializer_has_access_to_root_object
772
- hash_object = nil
773
-
774
- author_serializer = Class.new(ActiveModel::Serializer) do
775
- attributes :id, :name
776
-
777
- define_method :serializable_hash do
778
- hash_object = @options[:hash]
779
- super()
780
- end
781
- end
782
-
783
- serializer_class = Class.new(ActiveModel::Serializer) do
784
- root :post
785
-
786
- attributes :title, :body
787
- has_one :author, :serializer => author_serializer
788
- end
789
-
790
- post_class = Class.new(Model) do
791
- attr_accessor :author
792
- end
793
-
794
- author_class = Class.new(Model)
795
-
796
- post = post_class.new(:title => "New Post", :body => "It's a new post!")
797
- author = author_class.new(:id => 5, :name => "Tom Dale")
798
- post.author = author
799
-
800
- expected = serializer_class.new(post).as_json
801
- assert_equal expected, hash_object
802
- end
803
-
804
- def test_embed_ids_include_true_with_root
805
- serializer_class = post_serializer
806
-
807
- serializer_class.class_eval do
808
- root :post
809
- embed :ids, :include => true
810
- has_many :comments, :key => :comment_ids, :root => :comments
811
- has_one :author, :serializer => DefaultUserSerializer, :key => :author_id, :root => :author
812
- end
813
-
814
- post = Post.new(:title => "New Post", :body => "Body of new post", :email => "tenderlove@tenderlove.com")
815
- comments = [Comment.new(:title => "Comment1", :id => 1), Comment.new(:title => "Comment2", :id => 2)]
816
- post.comments = comments
817
-
818
- serializer = serializer_class.new(post)
819
-
820
- assert_equal({
821
- :post => {
822
- :title => "New Post",
823
- :body => "Body of new post",
824
- :comment_ids => [1, 2],
825
- :author_id => nil
826
- },
827
- :comments => [
828
- { :title => "Comment1" },
829
- { :title => "Comment2" }
830
- ],
831
- :author => []
832
- }, serializer.as_json)
833
-
834
- post.author = User.new(:id => 1)
835
-
836
- serializer = serializer_class.new(post)
837
-
838
- assert_equal({
839
- :post => {
840
- :title => "New Post",
841
- :body => "Body of new post",
842
- :comment_ids => [1, 2],
843
- :author_id => 1
844
- },
845
- :comments => [
846
- { :title => "Comment1" },
847
- { :title => "Comment2" }
848
- ],
849
- :author => [{ :first_name => "Jose", :last_name => "Valim" }]
850
- }, serializer.as_json)
851
- end
852
-
853
- # the point of this test is to illustrate that deeply nested serializers
854
- # still side-load at the root.
855
- def test_embed_with_include_inserts_at_root
856
- tag_serializer = Class.new(ActiveModel::Serializer) do
857
- attributes :id, :name
858
- end
859
-
860
- comment_serializer = Class.new(ActiveModel::Serializer) do
861
- embed :ids, :include => true
862
- attributes :id, :body
863
- has_many :tags, :serializer => tag_serializer
864
- end
865
-
866
- post_serializer = Class.new(ActiveModel::Serializer) do
867
- embed :ids, :include => true
868
- attributes :id, :title, :body
869
- has_many :comments, :serializer => comment_serializer
870
- end
871
-
872
- post_class = Class.new(Model) do
873
- attr_accessor :comments
874
-
875
- define_method :active_model_serializer do
876
- post_serializer
877
- end
878
- end
879
-
880
- comment_class = Class.new(Model) do
881
- attr_accessor :tags
882
- end
883
-
884
- tag_class = Class.new(Model)
885
-
886
- post = post_class.new(:title => "New Post", :body => "NEW POST", :id => 1)
887
- comment1 = comment_class.new(:body => "EWOT", :id => 1)
888
- comment2 = comment_class.new(:body => "YARLY", :id => 2)
889
- tag1 = tag_class.new(:name => "lolcat", :id => 1)
890
- tag2 = tag_class.new(:name => "nyancat", :id => 2)
891
- tag3 = tag_class.new(:name => "violetcat", :id => 3)
892
-
893
- post.comments = [comment1, comment2]
894
- comment1.tags = [tag1, tag3]
895
- comment2.tags = [tag1, tag2]
896
-
897
- actual = ActiveModel::ArraySerializer.new([post], :root => :posts).as_json
898
- assert_equal({
899
- :posts => [
900
- { :title => "New Post", :body => "NEW POST", :id => 1, :comment_ids => [1,2] }
901
- ],
902
-
903
- :comments => [
904
- { :body => "EWOT", :id => 1, :tag_ids => [1,3] },
905
- { :body => "YARLY", :id => 2, :tag_ids => [1,2] }
906
- ],
907
-
908
- :tags => [
909
- { :name => "lolcat", :id => 1 },
910
- { :name => "violetcat", :id => 3 },
911
- { :name => "nyancat", :id => 2 }
912
- ]
913
- }, actual)
914
- end
915
-
916
- def test_can_customize_attributes
917
- serializer = Class.new(ActiveModel::Serializer) do
918
- attributes :title, :body
919
-
920
- def title
921
- object.title.upcase
922
- end
923
- end
924
-
925
- klass = Class.new do
926
- def read_attribute_for_serialization(name)
927
- { :title => "New post!", :body => "First post body" }[name]
928
- end
929
-
930
- def title
931
- read_attribute_for_serialization(:title)
932
- end
933
-
934
- def body
935
- read_attribute_for_serialization(:body)
936
- end
937
- end
938
-
939
- object = klass.new
940
-
941
- actual = serializer.new(object, :root => :post).as_json
942
-
943
- assert_equal({
944
- :post => {
945
- :title => "NEW POST!",
946
- :body => "First post body"
947
- }
948
- }, actual)
949
- end
950
-
951
- def test_can_customize_attributes_with_read_attributes
952
- serializer = Class.new(ActiveModel::Serializer) do
953
- attributes :title, :body
954
-
955
- def read_attribute_for_serialization(name)
956
- { :title => "New post!", :body => "First post body" }[name]
957
- end
958
- end
959
-
960
- actual = serializer.new(Object.new, :root => :post).as_json
961
-
962
- assert_equal({
963
- :post => {
964
- :title => "New post!",
965
- :body => "First post body"
966
- }
967
- }, actual)
968
- end
969
-
970
- def test_active_support_on_load_hooks_fired
971
- loaded = nil
972
- ActiveSupport.on_load(:active_model_serializers) do
973
- loaded = self
974
- end
975
- assert_equal ActiveModel::Serializer, loaded
976
- end
977
-
978
- def tests_query_attributes_strip_question_mark
979
- todo = Class.new do
980
- def overdue?
981
- true
982
- end
983
-
984
- def read_attribute_for_serialization(name)
985
- send name
986
- end
987
- end
988
-
989
- serializer = Class.new(ActiveModel::Serializer) do
990
- attribute :overdue?
991
- end
992
-
993
- actual = serializer.new(todo.new).as_json
994
-
995
- assert_equal({
996
- :overdue => true
997
- }, actual)
998
- end
999
-
1000
- def tests_query_attributes_allow_key_option
1001
- todo = Class.new do
1002
- def overdue?
1003
- true
1004
- end
1005
-
1006
- def read_attribute_for_serialization(name)
1007
- send name
1008
- end
1009
- end
1010
-
1011
- serializer = Class.new(ActiveModel::Serializer) do
1012
- attribute :overdue?, :key => :foo
1013
- end
1014
-
1015
- actual = serializer.new(todo.new).as_json
1016
-
1017
- assert_equal({
1018
- :foo => true
1019
- }, actual)
1020
- end
1021
-
1022
- def tests_can_handle_polymorphism
1023
- email_serializer = Class.new(ActiveModel::Serializer) do
1024
- attributes :subject, :body
1025
- end
1026
-
1027
- email_class = Class.new(Model) do
1028
- def self.to_s
1029
- "Email"
1030
- end
1031
-
1032
- define_method :active_model_serializer do
1033
- email_serializer
1034
- end
1035
- end
1036
-
1037
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1038
- attributes :name, :url
1039
- has_one :attachable, :polymorphic => true
1040
- end
1041
-
1042
- email = email_class.new :subject => 'foo', :body => 'bar'
1043
-
1044
- attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email
1045
-
1046
- actual = attachment_serializer.new(attachment, {}).as_json
1047
-
1048
- assert_equal({
1049
- :name => 'logo.png',
1050
- :url => 'http://example.com/logo.png',
1051
- :attachable => {
1052
- :type => :email,
1053
- :email => { :subject => 'foo', :body => 'bar' }
1054
- }
1055
- }, actual)
1056
- end
1057
-
1058
- def test_can_handle_polymoprhic_ids
1059
- email_serializer = Class.new(ActiveModel::Serializer) do
1060
- attributes :subject, :body
1061
- end
1062
-
1063
- email_class = Class.new(Model) do
1064
- def self.to_s
1065
- "Email"
1066
- end
1067
-
1068
- define_method :active_model_serializer do
1069
- email_serializer
1070
- end
1071
- end
1072
-
1073
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1074
- embed :ids
1075
- attributes :name, :url
1076
- has_one :attachable, :polymorphic => true
1077
- end
1078
-
1079
- email = email_class.new :id => 1
1080
-
1081
- attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email
1082
-
1083
- actual = attachment_serializer.new(attachment, {}).as_json
1084
-
1085
- assert_equal({
1086
- :name => 'logo.png',
1087
- :url => 'http://example.com/logo.png',
1088
- :attachable => {
1089
- :type => :email,
1090
- :id => 1
1091
- }
1092
- }, actual)
1093
- end
1094
-
1095
- def test_polymorphic_associations_are_included_at_root
1096
- email_serializer = Class.new(ActiveModel::Serializer) do
1097
- attributes :subject, :body, :id
1098
- end
1099
-
1100
- email_class = Class.new(Model) do
1101
- def self.to_s
1102
- "Email"
1103
- end
1104
-
1105
- define_method :active_model_serializer do
1106
- email_serializer
1107
- end
1108
- end
1109
-
1110
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1111
- root :attachment
1112
- embed :ids, :include => true
1113
- attributes :name, :url
1114
- has_one :attachable, :polymorphic => true
1115
- end
1116
-
1117
- email = email_class.new :id => 1, :subject => "Hello", :body => "World"
1118
-
1119
- attachment = Attachment.new :name => 'logo.png', :url => 'http://example.com/logo.png', :attachable => email
1120
-
1121
- actual = attachment_serializer.new(attachment, {}).as_json
1122
-
1123
- assert_equal({
1124
- :attachment => {
1125
- :name => 'logo.png',
1126
- :url => 'http://example.com/logo.png',
1127
- :attachable => {
1128
- :type => :email,
1129
- :id => 1
1130
- }},
1131
- :emails => [{
1132
- :id => 1,
1133
- :subject => "Hello",
1134
- :body => "World"
1135
- }]
1136
- }, actual)
1137
- end
1138
-
1139
- def test_multiple_polymorphic_associations
1140
- email_serializer = Class.new(ActiveModel::Serializer) do
1141
- attributes :subject, :body, :id
1142
- end
1143
-
1144
- orange_serializer = Class.new(ActiveModel::Serializer) do
1145
- embed :ids, :include => true
1146
-
1147
- attributes :plu, :id
1148
- has_one :readable, :polymorphic => true
1149
- end
1150
-
1151
- email_class = Class.new(Model) do
1152
- def self.to_s
1153
- "Email"
1154
- end
1155
-
1156
- define_method :active_model_serializer do
1157
- email_serializer
1158
- end
1159
- end
1160
-
1161
- orange_class = Class.new(Model) do
1162
- def self.to_s
1163
- "Orange"
1164
- end
1165
-
1166
- def readable
1167
- @attributes[:readable]
1168
- end
1169
-
1170
- define_method :active_model_serializer do
1171
- orange_serializer
1172
- end
1173
- end
1174
-
1175
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1176
- root :attachment
1177
- embed :ids, :include => true
1178
-
1179
- attributes :name, :url
1180
-
1181
- has_one :attachable, :polymorphic => true
1182
- has_one :readable, :polymorphic => true
1183
- has_one :edible, :polymorphic => true
1184
- end
1185
-
1186
- email = email_class.new :id => 1, :subject => "Hello", :body => "World"
1187
- orange = orange_class.new :id => 1, :plu => "3027", :readable => email
1188
-
1189
- attachment = Attachment.new({
1190
- :name => 'logo.png',
1191
- :url => 'http://example.com/logo.png',
1192
- :attachable => email,
1193
- :readable => email,
1194
- :edible => orange
1195
- })
1196
-
1197
- actual = attachment_serializer.new(attachment, {}).as_json
1198
-
1199
- assert_equal({
1200
- :emails => [{
1201
- :subject => "Hello",
1202
- :body => "World",
1203
- :id => 1
1204
- }],
1205
-
1206
- :oranges => [{
1207
- :plu => "3027",
1208
- :id => 1,
1209
- :readable => { :type => :email, :id => 1 }
1210
- }],
1211
-
1212
- :attachment => {
1213
- :name => 'logo.png',
1214
- :url => 'http://example.com/logo.png',
1215
- :attachable => { :type => :email, :id => 1 },
1216
- :readable => { :type => :email, :id => 1 },
1217
- :edible => { :type => :orange, :id => 1 }
1218
- }
1219
- }, actual)
1220
- end
1221
-
1222
- def test_raises_an_error_when_a_child_serializer_includes_associations_when_the_source_doesnt
1223
- attachment_serializer = Class.new(ActiveModel::Serializer) do
1224
- attributes :name
1225
- end
1226
-
1227
- fruit_serializer = Class.new(ActiveModel::Serializer) do
1228
- embed :ids, :include => true
1229
- has_one :attachment, :serializer => attachment_serializer
1230
- attribute :color
1231
- end
1232
-
1233
- banana_class = Class.new Model do
1234
- def self.to_s
1235
- 'banana'
1236
- end
1237
-
1238
- def attachment
1239
- @attributes[:attachment]
1240
- end
1241
-
1242
- define_method :active_model_serializer do
1243
- fruit_serializer
1244
- end
1245
- end
1246
-
1247
- strawberry_class = Class.new Model do
1248
- def self.to_s
1249
- 'strawberry'
1250
- end
1251
-
1252
- def attachment
1253
- @attributes[:attachment]
1254
- end
1255
-
1256
- define_method :active_model_serializer do
1257
- fruit_serializer
1258
- end
1259
- end
1260
-
1261
- smoothie = Class.new do
1262
- attr_reader :base, :flavor
1263
-
1264
- def initialize(base, flavor)
1265
- @base, @flavor = base, flavor
1266
- end
1267
- end
1268
-
1269
- smoothie_serializer = Class.new(ActiveModel::Serializer) do
1270
- root false
1271
- embed :ids, :include => true
1272
-
1273
- has_one :base, :polymorphic => true
1274
- has_one :flavor, :polymorphic => true
1275
- end
1276
-
1277
- banana_attachment = Attachment.new({
1278
- :name => 'banana_blending.md',
1279
- :id => 3,
1280
- })
1281
-
1282
- strawberry_attachment = Attachment.new({
1283
- :name => 'strawberry_cleaning.doc',
1284
- :id => 4
1285
- })
1286
-
1287
- banana = banana_class.new :color => "yellow", :id => 1, :attachment => banana_attachment
1288
- strawberry = strawberry_class.new :color => "red", :id => 2, :attachment => strawberry_attachment
1289
-
1290
- smoothie = smoothie_serializer.new(smoothie.new(banana, strawberry))
1291
-
1292
- assert_raise ActiveModel::Serializer::IncludeError do
1293
- smoothie.as_json
1294
- end
1295
- end
1296
-
1297
- def tests_includes_does_not_include_nil_polymoprhic_associations
1298
- post_serializer = Class.new(ActiveModel::Serializer) do
1299
- root :post
1300
- embed :ids, :include => true
1301
- has_one :author, :polymorphic => true
1302
- attributes :title
1303
- end
1304
-
1305
- post = Post.new(:title => 'Foo')
1306
-
1307
- actual = post_serializer.new(post).as_json
1308
-
1309
- assert_equal({
1310
- :post => {
1311
- :title => 'Foo',
1312
- :author => nil
1313
- }
1314
- }, actual)
1315
- end
1316
-
1317
- def test_meta_key_serialization
1318
- tag_serializer = Class.new(ActiveModel::Serializer) do
1319
- attributes :name
1320
- end
1321
-
1322
- tag_class = Class.new(Model) do
1323
- def name
1324
- @attributes[:name]
1325
- end
1326
-
1327
- define_method :active_model_serializer do
1328
- tag_serializer
1329
- end
1330
- end
1331
-
1332
- serializable_array = Class.new(Array)
1333
-
1334
- array = serializable_array.new
1335
- array << tag_class.new(:name => 'Rails')
1336
- array << tag_class.new(:name => 'Sinatra')
1337
-
1338
- actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}).as_json
1339
-
1340
- assert_equal({
1341
- :meta => {
1342
- :total => 10,
1343
- },
1344
- :tags => [
1345
- { :name => "Rails" },
1346
- { :name => "Sinatra" },
1347
- ]
1348
- }, actual)
1349
-
1350
- actual = array.active_model_serializer.new(array, :root => :tags, :meta => {:total => 10}, :meta_key => 'meta_object').as_json
1351
-
1352
- assert_equal({
1353
- :meta_object => {
1354
- :total => 10,
1355
- },
1356
- :tags => [
1357
- { :name => "Rails" },
1358
- { :name => "Sinatra" },
1359
- ]
1360
- }, actual)
1361
- end
1362
-
1363
- def test_inheritance_does_not_used_cached_attributes
1364
- parent = Class.new(ActiveModel::Serializer) do
1365
- attributes :title
1366
- end
1367
-
1368
- child = Class.new(parent) do
1369
- attributes :body
1370
- end
1371
-
1372
- data_class = Class.new do
1373
- attr_accessor :title, :body
1374
- end
1375
-
1376
- item = data_class.new
1377
- item.title = "title"
1378
- item.body = "body"
1379
-
1380
- 2.times do
1381
- assert_equal({:title => "title"},
1382
- parent.new(item).attributes)
1383
- assert_equal({:body => "body", :title => "title"},
1384
- child.new(item).attributes)
1385
- end
1386
-
1387
- end
1388
-
1389
- def test_scope_name_method
1390
- serializer = Class.new(ActiveModel::Serializer) do
1391
- def has_permission?
1392
- current_user.super_user?
1393
- end
1394
- end
1395
-
1396
- user = User.new
1397
- user.superuser = true
1398
- post = Post.new(:title => 'Foo')
1399
-
1400
- a_serializer = serializer.new(post, :scope => user, :scope_name => :current_user)
1401
- assert a_serializer.has_permission?
1402
- end
1403
-
1404
- def test_only_option_filters_attributes_and_associations
1405
- post = Post.new(:title => "New Post", :body => "Body of new post")
1406
- comments = [Comment.new(:title => "Comment1")]
1407
- post.comments = comments
1408
-
1409
- post_serializer = PostSerializer.new(post, :only => :title)
1410
-
1411
- assert_equal({
1412
- :post => {
1413
- :title => "New Post"
1414
- }
1415
- }, post_serializer.as_json)
1416
- end
1417
-
1418
- def test_except_option_filters_attributes_and_associations
1419
- post = Post.new(:title => "New Post", :body => "Body of new post")
1420
- comments = [Comment.new(:title => "Comment1")]
1421
- post.comments = comments
1422
-
1423
- post_serializer = PostSerializer.new(post, :except => [:body, :comments])
1424
-
1425
- assert_equal({
1426
- :post => {
1427
- :title => "New Post"
1428
- }
1429
- }, post_serializer.as_json)
1430
- end
1431
-
1432
- def test_only_option_takes_precedence_over_custom_defined_include_methods
1433
- user = User.new
1434
-
1435
- post = Post.new(:title => "New Post", :body => "Body of new post", :author => "Sausage King")
1436
- comments = [Comment.new(:title => "Comment")]
1437
- post.comments = comments
1438
-
1439
- post_serializer = PostWithMultipleConditionalsSerializer.new(post, :scope => user, :only => :title)
1440
-
1441
- # comments enabled
1442
- post.comments_disabled = false
1443
- # superuser - should see author
1444
- user.superuser = true
1445
-
1446
- assert_equal({
1447
- :post => {
1448
- :title => "New Post"
1449
- }
1450
- }, post_serializer.as_json)
1451
- end
1452
-
1453
- def test_as_json_with_nil_options
1454
- user = User.new
1455
- user_serializer = DefaultUserSerializer.new(user, {})
1456
-
1457
- # ActiveSupport 3.1 Object#to_json generates this downstream call
1458
- assert_equal({
1459
- :default_user => {
1460
- :first_name => "Jose",
1461
- :last_name => "Valim"
1462
- }
1463
- }, user_serializer.as_json(nil))
1464
- end
1465
- end