active_model_serializers 0.8.3 → 0.10.0.rc4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -0
  3. data/.rubocop.yml +86 -0
  4. data/.rubocop_todo.yml +240 -0
  5. data/.simplecov +111 -0
  6. data/.travis.yml +33 -22
  7. data/CHANGELOG.md +358 -6
  8. data/CONTRIBUTING.md +220 -0
  9. data/Gemfile +46 -1
  10. data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
  11. data/README.md +81 -591
  12. data/Rakefile +68 -11
  13. data/active_model_serializers.gemspec +57 -23
  14. data/appveyor.yml +27 -0
  15. data/docs/ARCHITECTURE.md +120 -0
  16. data/docs/DESIGN.textile +8 -0
  17. data/docs/README.md +35 -0
  18. data/docs/general/adapters.md +162 -0
  19. data/docs/general/caching.md +52 -0
  20. data/docs/general/configuration_options.md +27 -0
  21. data/docs/general/getting_started.md +98 -0
  22. data/docs/general/instrumentation.md +40 -0
  23. data/docs/general/logging.md +14 -0
  24. data/docs/general/rendering.md +153 -0
  25. data/docs/general/serializers.md +207 -0
  26. data/docs/how-open-source-maintained.jpg +0 -0
  27. data/docs/howto/add_pagination_links.md +121 -0
  28. data/docs/howto/add_root_key.md +51 -0
  29. data/docs/howto/outside_controller_use.md +58 -0
  30. data/docs/howto/test.md +152 -0
  31. data/docs/integrations/ember-and-json-api.md +112 -0
  32. data/docs/integrations/grape.md +19 -0
  33. data/docs/jsonapi/schema/schema.json +366 -0
  34. data/docs/jsonapi/schema.md +140 -0
  35. data/lib/action_controller/serialization.rb +41 -37
  36. data/lib/active_model/serializable_resource.rb +72 -0
  37. data/lib/active_model/serializer/adapter/attributes.rb +66 -0
  38. data/lib/active_model/serializer/adapter/base.rb +58 -0
  39. data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
  40. data/lib/active_model/serializer/adapter/fragment_cache.rb +111 -0
  41. data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
  42. data/lib/active_model/serializer/adapter/json.rb +21 -0
  43. data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
  44. data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
  45. data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
  46. data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
  47. data/lib/active_model/serializer/adapter/json_api.rb +223 -0
  48. data/lib/active_model/serializer/adapter/null.rb +11 -0
  49. data/lib/active_model/serializer/adapter.rb +91 -0
  50. data/lib/active_model/serializer/array_serializer.rb +9 -0
  51. data/lib/active_model/serializer/association.rb +20 -0
  52. data/lib/active_model/serializer/associations.rb +87 -220
  53. data/lib/active_model/serializer/attribute.rb +25 -0
  54. data/lib/active_model/serializer/attributes.rb +82 -0
  55. data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
  56. data/lib/active_model/serializer/caching.rb +100 -0
  57. data/lib/active_model/serializer/collection_reflection.rb +7 -0
  58. data/lib/active_model/serializer/collection_serializer.rb +47 -0
  59. data/lib/active_model/serializer/configuration.rb +28 -0
  60. data/lib/active_model/serializer/field.rb +56 -0
  61. data/lib/active_model/serializer/fieldset.rb +31 -0
  62. data/lib/active_model/serializer/has_many_reflection.rb +10 -0
  63. data/lib/active_model/serializer/has_one_reflection.rb +10 -0
  64. data/lib/active_model/serializer/include_tree.rb +111 -0
  65. data/lib/active_model/serializer/links.rb +33 -0
  66. data/lib/active_model/serializer/lint.rb +142 -0
  67. data/lib/active_model/serializer/reflection.rb +91 -0
  68. data/lib/active_model/serializer/singular_reflection.rb +7 -0
  69. data/lib/active_model/serializer/type.rb +25 -0
  70. data/lib/active_model/{serializers → serializer}/version.rb +1 -1
  71. data/lib/active_model/serializer.rb +99 -479
  72. data/lib/active_model_serializers/callbacks.rb +55 -0
  73. data/lib/active_model_serializers/deserialization.rb +13 -0
  74. data/lib/active_model_serializers/logging.rb +119 -0
  75. data/lib/active_model_serializers/model.rb +39 -0
  76. data/lib/active_model_serializers/railtie.rb +38 -0
  77. data/lib/active_model_serializers/serialization_context.rb +10 -0
  78. data/lib/active_model_serializers/test/schema.rb +103 -0
  79. data/lib/active_model_serializers/test/serializer.rb +125 -0
  80. data/lib/active_model_serializers/test.rb +7 -0
  81. data/lib/active_model_serializers.rb +20 -92
  82. data/lib/generators/rails/USAGE +6 -0
  83. data/lib/generators/rails/resource_override.rb +10 -0
  84. data/lib/generators/rails/serializer_generator.rb +36 -0
  85. data/lib/generators/rails/templates/serializer.rb.erb +8 -0
  86. data/lib/grape/active_model_serializers.rb +14 -0
  87. data/lib/grape/formatters/active_model_serializers.rb +15 -0
  88. data/lib/grape/helpers/active_model_serializers.rb +16 -0
  89. data/test/action_controller/adapter_selector_test.rb +53 -0
  90. data/test/action_controller/explicit_serializer_test.rb +134 -0
  91. data/test/action_controller/json/include_test.rb +167 -0
  92. data/test/action_controller/json_api/deserialization_test.rb +59 -0
  93. data/test/action_controller/json_api/linked_test.rb +196 -0
  94. data/test/action_controller/json_api/pagination_test.rb +116 -0
  95. data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
  96. data/test/action_controller/serialization_test.rb +435 -0
  97. data/test/active_model_serializers/logging_test.rb +77 -0
  98. data/test/active_model_serializers/model_test.rb +9 -0
  99. data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
  100. data/test/active_model_serializers/serialization_context_test.rb +18 -0
  101. data/test/active_model_serializers/test/schema_test.rb +128 -0
  102. data/test/active_model_serializers/test/serializer_test.rb +63 -0
  103. data/test/active_record_test.rb +9 -0
  104. data/test/adapter/fragment_cache_test.rb +38 -0
  105. data/test/adapter/json/belongs_to_test.rb +47 -0
  106. data/test/adapter/json/collection_test.rb +92 -0
  107. data/test/adapter/json/has_many_test.rb +47 -0
  108. data/test/adapter/json_api/belongs_to_test.rb +157 -0
  109. data/test/adapter/json_api/collection_test.rb +97 -0
  110. data/test/adapter/json_api/fields_test.rb +89 -0
  111. data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
  112. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
  113. data/test/adapter/json_api/has_many_test.rb +145 -0
  114. data/test/adapter/json_api/has_one_test.rb +81 -0
  115. data/test/adapter/json_api/json_api_test.rb +37 -0
  116. data/test/adapter/json_api/linked_test.rb +394 -0
  117. data/test/adapter/json_api/links_test.rb +68 -0
  118. data/test/adapter/json_api/pagination_links_test.rb +115 -0
  119. data/test/adapter/json_api/parse_test.rb +139 -0
  120. data/test/adapter/json_api/resource_type_config_test.rb +71 -0
  121. data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
  122. data/test/adapter/json_test.rb +47 -0
  123. data/test/adapter/null_test.rb +25 -0
  124. data/test/adapter_test.rb +42 -0
  125. data/test/array_serializer_test.rb +36 -73
  126. data/test/collection_serializer_test.rb +100 -0
  127. data/test/fixtures/active_record.rb +56 -0
  128. data/test/fixtures/poro.rb +229 -0
  129. data/test/generators/scaffold_controller_generator_test.rb +24 -0
  130. data/test/generators/serializer_generator_test.rb +57 -0
  131. data/test/grape_test.rb +82 -0
  132. data/test/include_tree/from_include_args_test.rb +26 -0
  133. data/test/include_tree/from_string_test.rb +94 -0
  134. data/test/include_tree/include_args_to_hash_test.rb +64 -0
  135. data/test/lint_test.rb +40 -0
  136. data/test/logger_test.rb +18 -0
  137. data/test/poro_test.rb +9 -0
  138. data/test/serializable_resource_test.rb +27 -0
  139. data/test/serializers/adapter_for_test.rb +166 -0
  140. data/test/serializers/association_macros_test.rb +36 -0
  141. data/test/serializers/associations_test.rb +267 -0
  142. data/test/serializers/attribute_test.rb +123 -0
  143. data/test/serializers/attributes_test.rb +52 -0
  144. data/test/serializers/cache_test.rb +209 -0
  145. data/test/serializers/configuration_test.rb +32 -0
  146. data/test/serializers/fieldset_test.rb +14 -0
  147. data/test/serializers/meta_test.rb +130 -0
  148. data/test/serializers/options_test.rb +21 -0
  149. data/test/serializers/root_test.rb +21 -0
  150. data/test/serializers/serializer_for_test.rb +134 -0
  151. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  152. data/test/support/isolated_unit.rb +77 -0
  153. data/test/support/rails5_shims.rb +29 -0
  154. data/test/support/rails_app.rb +25 -0
  155. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
  156. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
  157. data/test/support/schemas/custom/show.json +7 -0
  158. data/test/support/schemas/hyper_schema.json +93 -0
  159. data/test/support/schemas/render_using_json_api.json +43 -0
  160. data/test/support/schemas/simple_json_pointers.json +10 -0
  161. data/test/support/serialization_testing.rb +53 -0
  162. data/test/support/simplecov.rb +6 -0
  163. data/test/support/stream_capture.rb +50 -0
  164. data/test/support/test_case.rb +19 -0
  165. data/test/test_helper.rb +55 -24
  166. metadata +358 -42
  167. data/DESIGN.textile +0 -586
  168. data/Gemfile.edge +0 -9
  169. data/bench/perf.rb +0 -43
  170. data/cruft.md +0 -19
  171. data/lib/active_model/array_serializer.rb +0 -104
  172. data/lib/active_record/serializer_override.rb +0 -16
  173. data/lib/generators/resource_override.rb +0 -13
  174. data/lib/generators/serializer/USAGE +0 -9
  175. data/lib/generators/serializer/serializer_generator.rb +0 -42
  176. data/lib/generators/serializer/templates/serializer.rb +0 -19
  177. data/test/association_test.rb +0 -592
  178. data/test/caching_test.rb +0 -96
  179. data/test/generators_test.rb +0 -85
  180. data/test/no_serialization_scope_test.rb +0 -34
  181. data/test/serialization_test.rb +0 -392
  182. data/test/serializer_support_test.rb +0 -51
  183. data/test/serializer_test.rb +0 -1465
  184. data/test/test_fakes.rb +0 -217
