active_model_serializers 0.10.0 → 0.10.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +6 -5
  3. data/.travis.yml +17 -5
  4. data/CHANGELOG.md +126 -2
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +5 -2
  7. data/README.md +166 -26
  8. data/Rakefile +3 -32
  9. data/active_model_serializers.gemspec +22 -25
  10. data/appveyor.yml +9 -3
  11. data/bin/rubocop +38 -0
  12. data/docs/README.md +2 -1
  13. data/docs/general/adapters.md +29 -11
  14. data/docs/general/caching.md +7 -1
  15. data/docs/general/configuration_options.md +70 -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 +62 -24
  21. data/docs/general/serializers.md +121 -13
  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 +4 -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 +13 -3
  34. data/lib/active_model/serializer/adapter/base.rb +2 -0
  35. data/lib/active_model/serializer/array_serializer.rb +8 -5
  36. data/lib/active_model/serializer/association.rb +62 -10
  37. data/lib/active_model/serializer/belongs_to_reflection.rb +4 -3
  38. data/lib/active_model/serializer/collection_serializer.rb +35 -12
  39. data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +82 -115
  40. data/lib/active_model/serializer/error_serializer.rb +11 -7
  41. data/lib/active_model/serializer/errors_serializer.rb +25 -20
  42. data/lib/active_model/serializer/has_many_reflection.rb +3 -3
  43. data/lib/active_model/serializer/has_one_reflection.rb +1 -4
  44. data/lib/active_model/serializer/lazy_association.rb +95 -0
  45. data/lib/active_model/serializer/lint.rb +134 -130
  46. data/lib/active_model/serializer/reflection.rb +127 -67
  47. data/lib/active_model/serializer/version.rb +1 -1
  48. data/lib/active_model/serializer.rb +296 -79
  49. data/lib/active_model_serializers/adapter/attributes.rb +3 -66
  50. data/lib/active_model_serializers/adapter/base.rb +39 -39
  51. data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
  52. data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
  53. data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
  54. data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -23
  55. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +32 -9
  56. data/lib/active_model_serializers/adapter/json_api.rb +71 -57
  57. data/lib/active_model_serializers/adapter.rb +6 -0
  58. data/lib/active_model_serializers/deprecate.rb +1 -2
  59. data/lib/active_model_serializers/deserialization.rb +2 -0
  60. data/lib/active_model_serializers/lookup_chain.rb +80 -0
  61. data/lib/active_model_serializers/model.rb +109 -28
  62. data/lib/active_model_serializers/railtie.rb +3 -1
  63. data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
  64. data/lib/active_model_serializers/serializable_resource.rb +6 -5
  65. data/lib/active_model_serializers/serialization_context.rb +10 -3
  66. data/lib/active_model_serializers/test/schema.rb +2 -2
  67. data/lib/active_model_serializers.rb +15 -0
  68. data/lib/generators/rails/resource_override.rb +1 -1
  69. data/lib/generators/rails/serializer_generator.rb +4 -4
  70. data/lib/grape/active_model_serializers.rb +7 -5
  71. data/lib/grape/formatters/active_model_serializers.rb +19 -2
  72. data/lib/grape/helpers/active_model_serializers.rb +1 -0
  73. data/lib/tasks/rubocop.rake +53 -0
  74. data/test/action_controller/adapter_selector_test.rb +14 -5
  75. data/test/action_controller/explicit_serializer_test.rb +5 -4
  76. data/test/action_controller/json/include_test.rb +106 -27
  77. data/test/action_controller/json_api/errors_test.rb +8 -9
  78. data/test/action_controller/json_api/fields_test.rb +66 -0
  79. data/test/action_controller/json_api/linked_test.rb +29 -24
  80. data/test/action_controller/json_api/pagination_test.rb +19 -19
  81. data/test/action_controller/json_api/transform_test.rb +11 -3
  82. data/test/action_controller/lookup_proc_test.rb +49 -0
  83. data/test/action_controller/namespace_lookup_test.rb +232 -0
  84. data/test/action_controller/serialization_scope_name_test.rb +12 -6
  85. data/test/action_controller/serialization_test.rb +12 -9
  86. data/test/active_model_serializers/json_pointer_test.rb +15 -13
  87. data/test/active_model_serializers/model_test.rb +137 -4
  88. data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
  89. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
  90. data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
  91. data/test/active_model_serializers/test/schema_test.rb +3 -2
  92. data/test/adapter/attributes_test.rb +40 -0
  93. data/test/adapter/json/collection_test.rb +14 -0
  94. data/test/adapter/json/has_many_test.rb +10 -2
  95. data/test/adapter/json/transform_test.rb +15 -15
  96. data/test/adapter/json_api/collection_test.rb +4 -3
  97. data/test/adapter/json_api/errors_test.rb +17 -19
  98. data/test/adapter/json_api/fields_test.rb +12 -3
  99. data/test/adapter/json_api/has_many_test.rb +49 -20
  100. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
  101. data/test/adapter/json_api/json_api_test.rb +5 -7
  102. data/test/adapter/json_api/linked_test.rb +33 -12
  103. data/test/adapter/json_api/links_test.rb +4 -2
  104. data/test/adapter/json_api/pagination_links_test.rb +35 -8
  105. data/test/adapter/json_api/relationship_test.rb +309 -73
  106. data/test/adapter/json_api/resource_identifier_test.rb +27 -2
  107. data/test/adapter/json_api/resource_meta_test.rb +3 -3
  108. data/test/adapter/json_api/transform_test.rb +263 -253
  109. data/test/adapter/json_api/type_test.rb +1 -1
  110. data/test/adapter/json_test.rb +8 -7
  111. data/test/adapter/null_test.rb +1 -2
  112. data/test/adapter/polymorphic_test.rb +5 -5
  113. data/test/adapter_test.rb +1 -1
  114. data/test/benchmark/app.rb +1 -1
  115. data/test/benchmark/benchmarking_support.rb +1 -1
  116. data/test/benchmark/bm_active_record.rb +81 -0
  117. data/test/benchmark/bm_adapter.rb +38 -0
  118. data/test/benchmark/bm_caching.rb +16 -16
  119. data/test/benchmark/bm_lookup_chain.rb +83 -0
  120. data/test/benchmark/bm_transform.rb +21 -10
  121. data/test/benchmark/controllers.rb +16 -17
  122. data/test/benchmark/fixtures.rb +72 -72
  123. data/test/cache_test.rb +235 -69
  124. data/test/collection_serializer_test.rb +25 -12
  125. data/test/fixtures/active_record.rb +45 -10
  126. data/test/fixtures/poro.rb +124 -181
  127. data/test/generators/serializer_generator_test.rb +23 -5
  128. data/test/grape_test.rb +170 -56
  129. data/test/lint_test.rb +1 -1
  130. data/test/logger_test.rb +13 -11
  131. data/test/serializable_resource_test.rb +18 -22
  132. data/test/serializers/association_macros_test.rb +3 -2
  133. data/test/serializers/associations_test.rb +178 -49
  134. data/test/serializers/attribute_test.rb +5 -3
  135. data/test/serializers/attributes_test.rb +1 -1
  136. data/test/serializers/caching_configuration_test_isolated.rb +6 -6
  137. data/test/serializers/fieldset_test.rb +1 -1
  138. data/test/serializers/meta_test.rb +12 -6
  139. data/test/serializers/options_test.rb +17 -6
  140. data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
  141. data/test/serializers/reflection_test.rb +427 -0
  142. data/test/serializers/root_test.rb +1 -1
  143. data/test/serializers/serialization_test.rb +2 -2
  144. data/test/serializers/serializer_for_test.rb +12 -10
  145. data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
  146. data/test/support/isolated_unit.rb +5 -2
  147. data/test/support/rails5_shims.rb +8 -2
  148. data/test/support/rails_app.rb +2 -9
  149. data/test/support/serialization_testing.rb +23 -5
  150. data/test/test_helper.rb +13 -0
  151. metadata +105 -42
  152. data/.rubocop_todo.yml +0 -167
  153. data/docs/ARCHITECTURE.md +0 -126
  154. data/lib/active_model/serializer/associations.rb +0 -100
  155. data/lib/active_model/serializer/attributes.rb +0 -82
  156. data/lib/active_model/serializer/collection_reflection.rb +0 -7
  157. data/lib/active_model/serializer/configuration.rb +0 -35
  158. data/lib/active_model/serializer/include_tree.rb +0 -111
  159. data/lib/active_model/serializer/links.rb +0 -35
  160. data/lib/active_model/serializer/meta.rb +0 -29
  161. data/lib/active_model/serializer/singular_reflection.rb +0 -7
  162. data/lib/active_model/serializer/type.rb +0 -25
  163. data/lib/active_model_serializers/key_transform.rb +0 -70
  164. data/test/active_model_serializers/key_transform_test.rb +0 -263
  165. data/test/adapter/json_api/relationships_test.rb +0 -199
  166. data/test/include_tree/from_include_args_test.rb +0 -26
  167. data/test/include_tree/from_string_test.rb +0 -94
  168. data/test/include_tree/include_args_to_hash_test.rb +0 -64
