active_model_serializers 0.9.12 → 0.10.15

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.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +638 -82
  3. data/MIT-LICENSE +3 -2
  4. data/README.md +194 -846
  5. data/lib/action_controller/serialization.rb +34 -74
  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 +53 -38
  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 +1 -1
  31. data/lib/active_model/serializer.rb +361 -263
  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.rb +132 -0
  53. data/lib/active_model_serializers/railtie.rb +62 -0
  54. data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
  55. data/lib/active_model_serializers/serializable_resource.rb +84 -0
  56. data/lib/active_model_serializers/serialization_context.rb +41 -0
  57. data/lib/active_model_serializers/test/schema.rb +140 -0
  58. data/lib/active_model_serializers/test/serializer.rb +127 -0
  59. data/lib/active_model_serializers/test.rb +9 -0
  60. data/lib/active_model_serializers.rb +58 -27
  61. data/lib/generators/rails/USAGE +6 -0
  62. data/lib/{active_model/serializer/generators → generators/rails}/resource_override.rb +1 -4
  63. data/lib/{active_model/serializer/generators/serializer → generators/rails}/serializer_generator.rb +4 -5
  64. data/lib/grape/active_model_serializers.rb +18 -0
  65. data/lib/grape/formatters/active_model_serializers.rb +34 -0
  66. data/lib/grape/helpers/active_model_serializers.rb +19 -0
  67. data/lib/tasks/rubocop.rake +60 -0
  68. metadata +248 -155
  69. data/CONTRIBUTING.md +0 -20
  70. data/DESIGN.textile +0 -586
  71. data/lib/action_controller/serialization_test_case.rb +0 -82
  72. data/lib/active_model/array_serializer.rb +0 -70
  73. data/lib/active_model/default_serializer.rb +0 -30
  74. data/lib/active_model/serializable/utils.rb +0 -18
  75. data/lib/active_model/serializable.rb +0 -61
  76. data/lib/active_model/serializer/association/has_many.rb +0 -41
  77. data/lib/active_model/serializer/association/has_one.rb +0 -27
  78. data/lib/active_model/serializer/config.rb +0 -33
  79. data/lib/active_model/serializer/generators/serializer/USAGE +0 -9
  80. data/lib/active_model/serializer/generators/serializer/scaffold_controller_generator.rb +0 -16
  81. data/lib/active_model/serializer/generators/serializer/templates/controller.rb +0 -93
  82. data/lib/active_model/serializer/railtie.rb +0 -24
  83. data/lib/active_model/serializer_support.rb +0 -7
  84. data/test/benchmark/app.rb +0 -60
  85. data/test/benchmark/benchmarking_support.rb +0 -67
  86. data/test/benchmark/bm_active_record.rb +0 -41
  87. data/test/benchmark/setup.rb +0 -75
  88. data/test/benchmark/tmp/miniprofiler/mp_timers_6eqewtfgrhitvq5gqm25 +0 -0
  89. data/test/benchmark/tmp/miniprofiler/mp_timers_8083sx03hu72pxz1a4d0 +0 -0
  90. data/test/benchmark/tmp/miniprofiler/mp_timers_fyz2gsml4z0ph9kpoy1c +0 -0
  91. data/test/benchmark/tmp/miniprofiler/mp_timers_hjry5rc32imd42oxoi48 +0 -0
  92. data/test/benchmark/tmp/miniprofiler/mp_timers_m8fpoz2cvt3g9agz0bs3 +0 -0
  93. data/test/benchmark/tmp/miniprofiler/mp_timers_p92m2drnj1i568u3sta0 +0 -0
  94. data/test/benchmark/tmp/miniprofiler/mp_timers_qg52tpca3uesdfguee9i +0 -0
  95. data/test/benchmark/tmp/miniprofiler/mp_timers_s15t1a6mvxe0z7vjv790 +0 -0
  96. data/test/benchmark/tmp/miniprofiler/mp_timers_x8kal3d17nfds6vp4kcj +0 -0
  97. data/test/benchmark/tmp/miniprofiler/mp_views_127.0.0.1 +0 -0
  98. data/test/fixtures/active_record.rb +0 -96
  99. data/test/fixtures/poro.rb +0 -255
  100. data/test/fixtures/template.html.erb +0 -1
  101. data/test/integration/action_controller/namespaced_serialization_test.rb +0 -105
  102. data/test/integration/action_controller/serialization_test.rb +0 -287
  103. data/test/integration/action_controller/serialization_test_case_test.rb +0 -71
  104. data/test/integration/active_record/active_record_test.rb +0 -94
  105. data/test/integration/generators/resource_generator_test.rb +0 -26
  106. data/test/integration/generators/scaffold_controller_generator_test.rb +0 -64
  107. data/test/integration/generators/serializer_generator_test.rb +0 -41
  108. data/test/test_app.rb +0 -18
  109. data/test/test_helper.rb +0 -31
  110. data/test/tmp/app/assets/javascripts/accounts.js +0 -2
  111. data/test/tmp/app/assets/stylesheets/accounts.css +0 -4
  112. data/test/tmp/app/controllers/accounts_controller.rb +0 -3
  113. data/test/tmp/app/helpers/accounts_helper.rb +0 -3
  114. data/test/tmp/app/serializers/account_serializer.rb +0 -4
  115. data/test/tmp/config/routes.rb +0 -2
  116. data/test/unit/active_model/array_serializer/except_test.rb +0 -18
  117. data/test/unit/active_model/array_serializer/key_format_test.rb +0 -18
  118. data/test/unit/active_model/array_serializer/meta_test.rb +0 -53
  119. data/test/unit/active_model/array_serializer/only_test.rb +0 -18
  120. data/test/unit/active_model/array_serializer/options_test.rb +0 -16
  121. data/test/unit/active_model/array_serializer/root_test.rb +0 -102
  122. data/test/unit/active_model/array_serializer/scope_test.rb +0 -24
  123. data/test/unit/active_model/array_serializer/serialization_test.rb +0 -239
  124. data/test/unit/active_model/default_serializer_test.rb +0 -13
  125. data/test/unit/active_model/serializer/associations/build_serializer_test.rb +0 -36
  126. data/test/unit/active_model/serializer/associations_test.rb +0 -49
  127. data/test/unit/active_model/serializer/attributes_test.rb +0 -57
  128. data/test/unit/active_model/serializer/config_test.rb +0 -91
  129. data/test/unit/active_model/serializer/filter_test.rb +0 -69
  130. data/test/unit/active_model/serializer/has_many_polymorphic_test.rb +0 -189
  131. data/test/unit/active_model/serializer/has_many_test.rb +0 -265
  132. data/test/unit/active_model/serializer/has_one_and_has_many_test.rb +0 -27
  133. data/test/unit/active_model/serializer/has_one_polymorphic_test.rb +0 -196
  134. data/test/unit/active_model/serializer/has_one_test.rb +0 -253
  135. data/test/unit/active_model/serializer/key_format_test.rb +0 -25
  136. data/test/unit/active_model/serializer/meta_test.rb +0 -39
  137. data/test/unit/active_model/serializer/options_test.rb +0 -42
  138. data/test/unit/active_model/serializer/root_test.rb +0 -117
  139. data/test/unit/active_model/serializer/scope_test.rb +0 -49
  140. data/test/unit/active_model/serializer/url_helpers_test.rb +0 -36
  141. data/test/unit/active_model/serilizable_test.rb +0 -50
  142. /data/lib/{active_model/serializer/generators/serializer/templates/serializer.rb → generators/rails/templates/serializer.rb.erb} +0 -0