@@ -0,0 +1,209 @@
1
+ require 'test_helper'
2
+ require 'tmpdir'
3
+ require 'tempfile'
4
+ module ActiveModel
5
+ class Serializer
6
+ class CacheTest < ActiveSupport::TestCase
7
+ include ActiveSupport::Testing::Stream
8
+
9
+ def setup
10
+ ActionController::Base.cache_store.clear
11
+ @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
12
+ @post = Post.new(title: 'New Post', body: 'Body')
13
+ @bio = Bio.new(id: 1, content: 'AMS Contributor')
14
+ @author = Author.new(name: 'Joao M. D. Moura')
15
+ @blog = Blog.new(id: 999, name: 'Custom blog', writer: @author, articles: [])
16
+ @role = Role.new(name: 'Great Author')
17
+ @location = Location.new(lat: '-23.550520', lng: '-46.633309')
18
+ @place = Place.new(name: 'Amazing Place')
19
+ @author.posts = [@post]
20
+ @author.roles = [@role]
21
+ @role.author = @author
22
+ @author.bio = @bio
23
+ @bio.author = @author
24
+ @post.comments = [@comment]
25
+ @post.author = @author
26
+ @comment.post = @post
27
+ @comment.author = @author
28
+ @post.blog = @blog
29
+ @location.place = @place
30
+
31
+ @location_serializer = LocationSerializer.new(@location)
32
+ @bio_serializer = BioSerializer.new(@bio)
33
+ @role_serializer = RoleSerializer.new(@role)
34
+ @post_serializer = PostSerializer.new(@post)
35
+ @author_serializer = AuthorSerializer.new(@author)
36
+ @comment_serializer = CommentSerializer.new(@comment)
37
+ @blog_serializer = BlogSerializer.new(@blog)
38
+ end
39
+
40
+ def test_inherited_cache_configuration
41
+ inherited_serializer = Class.new(PostSerializer)
42
+
43
+ assert_equal PostSerializer._cache_key, inherited_serializer._cache_key
44
+ assert_equal PostSerializer._cache_options, inherited_serializer._cache_options
45
+ end
46
+
47
+ def test_override_cache_configuration
48
+ inherited_serializer = Class.new(PostSerializer) do
49
+ cache key: 'new-key'
50
+ end
51
+
52
+ assert_equal PostSerializer._cache_key, 'post'
53
+ assert_equal inherited_serializer._cache_key, 'new-key'
54
+ end
55
+
56
+ def test_cache_definition
57
+ assert_equal(ActionController::Base.cache_store, @post_serializer.class._cache)
58
+ assert_equal(ActionController::Base.cache_store, @author_serializer.class._cache)
59
+ assert_equal(ActionController::Base.cache_store, @comment_serializer.class._cache)
60
+ end
61
+
62
+ def test_cache_key_definition
63
+ assert_equal('post', @post_serializer.class._cache_key)
64
+ assert_equal('writer', @author_serializer.class._cache_key)
65
+ assert_equal(nil, @comment_serializer.class._cache_key)
66
+ end
67
+
68
+ def test_cache_key_interpolation_with_updated_at
69
+ render_object_with_cache(@author)
70
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@author.cache_key))
71
+ assert_equal(@author_serializer.attributes.to_json, ActionController::Base.cache_store.fetch("#{@author_serializer.class._cache_key}/#{@author_serializer.object.id}-#{@author_serializer.object.updated_at.strftime("%Y%m%d%H%M%S%9N")}").to_json)
72
+ end
73
+
74
+ def test_default_cache_key_fallback
75
+ render_object_with_cache(@comment)
76
+ assert_equal(@comment_serializer.attributes.to_json, ActionController::Base.cache_store.fetch(@comment.cache_key).to_json)
77
+ end
78
+
79
+ def test_cache_options_definition
80
+ assert_equal({ expires_in: 0.1, skip_digest: true }, @post_serializer.class._cache_options)
81
+ assert_equal(nil, @blog_serializer.class._cache_options)
82
+ assert_equal({ expires_in: 1.day, skip_digest: true }, @comment_serializer.class._cache_options)
83
+ end
84
+
85
+ def test_fragment_cache_definition
86
+ assert_equal([:name], @role_serializer.class._cache_only)
87
+ assert_equal([:content], @bio_serializer.class._cache_except)
88
+ end
89
+
90
+ def test_associations_separately_cache
91
+ ActionController::Base.cache_store.clear
92
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@post.cache_key))
93
+ assert_equal(nil, ActionController::Base.cache_store.fetch(@comment.cache_key))
94
+
95
+ Timecop.freeze(Time.now) do
96
+ render_object_with_cache(@post)
97
+
98
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
99
+ assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
100
+ end
101
+ end
102
+
103
+ def test_associations_cache_when_updated
104
+ # Clean the Cache
105
+ ActionController::Base.cache_store.clear
106
+
107
+ Timecop.freeze(Time.now) do
108
+ # Generate a new Cache of Post object and each objects related to it.
109
+ render_object_with_cache(@post)
110
+
111
+ # Check if it cached the objects separately
112
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
113
+ assert_equal(@comment_serializer.attributes, ActionController::Base.cache_store.fetch(@comment.cache_key))
114
+
115
+ # Simulating update on comments relationship with Post
116
+ new_comment = Comment.new(id: 2, body: 'ZOMG A NEW COMMENT')
117
+ new_comment_serializer = CommentSerializer.new(new_comment)
118
+ @post.comments = [new_comment]
119
+
120
+ # Ask for the serialized object
121
+ render_object_with_cache(@post)
122
+
123
+ # Check if the the new comment was cached
124
+ assert_equal(new_comment_serializer.attributes, ActionController::Base.cache_store.fetch(new_comment.cache_key))
125
+ assert_equal(@post_serializer.attributes, ActionController::Base.cache_store.fetch(@post.cache_key))
126
+ end
127
+ end
128
+
129
+ def test_fragment_fetch_with_virtual_associations
130
+ expected_result = {
131
+ id: @location.id,
132
+ lat: @location.lat,
133
+ lng: @location.lng,
134
+ place: 'Nowhere'
135
+ }
136
+
137
+ hash = render_object_with_cache(@location)
138
+
139
+ assert_equal(hash, expected_result)
140
+ assert_equal({ place: 'Nowhere' }, ActionController::Base.cache_store.fetch(@location.cache_key))
141
+ end
142
+
143
+ def test_uses_file_digest_in_cache_key
144
+ render_object_with_cache(@blog)
145
+ assert_equal(@blog_serializer.attributes, ActionController::Base.cache_store.fetch(@blog.cache_key_with_digest))
146
+ end
147
+
148
+ def test_cache_digest_definition
149
+ assert_equal(::Model::FILE_DIGEST, @post_serializer.class._cache_digest)
150
+ end
151
+
152
+ def test_serializer_file_path_on_nix
153
+ path = '/Users/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
154
+ caller_line = "#{path}:1:in `<top (required)>'"
155
+ assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
156
+ end
157
+
158
+ def test_serializer_file_path_on_windows
159
+ path = 'c:/git/emberjs/ember-crm-backend/app/serializers/lead_serializer.rb'
160
+ caller_line = "#{path}:1:in `<top (required)>'"
161
+ assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
162
+ end
163
+
164
+ def test_serializer_file_path_with_space
165
+ path = '/Users/git/ember js/ember-crm-backend/app/serializers/lead_serializer.rb'
166
+ caller_line = "#{path}:1:in `<top (required)>'"
167
+ assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
168
+ end
169
+
170
+ def test_serializer_file_path_with_submatch
171
+ # The submatch in the path ensures we're using a correctly greedy regexp.
172
+ path = '/Users/git/ember js/ember:123:in x/app/serializers/lead_serializer.rb'
173
+ caller_line = "#{path}:1:in `<top (required)>'"
174
+ assert_equal caller_line[ActiveModel::Serializer::CALLER_FILE], path
175
+ end
176
+
177
+ def test_digest_caller_file
178
+ contents = "puts 'AMS rocks'!"
179
+ dir = Dir.mktmpdir('space char')
180
+ file = Tempfile.new('some_ruby.rb', dir)
181
+ file.write(contents)
182
+ path = file.path
183
+ caller_line = "#{path}:1:in `<top (required)>'"
184
+ file.close
185
+ assert_equal ActiveModel::Serializer.digest_caller_file(caller_line), Digest::MD5.hexdigest(contents)
186
+ ensure
187
+ file.unlink
188
+ FileUtils.remove_entry dir
189
+ end
190
+
191
+ def test_warn_on_serializer_not_defined_in_file
192
+ called = false
193
+ serializer = Class.new(ActiveModel::Serializer)
194
+ assert_match(/_cache_digest/, (capture(:stderr) do
195
+ serializer.digest_caller_file('')
196
+ called = true
197
+ end))
198
+ assert called
199
+ end
200
+
201
+ private
202
+
203
+ def render_object_with_cache(obj)
204
+ ActiveModel::SerializableResource.new(obj).serializable_hash
205
+ end
206
+ end
207
+ end
208
+ end
209
+
@@ -0,0 +1,32 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class ConfigurationTest < ActiveSupport::TestCase
6
+ def test_collection_serializer
7
+ assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.collection_serializer
8
+ end
9
+
10
+ def test_array_serializer
11
+ assert_equal ActiveModel::Serializer::CollectionSerializer, ActiveModelSerializers.config.array_serializer
12
+ end
13
+
14
+ def test_setting_array_serializer_sets_collection_serializer
15
+ config = ActiveModelSerializers.config
16
+ old_config = config.dup
17
+ begin
18
+ assert_equal ActiveModel::Serializer::CollectionSerializer, config.collection_serializer
19
+ config.array_serializer = :foo
20
+ assert_equal config.array_serializer, :foo
21
+ assert_equal config.collection_serializer, :foo
22
+ ensure
23
+ ActiveModelSerializers.config.replace(old_config)
24
+ end
25
+ end
26
+
27
+ def test_default_adapter
28
+ assert_equal :attributes, ActiveModelSerializers.config.adapter
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class FieldsetTest < ActiveSupport::TestCase
6
+ def test_fieldset_with_hash
7
+ fieldset = ActiveModel::Serializer::Fieldset.new('post' => %w(id title), 'comment' => ['body'])
8
+ expected = { :post => [:id, :title], :comment => [:body] }
9
+
10
+ assert_equal(expected, fieldset.fields)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,130 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class MetaTest < ActiveSupport::TestCase
6
+ def setup
7
+ @blog = Blog.new(id: 1,
8
+ name: 'AMS Hints',
9
+ writer: Author.new(id: 2, name: 'Steve'),
10
+ articles: [Post.new(id: 3, title: 'AMS')])
11
+ end
12
+
13
+ def test_meta_is_present_with_root
14
+ actual = ActiveModel::SerializableResource.new(
15
+ @blog,
16
+ adapter: :json,
17
+ serializer: AlternateBlogSerializer,
18
+ meta: { total: 10 }).as_json
19
+ expected = {
20
+ blog: {
21
+ id: 1,
22
+ title: 'AMS Hints'
23
+ },
24
+ 'meta' => {
25
+ total: 10
26
+ }
27
+ }
28
+ assert_equal(expected, actual)
29
+ end
30
+
31
+ def test_meta_is_not_included_when_root_is_missing
32
+ actual = ActiveModel::SerializableResource.new(
33
+ @blog,
34
+ adapter: :attributes,
35
+ serializer: AlternateBlogSerializer,
36
+ meta: { total: 10 }).as_json
37
+ expected = {
38
+ id: 1,
39
+ title: 'AMS Hints'
40
+ }
41
+ assert_equal(expected, actual)
42
+ end
43
+
44
+ def test_meta_key_is_used
45
+ actual = ActiveModel::SerializableResource.new(
46
+ @blog,
47
+ adapter: :json,
48
+ serializer: AlternateBlogSerializer,
49
+ meta: { total: 10 },
50
+ meta_key: 'haha_meta').as_json
51
+ expected = {
52
+ blog: {
53
+ id: 1,
54
+ title: 'AMS Hints'
55
+ },
56
+ 'haha_meta' => {
57
+ total: 10
58
+ }
59
+ }
60
+ assert_equal(expected, actual)
61
+ end
62
+
63
+ def test_meta_key_is_used_with_json_api
64
+ actual = ActiveModel::SerializableResource.new(
65
+ @blog,
66
+ adapter: :json_api,
67
+ serializer: AlternateBlogSerializer,
68
+ meta: { total: 10 },
69
+ meta_key: 'haha_meta').as_json
70
+ expected = {
71
+ data: {
72
+ id: '1',
73
+ type: 'blogs',
74
+ attributes: { title: 'AMS Hints' }
75
+ },
76
+ 'haha_meta' => { total: 10 }
77
+ }
78
+ assert_equal(expected, actual)
79
+ end
80
+
81
+ def test_meta_is_not_present_on_arrays_without_root
82
+ actual = ActiveModel::SerializableResource.new(
83
+ [@blog],
84
+ adapter: :attributes,
85
+ meta: { total: 10 }).as_json
86
+ expected = [{
87
+ id: 1,
88
+ name: 'AMS Hints',
89
+ writer: {
90
+ id: 2,
91
+ name: 'Steve'
92
+ },
93
+ articles: [{
94
+ id: 3,
95
+ title: 'AMS',
96
+ body: nil
97
+ }]
98
+ }]
99
+ assert_equal(expected, actual)
100
+ end
101
+
102
+ def test_meta_is_present_on_arrays_with_root
103
+ actual = ActiveModel::SerializableResource.new(
104
+ [@blog],
105
+ adapter: :json,
106
+ meta: { total: 10 },
107
+ meta_key: 'haha_meta').as_json
108
+ expected = {
109
+ blogs: [{
110
+ id: 1,
111
+ name: 'AMS Hints',
112
+ writer: {
113
+ id: 2,
114
+ name: 'Steve'
115
+ },
116
+ articles: [{
117
+ id: 3,
118
+ title: 'AMS',
119
+ body: nil
120
+ }]
121
+ }],
122
+ 'haha_meta' => {
123
+ total: 10
124
+ }
125
+ }
126
+ assert_equal(expected, actual)
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class OptionsTest < ActiveSupport::TestCase
6
+ def setup
7
+ @profile = Profile.new(name: 'Name 1', description: 'Description 1')
8
+ end
9
+
10
+ def test_options_are_accessible
11
+ @profile_serializer = ProfileSerializer.new(@profile, my_options: :accessible)
12
+ assert @profile_serializer.arguments_passed_in?
13
+ end
14
+
15
+ def test_no_option_is_passed_in
16
+ @profile_serializer = ProfileSerializer.new(@profile)
17
+ refute @profile_serializer.arguments_passed_in?
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class RootTest < ActiveSupport::TestCase
6
+ def setup
7
+ @virtual_value = VirtualValue.new(id: 1)
8
+ end
9
+
10
+ def test_overwrite_root
11
+ serializer = VirtualValueSerializer.new(@virtual_value, { root: 'smth' })
12
+ assert_equal('smth', serializer.json_key)
13
+ end
14
+
15
+ def test_underscore_in_root
16
+ serializer = VirtualValueSerializer.new(@virtual_value)
17
+ assert_equal('virtual_value', serializer.json_key)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,134 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class SerializerForTest < ActiveSupport::TestCase
6
+ class CollectionSerializerTest < ActiveSupport::TestCase
7
+ def setup
8
+ @array = [1, 2, 3]
9
+ @previous_collection_serializer = ActiveModelSerializers.config.collection_serializer
10
+ end
11
+
12
+ def teardown
13
+ ActiveModelSerializers.config.collection_serializer = @previous_collection_serializer
14
+ end
15
+
16
+ def test_serializer_for_array
17
+ serializer = ActiveModel::Serializer.serializer_for(@array)
18
+ assert_equal ActiveModelSerializers.config.collection_serializer, serializer
19
+ end
20
+
21
+ def test_overwritten_serializer_for_array
22
+ new_collection_serializer = Class.new
23
+ ActiveModelSerializers.config.collection_serializer = new_collection_serializer
24
+ serializer = ActiveModel::Serializer.serializer_for(@array)
25
+ assert_equal new_collection_serializer, serializer
26
+ end
27
+ end
28
+
29
+ class SerializerTest < ActiveSupport::TestCase
30
+ module ResourceNamespace
31
+ Post = Class.new(::Model)
32
+ Comment = Class.new(::Model)
33
+
34
+ class PostSerializer < ActiveModel::Serializer
35
+ class CommentSerializer < ActiveModel::Serializer
36
+ end
37
+ end
38
+ end
39
+
40
+ class MyProfile < Profile
41
+ end
42
+
43
+ class CustomProfile
44
+ def serializer_class; ProfileSerializer; end
45
+ end
46
+
47
+ Tweet = Class.new(::Model)
48
+ TweetSerializer = Class.new
49
+
50
+ def setup
51
+ @profile = Profile.new
52
+ @my_profile = MyProfile.new
53
+ @custom_profile = CustomProfile.new
54
+ @model = ::Model.new
55
+ @tweet = Tweet.new
56
+ end
57
+
58
+ def test_serializer_for_non_ams_serializer
59
+ serializer = ActiveModel::Serializer.serializer_for(@tweet)
60
+ assert_equal nil, serializer
61
+ end
62
+
63
+ def test_serializer_for_existing_serializer
64
+ serializer = ActiveModel::Serializer.serializer_for(@profile)
65
+ assert_equal ProfileSerializer, serializer
66
+ end
67
+
68
+ def test_serializer_for_existing_serializer_with_lookup_disabled
69
+ serializer = with_serializer_lookup_disabled do
70
+ ActiveModel::Serializer.serializer_for(@profile)
71
+ end
72
+ assert_equal nil, serializer
73
+ end
74
+
75
+ def test_serializer_for_not_existing_serializer
76
+ serializer = ActiveModel::Serializer.serializer_for(@model)
77
+ assert_equal nil, serializer
78
+ end
79
+
80
+ def test_serializer_inherited_serializer
81
+ serializer = ActiveModel::Serializer.serializer_for(@my_profile)
82
+ assert_equal ProfileSerializer, serializer
83
+ end
84
+
85
+ def test_serializer_inherited_serializer_with_lookup_disabled
86
+ serializer = with_serializer_lookup_disabled do
87
+ ActiveModel::Serializer.serializer_for(@my_profile)
88
+ end
89
+ assert_equal nil, serializer
90
+ end
91
+
92
+ def test_serializer_custom_serializer
93
+ serializer = ActiveModel::Serializer.serializer_for(@custom_profile)
94
+ assert_equal ProfileSerializer, serializer
95
+ end
96
+
97
+ def test_serializer_custom_serializer_with_lookup_disabled
98
+ serializer = with_serializer_lookup_disabled do
99
+ ActiveModel::Serializer.serializer_for(@custom_profile)
100
+ end
101
+ assert_equal ProfileSerializer, serializer
102
+ end
103
+
104
+ def test_serializer_for_namespaced_resource
105
+ post = ResourceNamespace::Post.new
106
+ serializer = ActiveModel::Serializer.serializer_for(post)
107
+ assert_equal ResourceNamespace::PostSerializer, serializer
108
+ end
109
+
110
+ def test_serializer_for_namespaced_resource_with_lookup_disabled
111
+ post = ResourceNamespace::Post.new
112
+ serializer = with_serializer_lookup_disabled do
113
+ ActiveModel::Serializer.serializer_for(post)
114
+ end
115
+ assert_equal nil, serializer
116
+ end
117
+
118
+ def test_serializer_for_nested_resource
119
+ comment = ResourceNamespace::Comment.new
120
+ serializer = ResourceNamespace::PostSerializer.serializer_for(comment)
121
+ assert_equal ResourceNamespace::PostSerializer::CommentSerializer, serializer
122
+ end
123
+
124
+ def test_serializer_for_nested_resource_with_lookup_disabled
125
+ comment = ResourceNamespace::Comment.new
126
+ serializer = with_serializer_lookup_disabled do
127
+ ResourceNamespace::PostSerializer.serializer_for(comment)
128
+ end
129
+ assert_equal nil, serializer
130
+ end
131
+ end
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,6 @@
1
+ {
2
+ "properties": {
3
+ "name" : { "type" : "string" },
4
+ "description" : { "type" : "string" }
5
+ }
6
+ }
@@ -0,0 +1,77 @@
1
+ # https://github.com/rails/rails/blob/v5.0.0.beta1/railties/test/isolation/abstract_unit.rb
2
+
3
+ # Usage Example:
4
+ #
5
+ # require 'support/isolated_unit'
6
+ #
7
+ # class RailtieTest < ActiveSupport::TestCase
8
+ # include ActiveSupport::Testing::Isolation
9
+ #
10
+ # class WithRailsDefinedOnLoad < RailtieTest
11
+ # setup do
12
+ # require 'rails'
13
+ # require 'active_model_serializers'
14
+ # make_basic_app
15
+ # end
16
+ #
17
+ # # some tests
18
+ # end
19
+ #
20
+ # class WithoutRailsDefinedOnLoad < RailtieTest
21
+ # setup do
22
+ # require 'active_model_serializers'
23
+ # make_basic_app
24
+ # end
25
+ #
26
+ # # some tests
27
+ # end
28
+ # end
29
+ #
30
+ # Note:
31
+ # It is important to keep this file as light as possible
32
+ # the goal for tests that require this is to test booting up
33
+ # rails from an empty state, so anything added here could
34
+ # hide potential failures
35
+ #
36
+ # It is also good to know what is the bare minimum to get
37
+ # Rails booted up.
38
+ require 'bundler/setup' unless defined?(Bundler)
39
+ require 'active_support'
40
+ require 'active_support/core_ext/string/access'
41
+
42
+ # These files do not require any others and are needed
43
+ # to run the tests
44
+ require 'active_support/testing/autorun'
45
+ require 'active_support/testing/isolation'
46
+
47
+ module TestHelpers
48
+ module Generation
49
+ # Make a very basic app, without creating the whole directory structure.
50
+ # Is faster and simpler than generating a Rails app in a temp directory
51
+ def make_basic_app
52
+ require 'rails'
53
+ require 'action_controller/railtie'
54
+
55
+ @app = Class.new(Rails::Application) do
56
+ config.eager_load = false
57
+ config.session_store :cookie_store, key: '_myapp_session'
58
+ config.active_support.deprecation = :log
59
+ config.active_support.test_order = :parallel
60
+ ActiveSupport::TestCase.respond_to?(:test_order=) && ActiveSupport::TestCase.test_order = :parallel
61
+ config.root = File.dirname(__FILE__)
62
+ config.log_level = :info
63
+ # Set a fake logger to avoid creating the log directory automatically
64
+ fake_logger = Logger.new(nil)
65
+ config.logger = fake_logger
66
+ end
67
+ @app.respond_to?(:secrets) && @app.secrets.secret_key_base = '3b7cd727ee24e8444053437c36cc66c4'
68
+
69
+ yield @app if block_given?
70
+ @app.initialize!
71
+ end
72
+ end
73
+ end
74
+
75
+ class ActiveSupport::TestCase
76
+ include TestHelpers::Generation
77
+ end
@@ -0,0 +1,29 @@
1
+ module Rails5Shims
2
+ module ControllerTests
3
+ # https://github.com/rails/rails/blob/b217354/actionpack/lib/action_controller/test_case.rb
4
+ REQUEST_KWARGS = [:params, :session, :flash, :method, :body, :xhr]
5
+
6
+ # Fold kwargs from test request into args
7
+ # Band-aid for DEPRECATION WARNING
8
+ def get(path, *args)
9
+ hash = args && args[0]
10
+ if hash.respond_to?(:key)
11
+ Rails5Shims::ControllerTests::REQUEST_KWARGS.each do |kwarg|
12
+ next unless hash.key?(kwarg)
13
+ hash.merge! hash.delete(kwarg)
14
+ end
15
+ end
16
+ super
17
+ end
18
+
19
+ # Uncomment for debugging where the kwargs warnings come from
20
+ # def non_kwarg_request_warning
21
+ # super.tap do
22
+ # STDOUT.puts caller[2..3]
23
+ # end
24
+ # end
25
+ end
26
+ end
27
+ if Rails::VERSION::MAJOR < 5
28
+ ActionController::TestCase.send :include, Rails5Shims::ControllerTests
29
+ end