@@ -1,33 +1,33 @@
1
1
  Rails.configuration.serializers = []
2
- class AuthorSerializer < ActiveModel::Serializer
2
+ class HasOneRelationshipSerializer < ActiveModel::Serializer
3
3
  attributes :id, :first_name, :last_name
4
4
 
5
- has_many :posts, embed: :ids
5
+ has_many :primary_resources, embed: :ids
6
6
  has_one :bio
7
7
  end
8
- Rails.configuration.serializers << AuthorSerializer
8
+ Rails.configuration.serializers << HasOneRelationshipSerializer
9
9
 
10
- class BlogSerializer < ActiveModel::Serializer
10
+ class VirtualAttributeSerializer < ActiveModel::Serializer
11
11
  attributes :id, :name
12
12
  end
13
- Rails.configuration.serializers << BlogSerializer
13
+ Rails.configuration.serializers << VirtualAttributeSerializer
14
14
 
15
- class CommentSerializer < ActiveModel::Serializer
16
- attributes :id, :body, :updated_at
15
+ class HasManyRelationshipSerializer < ActiveModel::Serializer
16
+ attributes :id, :body
17
17
 
18
- belongs_to :post
19
- belongs_to :author
18
+ belongs_to :primary_resource
19
+ belongs_to :has_one_relationship
20
20
  end
