active_model_serializers 0.10.0 → 0.10.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. checksums.yaml +5 -5
  2. data/.rubocop.yml +10 -5
  3. data/.travis.yml +41 -21
  4. data/CHANGELOG.md +200 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +25 -4
  7. data/README.md +166 -28
  8. data/Rakefile +5 -32
  9. data/active_model_serializers.gemspec +23 -25
  10. data/appveyor.yml +10 -6
  11. data/bin/rubocop +38 -0
  12. data/docs/README.md +2 -1
  13. data/docs/general/adapters.md +35 -11
  14. data/docs/general/caching.md +7 -1
  15. data/docs/general/configuration_options.md +86 -1
  16. data/docs/general/deserialization.md +1 -1
  17. data/docs/general/fields.md +31 -0
  18. data/docs/general/getting_started.md +1 -1
  19. data/docs/general/logging.md +7 -0
  20. data/docs/general/rendering.md +63 -25
  21. data/docs/general/serializers.md +137 -14
  22. data/docs/howto/add_pagination_links.md +16 -17
  23. data/docs/howto/add_relationship_links.md +140 -0
  24. data/docs/howto/add_root_key.md +11 -0
  25. data/docs/howto/grape_integration.md +42 -0
  26. data/docs/howto/outside_controller_use.md +12 -4
  27. data/docs/howto/passing_arbitrary_options.md +2 -2
  28. data/docs/howto/serialize_poro.md +46 -5
  29. data/docs/howto/test.md +2 -0
  30. data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
  31. data/docs/integrations/ember-and-json-api.md +67 -32
  32. data/docs/jsonapi/schema.md +1 -1
  33. data/lib/action_controller/serialization.rb +15 -3
  34. data/lib/active_model/serializable_resource.rb +2 -0
  35. data/lib/active_model/serializer/adapter/attributes.rb +2 -0
  36. data/lib/active_model/serializer/adapter/base.rb +4 -0
  37. data/lib/active_model/serializer/adapter/json.rb +2 -0
  38. data/lib/active_model/serializer/adapter/json_api.rb +2 -0
  39. data/lib/active_model/serializer/adapter/null.rb +2 -0
  40. data/lib/active_model/serializer/adapter.rb +2 -0
  41. data/lib/active_model/serializer/array_serializer.rb +10 -5
  42. data/lib/active_model/serializer/association.rb +64 -10
  43. data/lib/active_model/serializer/attribute.rb +2 -0
  44. data/lib/active_model/serializer/belongs_to_reflection.rb +6 -3
  45. data/lib/active_model/serializer/collection_serializer.rb +39 -13
  46. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +87 -116
  47. data/lib/active_model/serializer/error_serializer.rb +13 -7
  48. data/lib/active_model/serializer/errors_serializer.rb +27 -20
  49. data/lib/active_model/serializer/field.rb +2 -0
  50. data/lib/active_model/serializer/fieldset.rb +2 -0
  51. data/lib/active_model/serializer/has_many_reflection.rb +5 -3
  52. data/lib/active_model/serializer/has_one_reflection.rb +3 -4
  53. data/lib/active_model/serializer/lazy_association.rb +99 -0
  54. data/lib/active_model/serializer/link.rb +23 -0
  55. data/lib/active_model/serializer/lint.rb +136 -130
  56. data/lib/active_model/serializer/null.rb +2 -0
  57. data/lib/active_model/serializer/reflection.rb +132 -67
  58. data/lib/active_model/serializer/version.rb +3 -1
  59. data/lib/active_model/serializer.rb +308 -82
  60. data/lib/active_model_serializers/adapter/attributes.rb +5 -66
  61. data/lib/active_model_serializers/adapter/base.rb +41 -39
  62. data/lib/active_model_serializers/adapter/json.rb +2 -0
  63. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +4 -2
  64. data/lib/active_model_serializers/adapter/json_api/error.rb +2 -0
  65. data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +2 -0
  66. data/lib/active_model_serializers/adapter/json_api/link.rb +3 -1
  67. data/lib/active_model_serializers/adapter/json_api/meta.rb +2 -0
  68. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +49 -21
  69. data/lib/active_model_serializers/adapter/json_api/relationship.rb +77 -23
  70. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +41 -10
  71. data/lib/active_model_serializers/adapter/json_api.rb +84 -65
  72. data/lib/active_model_serializers/adapter/null.rb +2 -0
  73. data/lib/active_model_serializers/adapter.rb +9 -1
  74. data/lib/active_model_serializers/callbacks.rb +2 -0
  75. data/lib/active_model_serializers/deprecate.rb +3 -2
  76. data/lib/active_model_serializers/deserialization.rb +4 -0
  77. data/lib/active_model_serializers/json_pointer.rb +2 -0
  78. data/lib/active_model_serializers/logging.rb +2 -0
  79. data/lib/active_model_serializers/lookup_chain.rb +82 -0
  80. data/lib/active_model_serializers/model.rb +111 -28
  81. data/lib/active_model_serializers/railtie.rb +7 -1
  82. data/lib/active_model_serializers/register_jsonapi_renderer.rb +46 -31
  83. data/lib/active_model_serializers/serializable_resource.rb +10 -7
  84. data/lib/active_model_serializers/serialization_context.rb +12 -3
  85. data/lib/active_model_serializers/test/schema.rb +4 -2
  86. data/lib/active_model_serializers/test/serializer.rb +2 -0
  87. data/lib/active_model_serializers/test.rb +2 -0
  88. data/lib/active_model_serializers.rb +35 -10
  89. data/lib/generators/rails/resource_override.rb +3 -1
  90. data/lib/generators/rails/serializer_generator.rb +6 -4
  91. data/lib/grape/active_model_serializers.rb +9 -5
  92. data/lib/grape/formatters/active_model_serializers.rb +21 -2
  93. data/lib/grape/helpers/active_model_serializers.rb +3 -0
  94. data/lib/tasks/rubocop.rake +55 -0
  95. data/test/action_controller/adapter_selector_test.rb +16 -5
  96. data/test/action_controller/explicit_serializer_test.rb +7 -4
  97. data/test/action_controller/json/include_test.rb +108 -27
  98. data/test/action_controller/json_api/deserialization_test.rb +3 -1
  99. data/test/action_controller/json_api/errors_test.rb +10 -9
  100. data/test/action_controller/json_api/fields_test.rb +68 -0
  101. data/test/action_controller/json_api/linked_test.rb +31 -24
  102. data/test/action_controller/json_api/pagination_test.rb +33 -23
  103. data/test/action_controller/json_api/transform_test.rb +13 -3
  104. data/test/action_controller/lookup_proc_test.rb +51 -0
  105. data/test/action_controller/namespace_lookup_test.rb +234 -0
  106. data/test/action_controller/serialization_scope_name_test.rb +14 -6
  107. data/test/action_controller/serialization_test.rb +23 -12
  108. data/test/active_model_serializers/adapter_for_test.rb +2 -0
  109. data/test/active_model_serializers/json_pointer_test.rb +17 -13
  110. data/test/active_model_serializers/logging_test.rb +2 -0
  111. data/test/active_model_serializers/model_test.rb +139 -4
  112. data/test/active_model_serializers/railtie_test_isolated.rb +14 -7
  113. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
  114. data/test/active_model_serializers/serialization_context_test_isolated.rb +25 -10
  115. data/test/active_model_serializers/test/schema_test.rb +5 -2
  116. data/test/active_model_serializers/test/serializer_test.rb +2 -0
  117. data/test/active_record_test.rb +2 -0
  118. data/test/adapter/attributes_test.rb +42 -0
  119. data/test/adapter/deprecation_test.rb +2 -0
  120. data/test/adapter/json/belongs_to_test.rb +2 -0
  121. data/test/adapter/json/collection_test.rb +16 -0
  122. data/test/adapter/json/has_many_test.rb +12 -2
  123. data/test/adapter/json/transform_test.rb +17 -15
  124. data/test/adapter/json_api/belongs_to_test.rb +2 -0
  125. data/test/adapter/json_api/collection_test.rb +6 -3
  126. data/test/adapter/json_api/errors_test.rb +19 -19
  127. data/test/adapter/json_api/fields_test.rb +14 -3
  128. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +2 -0
  129. data/test/adapter/json_api/has_many_test.rb +51 -20
  130. data/test/adapter/json_api/has_one_test.rb +2 -0
  131. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
  132. data/test/adapter/json_api/json_api_test.rb +7 -7
  133. data/test/adapter/json_api/linked_test.rb +35 -12
  134. data/test/adapter/json_api/links_test.rb +22 -3
  135. data/test/adapter/json_api/pagination_links_test.rb +55 -13
  136. data/test/adapter/json_api/parse_test.rb +3 -1
  137. data/test/adapter/json_api/relationship_test.rb +311 -73
  138. data/test/adapter/json_api/resource_meta_test.rb +5 -3
  139. data/test/adapter/json_api/toplevel_jsonapi_test.rb +2 -0
  140. data/test/adapter/json_api/transform_test.rb +265 -253
  141. data/test/adapter/json_api/type_test.rb +170 -36
  142. data/test/adapter/json_test.rb +10 -7
  143. data/test/adapter/null_test.rb +3 -2
  144. data/test/adapter/polymorphic_test.rb +54 -5
  145. data/test/adapter_test.rb +3 -1
  146. data/test/array_serializer_test.rb +2 -0
  147. data/test/benchmark/app.rb +3 -1
  148. data/test/benchmark/benchmarking_support.rb +3 -1
  149. data/test/benchmark/bm_active_record.rb +83 -0
  150. data/test/benchmark/bm_adapter.rb +40 -0
  151. data/test/benchmark/bm_caching.rb +18 -16
  152. data/test/benchmark/bm_lookup_chain.rb +85 -0
  153. data/test/benchmark/bm_transform.rb +23 -10
  154. data/test/benchmark/controllers.rb +18 -17
  155. data/test/benchmark/fixtures.rb +74 -72
  156. data/test/cache_test.rb +301 -69
  157. data/test/collection_serializer_test.rb +33 -14
  158. data/test/fixtures/active_record.rb +47 -10
  159. data/test/fixtures/poro.rb +128 -183
  160. data/test/generators/scaffold_controller_generator_test.rb +2 -0
  161. data/test/generators/serializer_generator_test.rb +25 -5
  162. data/test/grape_test.rb +172 -56
  163. data/test/lint_test.rb +3 -1
  164. data/test/logger_test.rb +15 -11
  165. data/test/poro_test.rb +2 -0
  166. data/test/serializable_resource_test.rb +20 -22
  167. data/test/serializers/association_macros_test.rb +5 -2
  168. data/test/serializers/associations_test.rb +274 -49
  169. data/test/serializers/attribute_test.rb +7 -3
  170. data/test/serializers/attributes_test.rb +3 -1
  171. data/test/serializers/caching_configuration_test_isolated.rb +8 -6
  172. data/test/serializers/configuration_test.rb +2 -0
  173. data/test/serializers/fieldset_test.rb +3 -1
  174. data/test/serializers/meta_test.rb +14 -6
  175. data/test/serializers/options_test.rb +19 -6
  176. data/test/serializers/read_attribute_for_serialization_test.rb +5 -3
  177. data/test/serializers/reflection_test.rb +481 -0
  178. data/test/serializers/root_test.rb +3 -1
  179. data/test/serializers/serialization_test.rb +4 -2
  180. data/test/serializers/serializer_for_test.rb +14 -10
  181. data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
  182. data/test/support/isolated_unit.rb +11 -4
  183. data/test/support/rails5_shims.rb +10 -2
  184. data/test/support/rails_app.rb +4 -9
  185. data/test/support/serialization_testing.rb +33 -5
  186. data/test/test_helper.rb +15 -0
  187. metadata +126 -46
  188. data/.rubocop_todo.yml +0 -167
  189. data/docs/ARCHITECTURE.md +0 -126
  190. data/lib/active_model/serializer/associations.rb +0 -100
  191. data/lib/active_model/serializer/attributes.rb +0 -82
  192. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  193. data/lib/active_model/serializer/configuration.rb +0 -35
  194. data/lib/active_model/serializer/include_tree.rb +0 -111
  195. data/lib/active_model/serializer/links.rb +0 -35
  196. data/lib/active_model/serializer/meta.rb +0 -29
  197. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  198. data/lib/active_model/serializer/type.rb +0 -25
  199. data/lib/active_model_serializers/key_transform.rb +0 -70
  200. data/test/active_model_serializers/key_transform_test.rb +0 -263
  201. data/test/adapter/json_api/has_many_embed_ids_test.rb +0 -43
  202. data/test/adapter/json_api/relationships_test.rb +0 -199
  203. data/test/adapter/json_api/resource_identifier_test.rb +0 -85
  204. data/test/include_tree/from_include_args_test.rb +0 -26
  205. data/test/include_tree/from_string_test.rb +0 -94
  206. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,34 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './benchmarking_support'
