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,229 @@
1
+ verbose = $VERBOSE
2
+ $VERBOSE = nil
3
+ class Model < ActiveModelSerializers::Model
4
+ FILE_DIGEST = Digest::MD5.hexdigest(File.open(__FILE__).read)
5
+
6
+ ### Helper methods, not required to be serializable
7
+
8
+ # Convenience when not adding @attributes readers and writers
9
+ def method_missing(meth, *args)
10
+ if meth.to_s =~ /^(.*)=$/
11
+ attributes[$1.to_sym] = args[0]
12
+ elsif attributes.key?(meth)
13
+ attributes[meth]
14
+ else
15
+ super
16
+ end
17
+ end
18
+
19
+ # required for ActiveModel::AttributeAssignment#_assign_attribute
20
+ # in Rails 5
21
+ def respond_to_missing?(method_name, _include_private = false)
22
+ attributes.key?(method_name.to_s.tr('=', '').to_sym) || super
23
+ end
24
+
25
+ def cache_key_with_digest
26
+ "#{cache_key}/#{FILE_DIGEST}"
27
+ end
28
+ end
29
+
30
+ class Profile < Model
31
+ end
32
+
33
+ class ProfileSerializer < ActiveModel::Serializer
34
+ attributes :name, :description
35
+
36
+ # TODO: is this used anywhere?
37
+ def arguments_passed_in?
38
+ instance_options[:my_options] == :accessible
39
+ end
40
+ end
41
+
42
+ class ProfilePreviewSerializer < ActiveModel::Serializer
43
+ attributes :name
44
+ end
45
+
46
+ Post = Class.new(Model)
47
+ Like = Class.new(Model)
48
+ Author = Class.new(Model)
49
+ Bio = Class.new(Model)
50
+ Blog = Class.new(Model)
51
+ Role = Class.new(Model)
52
+ User = Class.new(Model)
53
+ Location = Class.new(Model)
54
+ Place = Class.new(Model)
55
+ Tag = Class.new(Model)
56
+ VirtualValue = Class.new(Model)
57
+ Comment = Class.new(Model) do
58
+ # Uses a custom non-time-based cache key
59
+ def cache_key
60
+ "#{self.class.name.downcase}/#{self.id}"
61
+ end
62
+ end
63
+
64
+ module Spam; end
65
+ Spam::UnrelatedLink = Class.new(Model)
66
+
67
+ PostSerializer = Class.new(ActiveModel::Serializer) do
68
+ cache key: 'post', expires_in: 0.1, skip_digest: true
69
+ attributes :id, :title, :body
70
+
71
+ has_many :comments
72
+ belongs_to :blog
73
+ belongs_to :author
74
+
75
+ def blog
76
+ Blog.new(id: 999, name: 'Custom blog')
77
+ end
78
+
79
+ # TODO: is this used anywhere?
80
+ def custom_options
81
+ instance_options
82
+ end
83
+ end
84
+
85
+ SpammyPostSerializer = Class.new(ActiveModel::Serializer) do
86
+ attributes :id
87
+ has_many :related
88
+ end
89
+
90
+ CommentSerializer = Class.new(ActiveModel::Serializer) do
91
+ cache expires_in: 1.day, skip_digest: true
92
+ attributes :id, :body
93
+
94
+ belongs_to :post
95
+ belongs_to :author
96
+
97
+ def custom_options
98
+ instance_options
99
+ end
100
+ end
101
+
102
+ AuthorSerializer = Class.new(ActiveModel::Serializer) do
103
+ cache key: 'writer', skip_digest: true
104
+ attribute :id
105
+ attribute :name
106
+
107
+ has_many :posts
108
+ has_many :roles
109
+ has_one :bio
110
+ end
111
+
112
+ RoleSerializer = Class.new(ActiveModel::Serializer) do
113
+ cache only: [:name], skip_digest: true
114
+ attributes :id, :name, :description, :slug
115
+
116
+ def slug
117
+ "#{object.name}-#{object.id}"
118
+ end
119
+
120
+ belongs_to :author
121
+ end
122
+
123
+ LikeSerializer = Class.new(ActiveModel::Serializer) do
124
+ attributes :id, :time
125
+
126
+ belongs_to :likeable
127
+ end
128
+
129
+ LocationSerializer = Class.new(ActiveModel::Serializer) do
130
+ cache only: [:place], skip_digest: true
131
+ attributes :id, :lat, :lng
132
+
133
+ belongs_to :place
134
+
135
+ def place
136
+ 'Nowhere'
137
+ end
138
+ end
139
+
140
+ PlaceSerializer = Class.new(ActiveModel::Serializer) do
141
+ attributes :id, :name
142
+
143
+ has_many :locations
144
+ end
145
+
146
+ BioSerializer = Class.new(ActiveModel::Serializer) do
147
+ cache except: [:content], skip_digest: true
148
+ attributes :id, :content, :rating
149
+
150
+ belongs_to :author
151
+ end
152
+
153
+ BlogSerializer = Class.new(ActiveModel::Serializer) do
154
+ cache key: 'blog'
155
+ attributes :id, :name
156
+
157
+ belongs_to :writer
158
+ has_many :articles
159
+ end
160
+
161
+ PaginatedSerializer = Class.new(ActiveModel::Serializer::CollectionSerializer) do
162
+ def json_key
163
+ 'paginated'
164
+ end
165
+ end
166
+
167
+ AlternateBlogSerializer = Class.new(ActiveModel::Serializer) do
168
+ attribute :id
169
+ attribute :name, key: :title
170
+ end
171
+
172
+ CustomBlogSerializer = Class.new(ActiveModel::Serializer) do
173
+ attribute :id
174
+ attribute :special_attribute
175
+
176
+ has_many :articles
177
+ end
178
+
179
+ CommentPreviewSerializer = Class.new(ActiveModel::Serializer) do
180
+ attributes :id
181
+
182
+ belongs_to :post
183
+ end
184
+
185
+ AuthorPreviewSerializer = Class.new(ActiveModel::Serializer) do
186
+ attributes :id
187
+
188
+ has_many :posts
189
+ end
190
+
191
+ PostPreviewSerializer = Class.new(ActiveModel::Serializer) do
192
+ attributes :title, :body, :id
193
+
194
+ has_many :comments, serializer: CommentPreviewSerializer
195
+ belongs_to :author, serializer: AuthorPreviewSerializer
196
+ end
197
+
198
+ PostWithTagsSerializer = Class.new(ActiveModel::Serializer) do
199
+ attributes :id
200
+
201
+ has_many :tags
202
+ end
203
+
204
+ PostWithCustomKeysSerializer = Class.new(ActiveModel::Serializer) do
205
+ attributes :id
206
+
207
+ has_many :comments, key: :reviews
208
+ belongs_to :author, key: :writer
209
+ has_one :blog, key: :site
210
+ end
211
+
212
+ VirtualValueSerializer = Class.new(ActiveModel::Serializer) do
213
+ attributes :id
214
+
215
+ has_many :reviews, virtual_value: [{ id: 1 }, { id: 2 }]
216
+ has_one :maker, virtual_value: { id: 1 }
217
+
218
+ def reviews
219
+ end
220
+
221
+ def maker
222
+ end
223
+ end
224
+
225
+ Spam::UnrelatedLinkSerializer = Class.new(ActiveModel::Serializer) do
226
+ cache only: [:id]
227
+ attributes :id
228
+ end
229
+ $VERBOSE = verbose
@@ -0,0 +1,24 @@
1
+ require 'test_helper'
2
+ require 'generators/rails/resource_override'
3
+
4
+ class ResourceGeneratorTest < Rails::Generators::TestCase
5
+ destination File.expand_path('../../../tmp/generators', __FILE__)
6
+ setup :prepare_destination, :copy_routes
7
+
8
+ tests Rails::Generators::ResourceGenerator
9
+ arguments %w(account)
10
+
11
+ def test_serializer_file_is_generated
12
+ run_generator
13
+
14
+ assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
15
+ end
16
+
17
+ private
18
+
19
+ def copy_routes
20
+ config_dir = File.join(destination_root, 'config')
21
+ FileUtils.mkdir_p(config_dir)
22
+ File.write(File.join(config_dir, 'routes.rb'), 'Rails.application.routes.draw {}')
23
+ end
24
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+ require 'generators/rails/resource_override'
3
+ require 'generators/rails/serializer_generator'
4
+
5
+ class SerializerGeneratorTest < Rails::Generators::TestCase
6
+ destination File.expand_path('../../../tmp/generators', __FILE__)
7
+ setup :prepare_destination
8
+
9
+ tests Rails::Generators::SerializerGenerator
10
+ arguments %w(account name:string description:text business:references)
11
+
12
+ def test_generates_a_serializer
13
+ run_generator
14
+ assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ActiveModel::Serializer/
15
+ end
16
+
17
+ def test_generates_a_namespaced_serializer
18
+ run_generator ['admin/account']
19
+ assert_file 'app/serializers/admin/account_serializer.rb', /class Admin::AccountSerializer < ActiveModel::Serializer/
20
+ end
21
+
22
+ def test_uses_application_serializer_if_one_exists
23
+ Object.const_set(:ApplicationSerializer, Class.new)
24
+ run_generator
25
+ assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < ApplicationSerializer/
26
+ ensure
27
+ Object.send :remove_const, :ApplicationSerializer
28
+ end
29
+
30
+ def test_uses_given_parent
31
+ Object.const_set(:ApplicationSerializer, Class.new)
32
+ run_generator ['Account', '--parent=MySerializer']
33
+ assert_file 'app/serializers/account_serializer.rb', /class AccountSerializer < MySerializer/
34
+ ensure
35
+ Object.send :remove_const, :ApplicationSerializer
36
+ end
37
+
38
+ def test_generates_attributes_and_associations
39
+ run_generator
40
+ assert_file 'app/serializers/account_serializer.rb' do |serializer|
41
+ assert_match(/^ attributes :id, :name, :description$/, serializer)
42
+ assert_match(/^ has_one :business$/, serializer)
43
+ assert_match(/^end\n*\z/, serializer)
44
+ end
45
+ end
46
+
47
+ def test_with_no_attributes_does_not_add_extra_space
48
+ run_generator ['account']
49
+ assert_file 'app/serializers/account_serializer.rb' do |content|
50
+ if RUBY_PLATFORM =~ /mingw/
51
+ assert_no_match(/\r\n\r\nend/, content)
52
+ else
53
+ assert_no_match(/\n\nend/, content)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,82 @@
1
+ require 'test_helper'
2
+ require 'grape'
3
+ require 'grape/active_model_serializers'
4
+
5
+ class ActiveModelSerializers::GrapeTest < ActiveSupport::TestCase
6
+ include Rack::Test::Methods
7
+ module Models
8
+ def self.model1
9
+ ARModels::Post.new(id: 1, title: 'Dummy Title', body: 'Lorem Ipsum')
10
+ end
11
+
12
+ def self.model2
13
+ ARModels::Post.new(id: 2, title: 'Second Dummy Title', body: 'Second Lorem Ipsum')
14
+ end
15
+
16
+ def self.all
17
+ @all ||=
18
+ begin
19
+ model1.save!
20
+ model2.save!
21
+ ARModels::Post.all
22
+ end
23
+ end
24
+ end
25
+
26
+ class GrapeTest < Grape::API
27
+ format :json
28
+ include Grape::ActiveModelSerializers
29
+
30
+ resources :grape do
31
+ get '/render' do
32
+ render Models.model1
33
+ end
34
+
35
+ get '/render_with_json_api' do
36
+ post = Models.model1
37
+ render post, meta: { page: 1, total_pages: 2 }, adapter: :json_api
38
+ end
39
+
40
+ get '/render_array_with_json_api' do
41
+ posts = Models.all
42
+ render posts, adapter: :json_api
43
+ end
44
+ end
45
+ end
46
+
47
+ def app
48
+ GrapeTest.new
49
+ end
50
+
51
+ def test_formatter_returns_json
52
+ get '/grape/render'
53
+
54
+ post = Models.model1
55
+ serializable_resource = serializable(post)
56
+
57
+ assert last_response.ok?
58
+ assert_equal serializable_resource.to_json, last_response.body
59
+ end
60
+
61
+ def test_render_helper_passes_through_options_correctly
62
+ get '/grape/render_with_json_api'
63
+
64
+ post = Models.model1
65
+ serializable_resource = serializable(post, serializer: ARModels::PostSerializer, adapter: :json_api, meta: { page: 1, total_pages: 2 })
66
+
67
+ assert last_response.ok?
68
+ assert_equal serializable_resource.to_json, last_response.body
69
+ end
70
+
71
+ def test_formatter_handles_arrays
72
+ get '/grape/render_array_with_json_api'
73
+
74
+ posts = Models.all
75
+ serializable_resource = serializable(posts, adapter: :json_api)
76
+
77
+ assert last_response.ok?
78
+ assert_equal serializable_resource.to_json, last_response.body
79
+ ensure
80
+ ARModels::Post.delete_all
81
+ end
82
+ end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class IncludeTree
6
+ class FromStringTest < ActiveSupport::TestCase
7
+ def test_simple_array
8
+ input = [:comments, :author]
9
+ actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
10
+ assert(actual.key?(:author))
11
+ assert(actual.key?(:comments))
12
+ end
13
+
14
+ def test_nested_array
15
+ input = [:comments, posts: [:author, comments: [:author]]]
16
+ actual = ActiveModel::Serializer::IncludeTree.from_include_args(input)
17
+ assert(actual.key?(:posts))
18
+ assert(actual[:posts].key?(:author))
19
+ assert(actual[:posts].key?(:comments))
20
+ assert(actual[:posts][:comments].key?(:author))
21
+ assert(actual.key?(:comments))
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,94 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class IncludeTree
6
+ class FromStringTest < ActiveSupport::TestCase
7
+ def test_single_string
8
+ input = 'author'
9
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
10
+ assert(actual.key?(:author))
11
+ end
12
+
13
+ def test_multiple_strings
14
+ input = 'author,comments'
15
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
16
+ assert(actual.key?(:author))
17
+ assert(actual.key?(:comments))
18
+ end
19
+
20
+ def test_multiple_strings_with_space
21
+ input = 'author, comments'
22
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
23
+ assert(actual.key?(:author))
24
+ assert(actual.key?(:comments))
25
+ end
26
+
27
+ def test_nested_string
28
+ input = 'posts.author'
29
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
30
+ assert(actual.key?(:posts))
31
+ assert(actual[:posts].key?(:author))
32
+ end
33
+
34
+ def test_multiple_nested_string
35
+ input = 'posts.author,posts.comments.author,comments'
36
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
37
+ assert(actual.key?(:posts))
38
+ assert(actual[:posts].key?(:author))
39
+ assert(actual[:posts].key?(:comments))
40
+ assert(actual[:posts][:comments].key?(:author))
41
+ assert(actual.key?(:comments))
42
+ end
43
+
44
+ def test_toplevel_star_string
45
+ input = '*'
46
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
47
+ assert(actual.key?(:comments))
48
+ end
49
+
50
+ def test_nested_star_string
51
+ input = 'posts.*'
52
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
53
+ assert(actual.key?(:posts))
54
+ assert(actual[:posts].key?(:comments))
55
+ end
56
+
57
+ def test_nested_star_middle_string
58
+ input = 'posts.*.author'
59
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
60
+ assert(actual.key?(:posts))
61
+ assert(actual[:posts].key?(:comments))
62
+ assert(actual[:posts][:comments].key?(:author))
63
+ refute(actual[:posts][:comments].key?(:unspecified))
64
+ end
65
+
66
+ def test_nested_star_lower_precedence_string
67
+ input = 'posts.comments.author,posts.*'
68
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
69
+ assert(actual.key?(:posts))
70
+ assert(actual[:posts].key?(:comments))
71
+ assert(actual[:posts][:comments].key?(:author))
72
+ end
73
+
74
+ def test_toplevel_double_star_string
75
+ input = '**'
76
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
77
+ assert(actual.key?(:posts))
78
+ assert(actual[:posts].key?(:comments))
79
+ assert(actual[:posts][:comments].key?(:posts))
80
+ end
81
+
82
+ def test_nested_double_star_string
83
+ input = 'comments, posts.**'
84
+ actual = ActiveModel::Serializer::IncludeTree.from_string(input)
85
+ assert(actual.key?(:comments))
86
+ refute(actual[:comments].key?(:author))
87
+ assert(actual.key?(:posts))
88
+ assert(actual[:posts].key?(:comments))
89
+ assert(actual[:posts][:comments].key?(:posts))
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,64 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class IncludeTree
6
+ module Parsing
7
+ class IncludeArgsToHashTest < MiniTest::Test
8
+ def test_include_args_to_hash_from_symbol
9
+ expected = { author: {} }
10
+ input = :author
11
+ actual = Parsing.include_args_to_hash(input)
12
+
13
+ assert_equal(expected, actual)
14
+ end
15
+
16
+ def test_include_args_to_hash_from_array
17
+ expected = { author: {}, comments: {} }
18
+ input = [:author, :comments]
19
+ actual = Parsing.include_args_to_hash(input)
20
+
21
+ assert_equal(expected, actual)
22
+ end
23
+
24
+ def test_include_args_to_hash_from_nested_array
25
+ expected = { author: {}, comments: { author: {} } }
26
+ input = [:author, comments: [:author]]
27
+ actual = Parsing.include_args_to_hash(input)
28
+
29
+ assert_equal(expected, actual)
30
+ end
31
+
32
+ def test_include_args_to_hash_from_array_of_hashes
33
+ expected = {
34
+ author: {},
35
+ blogs: { posts: { contributors: {} } },
36
+ comments: { author: { blogs: { posts: {} } } }
37
+ }
38
+ input = [
39
+ :author,
40
+ blogs: [posts: :contributors],
41
+ comments: { author: { blogs: :posts } }
42
+ ]
43
+ actual = Parsing.include_args_to_hash(input)
44
+
45
+ assert_equal(expected, actual)
46
+ end
47
+
48
+ def test_array_of_string
49
+ expected = {
50
+ comments: { author: {}, attachment: {} }
51
+ }
52
+ input = [
53
+ 'comments.author',
54
+ 'comments.attachment'
55
+ ]
56
+ actual = Parsing.include_args_to_hash(input)
57
+
58
+ assert_equal(expected, actual)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
data/test/lint_test.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class Serializer
5
+ class LintTest < ActiveSupport::TestCase
6
+ include ActiveModel::Serializer::Lint::Tests
7
+
8
+ class CompliantResource
9
+ def serializable_hash(options = nil)
10
+ end
11
+
12
+ def read_attribute_for_serialization(name)
13
+ end
14
+
15
+ def as_json(options = nil)
16
+ end
17
+
18
+ def to_json(options = nil)
19
+ end
20
+
21
+ def cache_key
22
+ end
23
+
24
+ def id
25
+ end
26
+
27
+ def updated_at
28
+ end
29
+
30
+ def self.model_name
31
+ @_model_name ||= ActiveModel::Name.new(self)
32
+ end
33
+ end
34
+
35
+ def setup
36
+ @resource = CompliantResource.new
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ class ActiveModelSerializers::LoggerTest < ActiveSupport::TestCase
4
+ def test_logger_is_set_to_action_controller_logger_when_initializer_runs
5
+ assert_equal $action_controller_logger, ActionController::Base.logger # rubocop:disable Style/GlobalVars
6
+ end
7
+
8
+ def test_logger_can_be_set
9
+ original_logger = ActiveModelSerializers.logger
10
+ logger = Logger.new(STDOUT)
11
+
12
+ ActiveModelSerializers.logger = logger
13
+
14
+ assert_equal ActiveModelSerializers.logger, logger
15
+ ensure
16
+ ActiveModelSerializers.logger = original_logger
17
+ end
18
+ end
data/test/poro_test.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class PoroTest < ActiveSupport::TestCase
4
+ include ActiveModel::Serializer::Lint::Tests
5
+
6
+ def setup
7
+ @resource = Model.new
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ module ActiveModel
4
+ class SerializableResourceTest < ActiveSupport::TestCase
5
+ def setup
6
+ @resource = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
7
+ @serializer = ProfileSerializer.new(@resource)
8
+ @adapter = ActiveModel::Serializer::Adapter.create(@serializer)
9
+ @serializable_resource = ActiveModel::SerializableResource.new(@resource)
10
+ end
11
+
12
+ def test_serializable_resource_delegates_serializable_hash_to_the_adapter
13
+ options = nil
14
+ assert_equal @adapter.serializable_hash(options), @serializable_resource.serializable_hash(options)
15
+ end
16
+
17
+ def test_serializable_resource_delegates_to_json_to_the_adapter
18
+ options = nil
19
+ assert_equal @adapter.to_json(options), @serializable_resource.to_json(options)
20
+ end
21
+
22
+ def test_serializable_resource_delegates_as_json_to_the_adapter
23
+ options = nil
24
+ assert_equal @adapter.as_json(options), @serializable_resource.as_json(options)
25
+ end
26
+ end
27
+ end