21
- Rails.configuration.serializers << CommentSerializer
21
+ Rails.configuration.serializers << HasManyRelationshipSerializer
22
22
 
23
- class PostSerializer < ActiveModel::Serializer
23
+ class PrimaryResourceSerializer < ActiveModel::Serializer
24
24
  attributes :id, :title, :body
25
25
 
26
- has_many :comments, serializer: CommentSerializer
27
- belongs_to :blog, serializer: BlogSerializer
28
- belongs_to :author, serializer: AuthorSerializer
26
+ has_many :has_many_relationships, serializer: HasManyRelationshipSerializer
27
+ belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
28
+ belongs_to :has_one_relationship, serializer: HasOneRelationshipSerializer
29
29
 
30
- link(:post_authors) { 'https://example.com/post_authors' }
30
+ link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
31
31
 
32
32
  meta do
33
33
  {
@@ -36,33 +36,33 @@ class PostSerializer < ActiveModel::Serializer
36
36
  }
37
37
  end
38
38
 
39
- def blog
40
- Blog.new(id: 999, name: 'Custom blog')
39
+ def virtual_attribute
40
+ VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
41
41
  end
42
42
  end
43
- Rails.configuration.serializers << PostSerializer
43
+ Rails.configuration.serializers << PrimaryResourceSerializer
44
44
 