2
4
  require_relative './app'
3
5
 
4
6
  time = 10
5
7
  disable_gc = true
6
8
  ActiveModelSerializers.config.key_transform = :unaltered
7
- comments = (0..50).map do |i|
8
- Comment.new(id: i, body: 'ZOMG A COMMENT')
9
+ has_many_relationships = (0..50).map do |i|
10
+ HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
9
11
  end
10
- author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
11
- post = Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
12
- serializer = PostSerializer.new(post)
12
+ has_one_relationship = HasOneRelationship.new(
13
+ id: 42,
14
+ first_name: 'Joao',
15
+ last_name: 'Moura'
16
+ )
17
+ primary_resource = PrimaryResource.new(
18
+ id: 1337,
19
+ title: 'New PrimaryResource',
20
+ virtual_attribute: nil,
21
+ body: 'Body',
22
+ has_many_relationships: has_many_relationships,
23
+ has_one_relationship: has_one_relationship
24
+ )
25
+ serializer = PrimaryResourceSerializer.new(primary_resource)
13
26
  adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)
14
27
  serialization = adapter.as_json
15
28
 
16
29
  Benchmark.ams('camel', time: time, disable_gc: disable_gc) do
17
- ActiveModelSerializers::KeyTransform.camel(serialization)
30
+ CaseTransform.camel(serialization)
18
31
  end