@@ -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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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_includes @response.content_type, 'application/json'
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
@@ -1,71 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActionController
4
- module SerializationsAssertions
5
- class RenderSerializerTest < ActionController::TestCase
6
- class MyController < ActionController::Base
7
- def render_using_serializer
8
- render json: Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
9
- end
10
-
11
- def render_text
12
- render plain: 'ok'
13
- end
14
-
15
- def render_template
16
- prepend_view_path "./test/fixtures"
17
- render template: "template"
18
- end
19
- end
20
-
21
- tests MyController
22
-
23
- def test_supports_specifying_serializers_with_a_serializer_class
24
- get :render_using_serializer
25
- assert_serializer ProfileSerializer
26
- end
27
-
28
- def test_supports_specifying_serializers_with_a_regexp
29
- get :render_using_serializer
30
- assert_serializer %r{\AProfile.+\Z}
31
- end
32
-
33
- def test_supports_specifying_serializers_with_a_string
34
- get :render_using_serializer
35
- assert_serializer 'ProfileSerializer'
36
- end
37
-
38
- def test_supports_specifying_serializers_with_a_symbol
39
- get :render_using_serializer
40
- assert_serializer :profile_serializer
41
- end
42
-
43
- def test_supports_specifying_serializers_with_a_nil
44
- get :render_text
45
- assert_serializer nil
46
- end
47
-
48
- def test_raises_descriptive_error_message_when_serializer_was_not_rendered
49
- get :render_using_serializer
50
- e = assert_raise ActiveSupport::TestCase::Assertion do
51
- assert_serializer 'PostSerializer'
52
- end
53
- assert_match 'expecting <"PostSerializer"> but rendering with <["ProfileSerializer"]>', e.message
54
- end
55
-
56
-
57
- def test_raises_argument_error_when_asserting_with_invalid_object
58
- get :render_using_serializer
59
- e = assert_raise ArgumentError do
60
- assert_serializer Hash
61
- end
62
- assert_match 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil', e.message
63
- end
64
-
65
- def test_does_not_overwrite_notification_subscriptions
66
- get :render_template
67
- assert_template "template"
68
- end
69
- end
70
- end
71
- end
@@ -1,94 +0,0 @@
1
- require 'test_helper'
2
- require 'fixtures/active_record'
3
-
4
- module ActiveModel
5
- class Serializer
6
- class ActiveRecordTest < Minitest::Test
7
- def setup
8
- @post = ARPost.first
9
- end
10
-
11
- def test_serialization_embedding_objects
12
- post_serializer = ARPostSerializer.new(@post)
13
-
14
- assert_equal({
15
- 'ar_post' => {
16
- title: 'New post', body: 'A body!!!',
17
- ar_comments: [{ body: 'what a dumb post', ar_tags: [{ name: 'happy' }, { name: 'whiny' }] },
18
- { body: 'i liked it', ar_tags: [{:name=>"happy"}, {:name=>"short"}] }],
19
- ar_tags: [{ name: 'short' }, { name: 'whiny' }],
20
- ar_section: { 'name' => 'ruby' }
21
- }
22
- }, post_serializer.as_json)
23
- end
24
-
25
- def test_serialization_embedding_ids
26
- post_serializer = ARPostSerializer.new(@post)
27
-
28
- embed(ARPostSerializer, embed: :ids) do
29
- assert_equal({
30
- 'ar_post' => {
31
- title: 'New post', body: 'A body!!!',
32
- 'ar_comment_ids' => [1, 2],
33
- 'ar_tag_ids' => [1, 2],
34
- 'ar_section_id' => 1
35
- }
36
- }, post_serializer.as_json)
37
- end
38
- end
39
-
40
- def test_serialization_embedding_ids_including_in_root
41
- post_serializer = ARPostSerializer.new(@post)
42
-
43
- embed(ARPostSerializer, embed: :ids, embed_in_root: true) do
44
- embed(ARCommentSerializer, embed: :ids, embed_in_root: true) do
45
- assert_equal({
46
- 'ar_post' => {
47
- title: 'New post', body: 'A body!!!',
48
- 'ar_comment_ids' => [1, 2],
49
- 'ar_tag_ids' => [1, 2],
50
- 'ar_section_id' => 1
51
- },
52
- 'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
53
- { body: 'i liked it', 'ar_tag_ids' => [3, 1] }],
54
- 'ar_tags' => [{ name: 'happy' }, { name: 'whiny' }, { name: 'short' }],
55
- 'ar_sections' => [{ 'name' => 'ruby' }]
56
- }, post_serializer.as_json)
57
- end
58
- end
59
- end
60
-
61
- def test_serialization_embedding_ids_in_common_root_key
62
- post_serializer = AREmbeddedSerializer.new(@post)
63
-
64
- embed(AREmbeddedSerializer, embed: :ids, embed_in_root: true, embed_in_root_key: :linked) do
65
- embed(ARCommentSerializer, embed: :ids, embed_in_root: true, embed_in_root_key: :linked) do
66
- assert_equal({
67
- 'ar_tags' => [{ name: 'short' },
68
- { name: 'whiny' },
69
- { name: 'happy' }],
70
- 'ar_comments' => [{ body: 'what a dumb post', 'ar_tag_ids' => [3, 2] },
71
- { body: 'i liked it', 'ar_tag_ids' => [3, 1] }]
72
- }, post_serializer.as_json[:linked])
73
- end
74
- end
75
- end
76
-
77
- private
78
-
79
- def embed(serializer_class, options = {})
80
- old_assocs = Hash[serializer_class._associations.to_a.map { |(name, association)| [name, association.dup] }]
81
-
82
- serializer_class._associations.each_value do |association|
83
- association.embed = options[:embed]
84
- association.embed_in_root = options[:embed_in_root]
85
- association.embed_in_root_key = options[:embed_in_root_key]
86
- end
87
-
88
- yield
89
- ensure
90
- serializer_class._associations = old_assocs
91
- end
92
- end
93
- end
94
- end
@@ -1,26 +0,0 @@
1
- require 'test_helper'
2
- require 'rails'
3
- require 'active_model/serializer/railtie'
4
- require 'test_app'
5
-
6
- class ResourceGeneratorTest < Rails::Generators::TestCase
7
- destination File.expand_path('../../../tmp', __FILE__)
8
- setup :prepare_destination, :copy_routes
9
-
10
- tests Rails::Generators::ResourceGenerator
11
- arguments %w(account)
12
-
13
- def test_serializer_file_is_generated
14
- run_generator
15
-
16
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
17
- end
18
-
19
- private
20
-
21
- def copy_routes
22
- config_dir = File.join(destination_root, 'config')
23
- FileUtils.mkdir_p(config_dir)
24
- File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw { }')
25
- end
26
- end
@@ -1,64 +0,0 @@
1
- require 'test_helper'
2
- require 'rails'
3
- require 'active_model/serializer/railtie'
4
- require 'test_app'
5
-
6
- class ScaffoldControllerGeneratorTest < Rails::Generators::TestCase
7
- destination File.expand_path('../../../tmp', __FILE__)
8
- setup :prepare_destination
9
-
10
- tests Rails::Generators::ScaffoldControllerGenerator
11
- arguments %w(account name:string description:text business:references)
12
-
13
- def test_generated_controller
14
- return true if Rails::VERSION::MAJOR < 4
15
-
16
- run_generator
17
-
18
- assert_file 'app/controllers/accounts_controller.rb' do |content|
19
- assert_instance_method :index, content do |m|
20
- assert_match(/@accounts = Account\.all/, m)
21
- assert_match(/format.html/, m)
22
- assert_match(/format.json \{ render json: @accounts \}/, m)
23
- end
24
-
25
- assert_instance_method :show, content do |m|
26
- assert_match(/format.html/, m)
27
- assert_match(/format.json \{ render json: @account \}/, m)
28
- end
29
-
30
- assert_instance_method :new, content do |m|
31
- assert_match(/@account = Account\.new/, m)
32
- end
33
-
34
- assert_instance_method :edit, content do |m|
35
- assert_predicate m, :blank?
36
- end
37
-
38
- assert_instance_method :create, content do |m|
39
- assert_match(/@account = Account\.new\(account_params\)/, m)
40
- assert_match(/@account\.save/, m)
41
- assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully created\.' \}/, m)
42
- assert_match(/format\.json \{ render json: @account, status: :created \}/, m)
43
- assert_match(/format\.html \{ render action: 'new' \}/, m)
44
- assert_match(/format\.json \{ render json: @account\.errors, status: :unprocessable_entity \}/, m)
45
- end
46
-
47
- assert_instance_method :update, content do |m|
48
- assert_match(/format\.html \{ redirect_to @account, notice: 'Account was successfully updated\.' \}/, m)
49
- assert_match(/format\.json \{ head :no_content \}/, m)
50
- assert_match(/format\.html \{ render action: 'edit' \}/, m)
51
- assert_match(/format\.json \{ render json: @account.errors, status: :unprocessable_entity \}/, m)
52
- end
53
-
54
- assert_instance_method :destroy, content do |m|
55
- assert_match(/@account\.destroy/, m)
56
- assert_match(/format\.html { redirect_to accounts_url \}/, m)
57
- assert_match(/format\.json \{ head :no_content \}/, m)
58
- end
59
-
60
- assert_match(/def account_params/, content)
61
- assert_match(/params\.require\(:account\)\.permit\(:name, :description, :business_id\)/, content)
62
- end
63
- end
64
- end
@@ -1,41 +0,0 @@
1
- require 'test_helper'
2
- require 'rails'
3
- require 'active_model/serializer/railtie'
4
- require 'test_app'
5
-
6
- class SerializerGeneratorTest < Rails::Generators::TestCase
7
- destination File.expand_path('../../../tmp', __FILE__)
8
- setup :prepare_destination
9
-
10
- tests Rails::Generators::SerializerGenerator
11
- arguments %w(account name:string description:text business:references)
12
-
13
- def test_generates_a_serializer_with_attributes_and_associations
14
- run_generator
15
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/ do |serializer|
16
- assert_match(/attributes :id, :name, :description/, serializer)
17
- assert_match(/has_one :business/, serializer)
18
- end
19
- end
20
-
21
- def test_generates_a_namespaced_serializer
22
- run_generator ['admin/account']
23
- assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/
24
- end
25
-
26
- def test_uses_application_serializer_if_one_exists
27
- Object.const_set(:ApplicationSerializer, Class.new)
28
- run_generator
29
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
30
- ensure
31
- Object.send :remove_const, :ApplicationSerializer
32
- end
33
-
34
- def test_uses_given_parent
35
- Object.const_set(:ApplicationSerializer, Class.new)
36
- run_generator ['Account', '--parent=MySerializer']
37
- assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/
38
- ensure
39
- Object.send :remove_const, :ApplicationSerializer
40
- end
41
- end
data/test/test_app.rb DELETED
@@ -1,18 +0,0 @@
1
- class TestApp < Rails::Application
2
- if config.respond_to?(:load_defaults)
3
- config.load_defaults("#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}")
4
- end
5
-
6
- if Rails.version.to_s.first >= '4'
7
- config.eager_load = false
8
- config.secret_key_base = 'abc123'
9
- end
10
- config.after_initialize do
11
- Rails.application.routes.default_url_options = { host: 'http://example.com' }
12
- end
13
-
14
- # Set up a logger to avoid creating a log directory on every run.
15
- config.logger = Logger.new(nil)
16
- end
17
-
18
- TestApp.initialize!
data/test/test_helper.rb DELETED
@@ -1,31 +0,0 @@
1
- require 'bundler/setup'
2
- require 'minitest/autorun'
3
- require 'active_model_serializers'
4
- require 'fixtures/poro'
5
- begin
6
- require 'rails-controller-testing'
7
- Rails::Controller::Testing.install
8
- rescue LoadError
9
- false # Rails 5 < backward compatibility
10
- end
11
-
12
-
13
- # Ensure backward compatibility with Minitest 4
14
- Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
15
-
16
- module TestHelper
17
- Routes = ActionDispatch::Routing::RouteSet.new
18
- Routes.draw do
19
- get ':controller(/:action(/:id))'
20
- get ':controller(/:action)'
21
- end
22
-
23
- ActionController::Base.send :include, Routes.url_helpers
24
- ActionController::Base.send :include, ActionController::Serialization
25
- end
26
-
27
- ActionController::TestCase.class_eval do
28
- def setup
29
- @routes = TestHelper::Routes
30
- end
31
- end
@@ -1,2 +0,0 @@
1
- // Place all the behaviors and hooks related to the matching controller here.
2
- // All this logic will automatically be available in application.js.
@@ -1,4 +0,0 @@
1
- /*
2
- Place all the styles related to the matching controller here.
3
- They will automatically be included in application.css.
4
- */
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
- class AccountsController < ApplicationController
3
- end
@@ -1,3 +0,0 @@
1
- # frozen_string_literal: true
2
- module AccountsHelper
3
- end
@@ -1,4 +0,0 @@
1
- # frozen_string_literal: true
2
- class AccountSerializer < ActiveModel::Serializer
3
- attributes :id
4
- end
@@ -1,2 +0,0 @@
1
- # frozen_string_literal: true
2
- Rails.application.routes.draw {}
@@ -1,18 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActiveModel
4
- class ArraySerializer
5
- class ExceptTest < Minitest::Test
6
- def test_array_serializer_pass_except_to_items_serializers
7
- array = [Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' }),
8
- Profile.new({ name: 'Name 2', description: 'Description 2', comments: 'Comments 2' })]
9
- serializer = ArraySerializer.new(array, except: [:description])
10
-
11
- expected = [{ name: 'Name 1' },
12
- { name: 'Name 2' }]
13
-
14
- assert_equal expected, serializer.serializable_array
15
- end
16
- end
17
- end
18
- end