45
- class CachingAuthorSerializer < AuthorSerializer
45
+ class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
46
46
  cache key: 'writer', skip_digest: true
47
47
  end
48
- Rails.configuration.serializers << CachingAuthorSerializer
48
+ Rails.configuration.serializers << CachingHasOneRelationshipSerializer
49
49
 
50
- class CachingCommentSerializer < CommentSerializer
50
+ class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
51
51
  cache expires_in: 1.day, skip_digest: true
52
52
  end
53
- Rails.configuration.serializers << CachingCommentSerializer
53
+ Rails.configuration.serializers << CachingHasManyRelationshipSerializer
54
54
 
55
55
  # 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
56
+ class CachingPrimaryResourceSerializer < ActiveModel::Serializer
57
+ cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
58
58
 
59
59
  attributes :id, :title, :body
60
60
 
61
- belongs_to :blog, serializer: BlogSerializer
62
- belongs_to :author, serializer: CachingAuthorSerializer
63
- has_many :comments, serializer: CachingCommentSerializer
61
+ belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
62
+ belongs_to :has_one_relationship, serializer: CachingHasOneRelationshipSerializer
63
+ has_many :has_many_relationships, serializer: CachingHasManyRelationshipSerializer
64
64
 
65
- link(:post_authors) { 'https://example.com/post_authors' }
65
+ link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
66
66
 
67
67
  meta do
68
68
  {
@@ -71,33 +71,33 @@ class CachingPostSerializer < ActiveModel::Serializer
71
71
  }
72
72
  end
73
73
 
74
- def blog
75
- Blog.new(id: 999, name: 'Custom blog')
74
+ def virtual_attribute
75
+ VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
76
76
  end
77
77
  end
78
- Rails.configuration.serializers << CachingPostSerializer
78
+ Rails.configuration.serializers << CachingPrimaryResourceSerializer
79
79
 
80
- class FragmentCachingAuthorSerializer < AuthorSerializer
80
+ class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
81
81
  cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
82
82
  end
83
- Rails.configuration.serializers << FragmentCachingAuthorSerializer
83
+ Rails.configuration.serializers << FragmentCachingHasOneRelationshipSerializer
84
84
 
85
- class FragmentCachingCommentSerializer < CommentSerializer
86
- cache expires_in: 1.day, except: [:updated_at], skip_digest: true
85
+ class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
86
+ cache expires_in: 1.day, except: [:body], skip_digest: true
87
87
  end
88
- Rails.configuration.serializers << CachingCommentSerializer
88
+ Rails.configuration.serializers << CachingHasManyRelationshipSerializer
89
89
 
90
90
  # 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
91
+ class FragmentCachingPrimaryResourceSerializer < ActiveModel::Serializer
92
+ cache key: 'primary_resource', expires_in: 0.1, skip_digest: true
93
93
 
94
94
  attributes :id, :title, :body
95
95
 
96
- belongs_to :blog, serializer: BlogSerializer
97
- belongs_to :author, serializer: FragmentCachingAuthorSerializer
98
- has_many :comments, serializer: FragmentCachingCommentSerializer
96
+ belongs_to :virtual_attribute, serializer: VirtualAttributeSerializer
97
+ belongs_to :has_one_relationship, serializer: FragmentCachingHasOneRelationshipSerializer
98
+ has_many :has_many_relationships, serializer: FragmentCachingHasManyRelationshipSerializer
99
99
 
100
- link(:post_authors) { 'https://example.com/post_authors' }
100
+ link(:primary_resource_has_one_relationships) { 'https://example.com/primary_resource_has_one_relationships' }
101
101
 
102
102
  meta do
103
103
  {
@@ -106,11 +106,11 @@ class FragmentCachingPostSerializer < ActiveModel::Serializer
106
106
  }
107
107
  end
108
108
 