19
32
 
20
33
  Benchmark.ams('camel_lower', time: time, disable_gc: disable_gc) do
21
- ActiveModelSerializers::KeyTransform.camel_lower(serialization)
34
+ CaseTransform.camel_lower(serialization)
22
35
  end
23
36
 
24
37
  Benchmark.ams('dash', time: time, disable_gc: disable_gc) do
25
- ActiveModelSerializers::KeyTransform.dash(serialization)
38
+ CaseTransform.dash(serialization)
26
39
  end
27
40
 
28
41
  Benchmark.ams('unaltered', time: time, disable_gc: disable_gc) do
29
- ActiveModelSerializers::KeyTransform.unaltered(serialization)
42
+ CaseTransform.unaltered(serialization)
30
43
  end
31
44
 
32
45
  Benchmark.ams('underscore', time: time, disable_gc: disable_gc) do
33
- ActiveModelSerializers::KeyTransform.underscore(serialization)
46
+ CaseTransform.underscore(serialization)
34
47
  end
@@ -1,31 +1,32 @@
1
- class PostController < ActionController::Base
2
- POST =
1
+ # frozen_string_literal: true
2
+
3
+ class PrimaryResourceController < ActionController::Base
4
+ PRIMARY_RESOURCE =
3
5
  begin
4
- updated_at = Time.current
5
6
  if ENV['BENCH_STRESS']
6
- comments = (0..50).map do |i|
7
- Comment.new(id: i, body: 'ZOMG A COMMENT', updated_at: updated_at + i)
7
+ has_many_relationships = (0..50).map do |i|
8
+ HasManyRelationship.new(id: i, body: 'ZOMG A HAS MANY RELATIONSHIP')
8
9
  end
9
10
  else
10
- comments = [Comment.new(id: 1, body: 'ZOMG A COMMENT', updated_at: updated_at)]
11
+ has_many_relationships = [HasManyRelationship.new(id: 1, body: 'ZOMG A HAS MANY RELATIONSHIP')]
11
12
  end
12
- author = Author.new(id: 42, first_name: 'Joao', last_name: 'Moura')
13
- Post.new(id: 1337, title: 'New Post', blog: nil, body: 'Body', comments: comments, author: author)
13
+ has_one_relationship = HasOneRelationship.new(id: 42, first_name: 'Joao', last_name: 'Moura')
14
+ PrimaryResource.new(id: 1337, title: 'New PrimaryResource', virtual_attribute: nil, body: 'Body', has_many_relationships: has_many_relationships, has_one_relationship: has_one_relationship)
14
15
  end
15
16
 
16
17
  def render_with_caching_serializer
17
18
  toggle_cache_status
18
- render json: POST, serializer: CachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
19
+ render json: PRIMARY_RESOURCE, serializer: CachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
19
20
  end