109
- def blog
110
- Blog.new(id: 999, name: 'Custom blog')
109
+ def virtual_attribute
110
+ VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
111
111
  end
112
112
  end
113
- Rails.configuration.serializers << FragmentCachingPostSerializer
113
+ Rails.configuration.serializers << FragmentCachingPrimaryResourceSerializer
114
114
 
115
115
  if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
116
116
  require 'active_record'
@@ -119,48 +119,48 @@ if ENV['ENABLE_ACTIVE_RECORD'] == 'true'
119
119
  ActiveRecord::Schema.define do
120
120
  self.verbose = false
121
121
 
122
- create_table :blogs, force: true do |t|
122
+ create_table :virtual_attributes, force: true do |t|
123
123
  t.string :name
124
124
  t.timestamps null: false
125
125
  end
126
- create_table :authors, force: true do |t|
126
+ create_table :has_one_relationships, force: true do |t|
127
127
  t.string :first_name
128
128
  t.string :last_name
129
129
  t.timestamps null: false
130
130
  end
131
- create_table :posts, force: true do |t|
131
+ create_table :primary_resources, force: true do |t|
132
132
  t.string :title
133
133
  t.text :body
134
- t.references :author
135
- t.references :blog
134
+ t.references :has_one_relationship
135
+ t.references :virtual_attribute
136
136
  t.timestamps null: false
137
137
  end
138
- create_table :comments, force: true do |t|
138
+ create_table :has_many_relationships, force: true do |t|
139
139
  t.text :body
140
- t.references :author
141
- t.references :post
140
+ t.references :has_one_relationship
141
+ t.references :primary_resource
142
142
  t.timestamps null: false
143
143
  end
144
144
  end
145
145
 
146
- class Comment < ActiveRecord::Base
147
- belongs_to :author
148
- belongs_to :post
146
+ class HasManyRelationship < ActiveRecord::Base
147
+ belongs_to :has_one_relationship
148
+ belongs_to :primary_resource
149
149
  end
150
150
 
151
- class Author < ActiveRecord::Base
152
- has_many :posts
153
- has_many :comments
151
+ class HasOneRelationship < ActiveRecord::Base
152
+ has_many :primary_resources
153
+ has_many :has_many_relationships
154
154
  end
155
155
 
156
- class Post < ActiveRecord::Base
157
- has_many :comments
158
- belongs_to :author
159
- belongs_to :blog
156
+ class PrimaryResource < ActiveRecord::Base
157
+ has_many :has_many_relationships
158
+ belongs_to :has_one_relationship
159
+ belongs_to :virtual_attribute
160
160
  end
161
161
 
162
- class Blog < ActiveRecord::Base
163
- has_many :posts
162
+ class VirtualAttribute < ActiveRecord::Base
163
+ has_many :primary_resources
164
164
  end
165
165
  else
166
166
  # ActiveModelSerializers::Model is a convenient
@@ -201,19 +201,19 @@ else
201
201
  end
202
202
  end
203
203
 
204
- class Comment < BenchmarkModel
205
- attr_accessor :id, :body, :updated_at
204
+ class HasManyRelationship < BenchmarkModel
205
+ attr_accessor :id, :body
206
206
  end
207
207
 
208
- class Author < BenchmarkModel
209
- attr_accessor :id, :first_name, :last_name, :posts
208
+ class HasOneRelationship < BenchmarkModel
209
+ attr_accessor :id, :first_name, :last_name, :primary_resources
210
210
  end
211
211
 
212
- class Post < BenchmarkModel
213
- attr_accessor :id, :title, :body, :comments, :blog, :author
212
+ class PrimaryResource < BenchmarkModel
213
+ attr_accessor :id, :title, :body, :has_many_relationships, :virtual_attribute, :has_one_relationship
214
214
  end
215
215
 
216
- class Blog < BenchmarkModel
216
+ class VirtualAttribute < BenchmarkModel
217
217
  attr_accessor :id, :name
218
218
  end
219
219
  end