20
21
 
21
22
  def render_with_fragment_caching_serializer
22
23
  toggle_cache_status
23
- render json: POST, serializer: FragmentCachingPostSerializer, adapter: :json, meta: { caching: perform_caching }
24
+ render json: PRIMARY_RESOURCE, serializer: FragmentCachingPrimaryResourceSerializer, adapter: :json, meta: { caching: perform_caching }
24
25
  end
25
26
 
26
27
  def render_with_non_caching_serializer
27
28
  toggle_cache_status
28
- render json: POST, adapter: :json, meta: { caching: perform_caching }
29
+ render json: PRIMARY_RESOURCE, adapter: :json, meta: { caching: perform_caching }
29
30
  end
30
31
 
31
32
  def render_cache_status
@@ -33,7 +34,7 @@ class PostController < ActionController::Base
33
34
  # Uncomment to debug
34
35
  # STDERR.puts cache_store.class
35
36
  # STDERR.puts cache_dependencies
36
- # ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPostSerializer._cache, perform_caching, params].inspect
37
+ # ActiveSupport::Cache::Store.logger.debug [ActiveModelSerializers.config.cache_store, ActiveModelSerializers.config.perform_caching, CachingPrimaryResourceSerializer._cache, perform_caching, params].inspect
37
38
  render json: { caching: perform_caching, meta: { cache_log: cache_messages, cache_status: cache_status } }.to_json
38
39
  end
39
40
 
@@ -76,9 +77,9 @@ class PostController < ActionController::Base
76
77
  end
77
78
 
78
79
  Rails.application.routes.draw do
79
- get '/status(/:on)' => 'post#render_cache_status'
80
- get '/clear' => 'post#clear'
81
- get '/caching(/:on)' => 'post#render_with_caching_serializer'
82
- get '/fragment_caching(/:on)' => 'post#render_with_fragment_caching_serializer'
83
- get '/non_caching(/:on)' => 'post#render_with_non_caching_serializer'
80
+ get '/status(/:on)' => 'primary_resource#render_cache_status'
81
+ get '/clear' => 'primary_resource#clear'
82
+ get '/caching(/:on)' => 'primary_resource#render_with_caching_serializer'
83
+ get '/fragment_caching(/:on)' => 'primary_resource#render_with_fragment_caching_serializer'
84
+ get '/non_caching(/:on)' => 'primary_resource#render_with_non_caching_serializer'
84
85
  end
@@ -1,33 +1,35 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.configuration.serializers = []
2
- class AuthorSerializer < ActiveModel::Serializer
4
+ class HasOneRelationshipSerializer < ActiveModel::Serializer
3
5
  attributes :id, :first_name, :last_name
4
6
 
5
- has_many :posts, embed: :ids
7
+ has_many :primary_resources, embed: :ids
6
8
  has_one :bio
7
9
  end
8
- Rails.configuration.serializers << AuthorSerializer
10
+ Rails.configuration.serializers << HasOneRelationshipSerializer
9
11
 
10
- class BlogSerializer < ActiveModel::Serializer
12
+ class VirtualAttributeSerializer < ActiveModel::Serializer
11
13
  attributes :id, :name
12
14
  end
13
- Rails.configuration.serializers << BlogSerializer
15
+ Rails.configuration.serializers << VirtualAttributeSerializer
14
16
 
15
- class CommentSerializer < ActiveModel::Serializer
16
- attributes :id, :body, :updated_at
17
+ class HasManyRelationshipSerializer < ActiveModel::Serializer
18
+ attributes :id, :body
17
19
 
18
- belongs_to :post
19
- belongs_to :author
20
+ belongs_to :primary_resource
21
+ belongs_to :has_one_relationship
20
22
  end
21
- Rails.configuration.serializers << CommentSerializer
23
+ Rails.configuration.serializers << HasManyRelationshipSerializer
22
24
 
23
- class PostSerializer < ActiveModel::Serializer
25
+ class PrimaryResourceSerializer < ActiveModel::Serializer
24
26
  attributes :id, :title, :body
25
27
 
26
- has_many :comments, serializer: CommentSerializer
27
- belongs_to :blog, serializer: BlogSerializer
28
- belongs_to :author, serializer: AuthorSerializer
28
+ has_many :has_many_relationships, serializer: HasManyRelationshipSerializer
29
+ belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
30
+ belongs_to :has_one_relationship, serializer: HasOneRelationshipSerializer
29
31
 
30
- link(:post_authors) { 'https://example.com/post_authors' }
32
+ link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
31
33
 
32
34
  meta do
33
35
  {
@@ -36,33 +38,33 @@ class PostSerializer < ActiveModel::Serializer
36
38
  }
37
39
  end
38
40
 
39
- def blog
40
- Blog.new(id: 999, name: 'Custom blog')
41
+ def virtual_attribute
42
+ VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
41
43
  end
42
44
  end
43
- Rails.configuration.serializers << PostSerializer
45
+ Rails.configuration.serializers << PrimaryResourceSerializer
44
46
 
45
- class CachingAuthorSerializer < AuthorSerializer
47
+ class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
46
48
  cache key: 'writer', skip_digest: true
47
49
  end
48
- Rails.configuration.serializers << CachingAuthorSerializer
50
+ Rails.configuration.serializers << CachingHasOneRelationshipSerializer
49
51
 
50
- class CachingCommentSerializer < CommentSerializer
52
+ class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
51
53
  cache expires_in: 1.day, skip_digest: true
52
54
  end
53
- Rails.configuration.serializers << CachingCommentSerializer
55
+ Rails.configuration.serializers << CachingHasManyRelationshipSerializer
54
56
 
55
57
  # see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
56
- class CachingPostSerializer < ActiveModel::Serializer
57
- cache key: 'post', expires_in: 0.1, skip_digest: true
58
+ class CachingPrimaryResourceSerializer < ActiveModel::Serializer
59
+ cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
58
60
 
59
61
  attributes :id, :title, :body
60
62
 
61
- belongs_to :blog, serializer: BlogSerializer
62
- belongs_to :author, serializer: CachingAuthorSerializer
63
- has_many :comments, serializer: CachingCommentSerializer
63
+ belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
64
+ belongs_to :has_one_relationship, serializer: CachingHasOneRelationshipSerializer
65
+ has_many :has_many_relationships, serializer: CachingHasManyRelationshipSerializer
64
66
 
65
- link(:post_authors) { 'https://example.com/post_authors' }
67
+ link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
66
68
 
67
69
  meta do
68
70
  {
@@ -71,33 +73,33 @@ class CachingPostSerializer < ActiveModel::Serializer
71
73
  }
72
74
  end
73
75
 
74
- def blog
75
- Blog.new(id: 999, name: 'Custom blog')
76
+ def virtual_attribute
77
+ VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
76
78
  end
77
79
  end
78
- Rails.configuration.serializers << CachingPostSerializer
80
+ Rails.configuration.serializers << CachingPrimaryResourceSerializer
79
81
 
80
- class FragmentCachingAuthorSerializer < AuthorSerializer
82
+ class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
81
83
  cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
82
84
  end
83
- Rails.configuration.serializers << FragmentCachingAuthorSerializer
85
+ Rails.configuration.serializers << FragmentCachingHasOneRelationshipSerializer
84
86
 
85
- class FragmentCachingCommentSerializer < CommentSerializer
86
- cache expires_in: 1.day, except: [:updated_at], skip_digest: true
87
+ class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
88
+ cache expires_in: 1.day, except: [:body], skip_digest: true
87
89
  end
88
- Rails.configuration.serializers << CachingCommentSerializer
90
+ Rails.configuration.serializers << CachingHasManyRelationshipSerializer
89
91
 
90
92
  # see https://github.com/rails-api/active_model_serializers/pull/1690/commits/68715b8f99bc29677e8a47bb3f305f23c077024b#r60344532
91
- class FragmentCachingPostSerializer < ActiveModel::Serializer
92
- cache key: 'post', expires_in: 0.1, skip_digest: true
93
+ class FragmentCachingPrimaryResourceSerializer < ActiveModel::Serializer
94
+ cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
93
95
 
94
96
  attributes :id, :title, :body
95
97
 
96
- belongs_to :blog, serializer: BlogSerializer
97
- belongs_to :author, serializer: FragmentCachingAuthorSerializer
98
- has_many :comments, serializer: FragmentCachingCommentSerializer
98
+ belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
99
+ belongs_to :has_one_relationship, serializer: FragmentCachingHasOneRelationshipSerializer
100
+ has_many :has_many_relationships, serializer: FragmentCachingHasManyRelationshipSerializer
99
101
 
100
- link(:post_authors) { 'https://example.com/post_authors' }
102
+ link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
101
103
 
102
104
  meta do
103
105
  {
@@ -106,11 +108,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
106
108
  }
107
109
  end
108
110
 
109
- def blog
110
- Blog.new(id: 999, name: 'Custom blog')
111
+ def virtual_attribute
112
+ VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
111
113
  end
112
114
  end
113
- Rails.configuration.serializers << FragmentCachingPostSerializer
115
+ Rails.configuration.serializers << FragmentCachingPrimaryResourceSerializer
114
116
 
115
117
  if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
116
118
  require 'active_record'
@@ -119,48 +121,48 @@ if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
119
121
  ActiveRecord::Schema.define do
120
122
  self.verbose = false
121
123
 
122
- create_table :blogs, force: true do |t|
124
+ create_table :virtual_attributes, force: true do |t|
123
125
  t.string :name
124
126
  t.timestamps null: false
125
127
  end
126
- create_table :authors, force: true do |t|
128
+ create_table :has_one_relationships, force: true do |t|
127
129
  t.string :first_name
128
130
  t.string :last_name
129
131
  t.timestamps null: false
130
132
  end
131
- create_table :posts, force: true do |t|
133
+ create_table :primary_resources, force: true do |t|
132
134
  t.string :title
133
135
  t.text :body
134
- t.references :author
135
- t.references :blog
136
+ t.references :has_one_relationship
137
+ t.references :virtual_attribute
136
138
  t.timestamps null: false
137
139
  end
138
- create_table :comments, force: true do |t|
140
+ create_table :has_many_relationships, force: true do |t|
139
141
  t.text :body
140
- t.references :author
141
- t.references :post
142
+ t.references :has_one_relationship
143
+ t.references :primary_resource
142
144
  t.timestamps null: false
143
145
  end
144
146
  end
145
147
 
146
- class Comment < ActiveRecord::Base
147
- belongs_to :author
148
- belongs_to :post
148
+ class HasManyRelationship < ActiveRecord::Base
149
+ belongs_to :has_one_relationship
150
+ belongs_to :primary_resource
149
151
  end
150
152
 
151
- class Author < ActiveRecord::Base
152
- has_many :posts
153
- has_many :comments
153
+ class HasOneRelationship < ActiveRecord::Base
154
+ has_many :primary_resources
155
+ has_many :has_many_relationships
154
156
  end
155
157
 
156
- class Post < ActiveRecord::Base
157
- has_many :comments
158
- belongs_to :author
159
- belongs_to :blog
158
+ class PrimaryResource < ActiveRecord::Base
159
+ has_many :has_many_relationships
160
+ belongs_to :has_one_relationship
161
+ belongs_to :virtual_attribute
160
162
  end
161
163
 
162
- class Blog < ActiveRecord::Base
163
- has_many :posts
164
+ class VirtualAttribute < ActiveRecord::Base
165
+ has_many :primary_resources
164
166
  end
165
167
  else
166
168
  # ActiveModelSerializers::Model is a convenient
@@ -201,19 +203,19 @@ else
201
203
  end
202
204
  end
203
205
 
204
- class Comment < BenchmarkModel
205
- attr_accessor :id, :body, :updated_at
206
+ class HasManyRelationship < BenchmarkModel
207
+ attr_accessor :id, :body
206
208
  end
207
209
 
208
- class Author < BenchmarkModel
209
- attr_accessor :id, :first_name, :last_name, :posts
210
+ class HasOneRelationship < BenchmarkModel
211
+ attr_accessor :id, :first_name, :last_name, :primary_resources
210
212
  end
211
213
 
212
- class Post < BenchmarkModel
213
- attr_accessor :id, :title, :body, :comments, :blog, :author
214
+ class PrimaryResource < BenchmarkModel
215
+ attr_accessor :id, :title, :body, :has_many_relationships, :virtual_attribute, :has_one_relationship
214
216
  end
215
217
 
216
- class Blog < BenchmarkModel
218
+ class VirtualAttribute < BenchmarkModel
217
219
  attr_accessor :id, :name
218
220
  end
219
221
  end