active_model_serializers 0.10.9 → 0.10.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -2
  3. data/lib/action_controller/serialization.rb +9 -1
  4. data/lib/active_model/serializer.rb +10 -2
  5. data/lib/active_model/serializer/version.rb +1 -1
  6. data/lib/active_model_serializers/adapter/attributes.rb +21 -0
  7. data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +1 -1
  8. metadata +12 -259
  9. data/.github/ISSUE_TEMPLATE.md +0 -29
  10. data/.github/PULL_REQUEST_TEMPLATE.md +0 -15
  11. data/.gitignore +0 -35
  12. data/.rubocop.yml +0 -109
  13. data/.simplecov +0 -110
  14. data/.travis.yml +0 -63
  15. data/CODE_OF_CONDUCT.md +0 -74
  16. data/CONTRIBUTING.md +0 -105
  17. data/Gemfile +0 -74
  18. data/Rakefile +0 -76
  19. data/active_model_serializers.gemspec +0 -64
  20. data/appveyor.yml +0 -28
  21. data/bin/bench +0 -171
  22. data/bin/bench_regression +0 -316
  23. data/bin/rubocop +0 -38
  24. data/bin/serve_benchmark +0 -39
  25. data/docs/README.md +0 -41
  26. data/docs/STYLE.md +0 -58
  27. data/docs/general/adapters.md +0 -269
  28. data/docs/general/caching.md +0 -58
  29. data/docs/general/configuration_options.md +0 -185
  30. data/docs/general/deserialization.md +0 -100
  31. data/docs/general/fields.md +0 -31
  32. data/docs/general/getting_started.md +0 -133
  33. data/docs/general/instrumentation.md +0 -40
  34. data/docs/general/key_transforms.md +0 -40
  35. data/docs/general/logging.md +0 -21
  36. data/docs/general/rendering.md +0 -293
  37. data/docs/general/serializers.md +0 -495
  38. data/docs/how-open-source-maintained.jpg +0 -0
  39. data/docs/howto/add_pagination_links.md +0 -138
  40. data/docs/howto/add_relationship_links.md +0 -140
  41. data/docs/howto/add_root_key.md +0 -62
  42. data/docs/howto/grape_integration.md +0 -42
  43. data/docs/howto/outside_controller_use.md +0 -66
  44. data/docs/howto/passing_arbitrary_options.md +0 -27
  45. data/docs/howto/serialize_poro.md +0 -73
  46. data/docs/howto/test.md +0 -154
  47. data/docs/howto/upgrade_from_0_8_to_0_10.md +0 -265
  48. data/docs/integrations/ember-and-json-api.md +0 -147
  49. data/docs/integrations/grape.md +0 -19
  50. data/docs/jsonapi/errors.md +0 -56
  51. data/docs/jsonapi/schema.md +0 -151
  52. data/docs/jsonapi/schema/schema.json +0 -366
  53. data/docs/rfcs/0000-namespace.md +0 -106
  54. data/docs/rfcs/template.md +0 -15
  55. data/test/action_controller/adapter_selector_test.rb +0 -64
  56. data/test/action_controller/explicit_serializer_test.rb +0 -137
  57. data/test/action_controller/json/include_test.rb +0 -248
  58. data/test/action_controller/json_api/deserialization_test.rb +0 -114
  59. data/test/action_controller/json_api/errors_test.rb +0 -42
  60. data/test/action_controller/json_api/fields_test.rb +0 -68
  61. data/test/action_controller/json_api/linked_test.rb +0 -204
  62. data/test/action_controller/json_api/pagination_test.rb +0 -126
  63. data/test/action_controller/json_api/transform_test.rb +0 -191
  64. data/test/action_controller/lookup_proc_test.rb +0 -51
  65. data/test/action_controller/namespace_lookup_test.rb +0 -234
  66. data/test/action_controller/serialization_scope_name_test.rb +0 -237
  67. data/test/action_controller/serialization_test.rb +0 -480
  68. data/test/active_model_serializers/adapter_for_test.rb +0 -210
  69. data/test/active_model_serializers/json_pointer_test.rb +0 -24
  70. data/test/active_model_serializers/logging_test.rb +0 -79
  71. data/test/active_model_serializers/model_test.rb +0 -144
  72. data/test/active_model_serializers/railtie_test_isolated.rb +0 -70
  73. data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +0 -163
  74. data/test/active_model_serializers/serialization_context_test_isolated.rb +0 -73
  75. data/test/active_model_serializers/test/schema_test.rb +0 -133
  76. data/test/active_model_serializers/test/serializer_test.rb +0 -64
  77. data/test/active_record_test.rb +0 -11
  78. data/test/adapter/attributes_test.rb +0 -42
  79. data/test/adapter/deprecation_test.rb +0 -102
  80. data/test/adapter/json/belongs_to_test.rb +0 -47
  81. data/test/adapter/json/collection_test.rb +0 -106
  82. data/test/adapter/json/has_many_test.rb +0 -55
  83. data/test/adapter/json/transform_test.rb +0 -95
  84. data/test/adapter/json_api/belongs_to_test.rb +0 -157
  85. data/test/adapter/json_api/collection_test.rb +0 -98
  86. data/test/adapter/json_api/errors_test.rb +0 -78
  87. data/test/adapter/json_api/fields_test.rb +0 -98
  88. data/test/adapter/json_api/has_many_explicit_serializer_test.rb +0 -98
  89. data/test/adapter/json_api/has_many_test.rb +0 -175
  90. data/test/adapter/json_api/has_one_test.rb +0 -82
  91. data/test/adapter/json_api/include_data_if_sideloaded_test.rb +0 -215
  92. data/test/adapter/json_api/json_api_test.rb +0 -35
  93. data/test/adapter/json_api/linked_test.rb +0 -415
  94. data/test/adapter/json_api/links_test.rb +0 -112
  95. data/test/adapter/json_api/pagination_links_test.rb +0 -208
  96. data/test/adapter/json_api/parse_test.rb +0 -139
  97. data/test/adapter/json_api/relationship_test.rb +0 -399
  98. data/test/adapter/json_api/resource_meta_test.rb +0 -102
  99. data/test/adapter/json_api/toplevel_jsonapi_test.rb +0 -84
  100. data/test/adapter/json_api/transform_test.rb +0 -514
  101. data/test/adapter/json_api/type_test.rb +0 -195
  102. data/test/adapter/json_test.rb +0 -48
  103. data/test/adapter/null_test.rb +0 -24
  104. data/test/adapter/polymorphic_test.rb +0 -220
  105. data/test/adapter_test.rb +0 -69
  106. data/test/array_serializer_test.rb +0 -24
  107. data/test/benchmark/app.rb +0 -67
  108. data/test/benchmark/benchmarking_support.rb +0 -69
  109. data/test/benchmark/bm_active_record.rb +0 -83
  110. data/test/benchmark/bm_adapter.rb +0 -40
  111. data/test/benchmark/bm_caching.rb +0 -121
  112. data/test/benchmark/bm_lookup_chain.rb +0 -85
  113. data/test/benchmark/bm_transform.rb +0 -47
  114. data/test/benchmark/config.ru +0 -3
  115. data/test/benchmark/controllers.rb +0 -85
  116. data/test/benchmark/fixtures.rb +0 -221
  117. data/test/cache_test.rb +0 -717
  118. data/test/collection_serializer_test.rb +0 -129
  119. data/test/fixtures/active_record.rb +0 -115
  120. data/test/fixtures/poro.rb +0 -227
  121. data/test/generators/scaffold_controller_generator_test.rb +0 -26
  122. data/test/generators/serializer_generator_test.rb +0 -77
  123. data/test/grape_test.rb +0 -198
  124. data/test/lint_test.rb +0 -51
  125. data/test/logger_test.rb +0 -22
  126. data/test/poro_test.rb +0 -11
  127. data/test/serializable_resource_test.rb +0 -81
  128. data/test/serializers/association_macros_test.rb +0 -39
  129. data/test/serializers/associations_test.rb +0 -520
  130. data/test/serializers/attribute_test.rb +0 -155
  131. data/test/serializers/attributes_test.rb +0 -54
  132. data/test/serializers/caching_configuration_test_isolated.rb +0 -172
  133. data/test/serializers/configuration_test.rb +0 -34
  134. data/test/serializers/fieldset_test.rb +0 -16
  135. data/test/serializers/meta_test.rb +0 -204
  136. data/test/serializers/options_test.rb +0 -34
  137. data/test/serializers/read_attribute_for_serialization_test.rb +0 -81
  138. data/test/serializers/reflection_test.rb +0 -481
  139. data/test/serializers/root_test.rb +0 -23
  140. data/test/serializers/serialization_test.rb +0 -57
  141. data/test/serializers/serializer_for_test.rb +0 -138
  142. data/test/serializers/serializer_for_with_namespace_test.rb +0 -90
  143. data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  144. data/test/support/isolated_unit.rb +0 -86
  145. data/test/support/rails5_shims.rb +0 -55
  146. data/test/support/rails_app.rb +0 -40
  147. data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +0 -6
  148. data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +0 -6
  149. data/test/support/schemas/custom/show.json +0 -7
  150. data/test/support/schemas/hyper_schema.json +0 -93
  151. data/test/support/schemas/render_using_json_api.json +0 -43
  152. data/test/support/schemas/simple_json_pointers.json +0 -10
  153. data/test/support/serialization_testing.rb +0 -81
  154. data/test/test_helper.rb +0 -72
@@ -1,129 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module ActiveModel
6
- class Serializer
7
- class CollectionSerializerTest < ActiveSupport::TestCase
8
- class SingularModel < ::Model; end
9
- class SingularModelSerializer < ActiveModel::Serializer
10
- end
11
- class HasManyModel < ::Model
12
- associations :singular_models
13
- end
14
- class HasManyModelSerializer < ActiveModel::Serializer
15
- has_many :singular_models
16
-
17
- def custom_options
18
- instance_options
19
- end
20
- end
21
- class MessagesSerializer < ActiveModel::Serializer
22
- type 'messages'
23
- end
24
-
25
- def setup
26
- @singular_model = SingularModel.new
27
- @has_many_model = HasManyModel.new
28
- @resource = build_named_collection @singular_model, @has_many_model
29
- @serializer = collection_serializer.new(@resource, some: :options)
30
- end
31
-
32
- def collection_serializer
33
- CollectionSerializer
34
- end
35
-
36
- def build_named_collection(*resource)
37
- resource.define_singleton_method(:name) { 'MeResource' }
38
- resource
39
- end
40
-
41
- def test_has_object_reader_serializer_interface
42
- assert_equal @serializer.object, @resource
43
- end
44
-
45
- def test_respond_to_each
46
- assert_respond_to @serializer, :each
47
- end
48
-
49
- def test_each_object_should_be_serialized_with_appropriate_serializer
50
- serializers = @serializer.to_a
51
-
52
- assert_kind_of SingularModelSerializer, serializers.first
53
- assert_kind_of SingularModel, serializers.first.object
54
-
55
- assert_kind_of HasManyModelSerializer, serializers.last
56
- assert_kind_of HasManyModel, serializers.last.object
57
-
58
- assert_equal :options, serializers.last.custom_options[:some]
59
- end
60
-
61
- def test_serializer_option_not_passed_to_each_serializer
62
- serializers = collection_serializer.new([@has_many_model], serializer: HasManyModelSerializer).to_a
63
-
64
- refute serializers.first.custom_options.key?(:serializer)
65
- end
66
-
67
- def test_root_default
68
- @serializer = collection_serializer.new([@singular_model, @has_many_model])
69
- assert_nil @serializer.root
70
- end
71
-
72
- def test_root
73
- expected = 'custom_root'
74
- @serializer = collection_serializer.new([@singular_model, @has_many_model], root: expected)
75
- assert_equal expected, @serializer.root
76
- end
77
-
78
- def test_root_with_no_serializers
79
- expected = 'custom_root'
80
- @serializer = collection_serializer.new([], root: expected)
81
- assert_equal expected, @serializer.root
82
- end
83
-
84
- def test_json_key_with_resource_with_serializer
85
- singular_key = @serializer.send(:serializers).first.json_key
86
- assert_equal singular_key.pluralize, @serializer.json_key
87
- end
88
-
89
- def test_json_key_with_resource_with_name_and_no_serializers
90
- serializer = collection_serializer.new(build_named_collection)
91
- assert_equal 'me_resources', serializer.json_key
92
- end
93
-
94
- def test_json_key_with_resource_with_nil_name_and_no_serializers
95
- resource = []
96
- resource.define_singleton_method(:name) { nil }
97
- serializer = collection_serializer.new(resource)
98
- assert_raise ArgumentError do
99
- serializer.json_key
100
- end
101
- end
102
-
103
- def test_json_key_with_resource_without_name_and_no_serializers
104
- serializer = collection_serializer.new([])
105
- assert_raise ArgumentError do
106
- serializer.json_key
107
- end
108
- end
109
-
110
- def test_json_key_with_empty_resources_with_serializer
111
- resource = []
112
- serializer = collection_serializer.new(resource, serializer: MessagesSerializer)
113
- assert_equal 'messages', serializer.json_key
114
- end
115
-
116
- def test_json_key_with_root
117
- expected = 'custom_root'
118
- serializer = collection_serializer.new(@resource, root: expected)
119
- assert_equal expected, serializer.json_key
120
- end
121
-
122
- def test_json_key_with_root_and_no_serializers
123
- expected = 'custom_root'
124
- serializer = collection_serializer.new(build_named_collection, root: expected)
125
- assert_equal expected, serializer.json_key
126
- end
127
- end
128
- end
129
- end
@@ -1,115 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record'
4
-
5
- ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
6
- ActiveRecord::Schema.define do
7
- self.verbose = false
8
- create_table :posts, force: true do |t|
9
- t.string :title
10
- t.text :body
11
- t.references :author
12
- t.timestamps null: false
13
- end
14
- create_table :authors, force: true do |t|
15
- t.string :name
16
- t.timestamps null: false
17
- end
18
- create_table :comments, force: true do |t|
19
- t.text :contents
20
- t.references :author
21
- t.references :post
22
- t.timestamp null: false
23
- end
24
- create_table :employees, force: true do |t|
25
- t.string :name
26
- t.string :email
27
- t.timestamp null: false
28
- end
29
- create_table :object_tags, force: true do |t|
30
- t.string :poly_tag_id
31
- t.string :taggable_type
32
- t.string :taggable_id
33
- t.timestamp null: false
34
- end
35
- create_table :poly_tags, force: true do |t|
36
- t.string :phrase
37
- t.timestamp null: false
38
- end
39
- create_table :pictures, force: true do |t|
40
- t.string :title
41
- t.string :imageable_type
42
- t.string :imageable_id
43
- t.timestamp null: false
44
- end
45
- end
46
-
47
- module ARModels
48
- class Post < ActiveRecord::Base
49
- has_many :comments
50
- belongs_to :author
51
- end
52
- class PostSerializer < ActiveModel::Serializer
53
- attributes :id, :title, :body
54
-
55
- has_many :comments
56
- belongs_to :author
57
- end
58
-
59
- class Comment < ActiveRecord::Base
60
- belongs_to :post
61
- belongs_to :author
62
- end
63
- class CommentSerializer < ActiveModel::Serializer
64
- attributes :id, :contents
65
-
66
- belongs_to :author
67
- end
68
-
69
- class Author < ActiveRecord::Base
70
- has_many :posts
71
- end
72
- class AuthorSerializer < ActiveModel::Serializer
73
- attributes :id, :name
74
-
75
- has_many :posts
76
- end
77
- end
78
-
79
- class Employee < ActiveRecord::Base
80
- has_many :pictures, as: :imageable
81
- has_many :object_tags, as: :taggable
82
- end
83
-
84
- class PolymorphicSimpleSerializer < ActiveModel::Serializer
85
- attributes :id
86
- end
87
-
88
- class ObjectTag < ActiveRecord::Base
89
- belongs_to :poly_tag
90
- belongs_to :taggable, polymorphic: true
91
- end
92
- class PolymorphicObjectTagSerializer < ActiveModel::Serializer
93
- attributes :id
94
- belongs_to :taggable, serializer: PolymorphicSimpleSerializer, polymorphic: true
95
- end
96
-
97
- class PolyTag < ActiveRecord::Base
98
- has_many :object_tags
99
- end
100
- class PolymorphicTagSerializer < ActiveModel::Serializer
101
- attributes :id, :phrase
102
- has_many :object_tags, serializer: PolymorphicObjectTagSerializer
103
- end
104
-
105
- class Picture < ActiveRecord::Base
106
- belongs_to :imageable, polymorphic: true
107
- has_many :object_tags, as: :taggable
108
- end
109
- class PolymorphicHasManySerializer < ActiveModel::Serializer
110
- attributes :id, :name
111
- end
112
- class PolymorphicBelongsToSerializer < ActiveModel::Serializer
113
- attributes :id, :title
114
- belongs_to :imageable, serializer: PolymorphicHasManySerializer, polymorphic: true
115
- end
@@ -1,227 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Model < ActiveModelSerializers::Model
4
- rand(2).zero? && derive_attributes_from_names_and_fix_accessors
5
-
6
- attr_writer :id
7
-
8
- # At this time, just for organization of intent
9
- class_attribute :association_names
10
- self.association_names = []
11
-
12
- def self.associations(*names)
13
- self.association_names |= names.map(&:to_sym)
14
- # Silence redefinition of methods warnings
15
- ActiveModelSerializers.silence_warnings do
16
- attr_accessor(*names)
17
- end
18
- end
19
-
20
- def associations
21
- association_names.each_with_object({}) do |association_name, result|
22
- result[association_name] = public_send(association_name).freeze
23
- end.with_indifferent_access.freeze
24
- end
25
-
26
- def attributes
27
- super.except(*association_names)
28
- end
29
- end
30
-
31
- # see
32
- # https://github.com/rails/rails/blob/4-2-stable/activemodel/lib/active_model/errors.rb
33
- # The below allows you to do:
34
- #
35
- # model = ModelWithErrors.new
36
- # model.validate! # => ["cannot be nil"]
37
- # model.errors.full_messages # => ["name cannot be nil"]
38
- class ModelWithErrors < Model
39
- attributes :name
40
- end
41
-
42
- class Profile < Model
43
- attributes :name, :description
44
- associations :comments
45
- end
46
- class ProfileSerializer < ActiveModel::Serializer
47
- attributes :name, :description
48
- end
49
- class ProfilePreviewSerializer < ActiveModel::Serializer
50
- attributes :name
51
- end
52
-
53
- class Author < Model
54
- attributes :name
55
- associations :posts, :bio, :roles, :comments
56
- end
57
- class AuthorSerializer < ActiveModel::Serializer
58
- cache key: 'writer', skip_digest: true
59
- attribute :id
60
- attribute :name
61
-
62
- has_many :posts
63
- has_many :roles
64
- has_one :bio
65
- end
66
- class AuthorPreviewSerializer < ActiveModel::Serializer
67
- attributes :id
68
- has_many :posts
69
- end
70
-
71
- class Comment < Model
72
- attributes :body, :date
73
- associations :post, :author, :likes
74
- end
75
- class CommentSerializer < ActiveModel::Serializer
76
- cache expires_in: 1.day, skip_digest: true
77
- attributes :id, :body
78
- belongs_to :post
79
- belongs_to :author
80
- end
81
- class CommentPreviewSerializer < ActiveModel::Serializer
82
- attributes :id
83
-
84
- belongs_to :post
85
- end
86
-
87
- class Post < Model
88
- attributes :title, :body
89
- associations :author, :comments, :blog, :tags, :related
90
- end
91
- class PostSerializer < ActiveModel::Serializer
92
- cache key: 'post', expires_in: 0.1, skip_digest: true
93
- attributes :id, :title, :body
94
-
95
- has_many :comments
96
- belongs_to :blog
97
- belongs_to :author
98
-
99
- def blog
100
- Blog.new(id: 999, name: 'Custom blog')
101
- end
102
- end
103
- class SpammyPostSerializer < ActiveModel::Serializer
104
- attributes :id
105
- has_many :related
106
- end
107
- class PostPreviewSerializer < ActiveModel::Serializer
108
- attributes :title, :body, :id
109
-
110
- has_many :comments, serializer: ::CommentPreviewSerializer
111
- belongs_to :author, serializer: ::AuthorPreviewSerializer
112
- end
113
- class PostWithCustomKeysSerializer < ActiveModel::Serializer
114
- attributes :id
115
- has_many :comments, key: :reviews
116
- belongs_to :author, key: :writer
117
- has_one :blog, key: :site
118
- end
119
-
120
- class Bio < Model
121
- attributes :content, :rating
122
- associations :author
123
- end
124
- class BioSerializer < ActiveModel::Serializer
125
- cache except: [:content], skip_digest: true
126
- attributes :id, :content, :rating
127
-
128
- belongs_to :author
129
- end
130
-
131
- class Blog < Model
132
- attributes :name, :type, :special_attribute
133
- associations :writer, :articles
134
- end
135
- class BlogSerializer < ActiveModel::Serializer
136
- cache key: 'blog'
137
- attributes :id, :name
138
-
139
- belongs_to :writer
140
- has_many :articles
141
- end
142
- class AlternateBlogSerializer < ActiveModel::Serializer
143
- attribute :id
144
- attribute :name, key: :title
145
- end
146
- class CustomBlogSerializer < ActiveModel::Serializer
147
- attribute :id
148
- attribute :special_attribute
149
- has_many :articles
150
- end
151
-
152
- class Role < Model
153
- attributes :name, :description, :special_attribute
154
- associations :author
155
- end
156
- class RoleSerializer < ActiveModel::Serializer
157
- cache only: [:name, :slug], skip_digest: true
158
- attributes :id, :name, :description
159
- attribute :friendly_id, key: :slug
160
- belongs_to :author
161
-
162
- def friendly_id
163
- "#{object.name}-#{object.id}"
164
- end
165
- end
166
-
167
- class Location < Model
168
- attributes :lat, :lng
169
- associations :place
170
- end
171
- class LocationSerializer < ActiveModel::Serializer
172
- cache only: [:address], skip_digest: true
173
- attributes :id, :lat, :lng
174
-
175
- belongs_to :place, key: :address
176
-
177
- def place
178
- 'Nowhere'
179
- end
180
- end
181
-
182
- class Place < Model
183
- attributes :name
184
- associations :locations
185
- end
186
- class PlaceSerializer < ActiveModel::Serializer
187
- attributes :id, :name
188
- has_many :locations
189
- end
190
-
191
- class Like < Model
192
- attributes :time
193
- associations :likeable
194
- end
195
- class LikeSerializer < ActiveModel::Serializer
196
- attributes :id, :time
197
- belongs_to :likeable
198
- end
199
-
200
- module Spam
201
- class UnrelatedLink < Model
202
- end
203
- class UnrelatedLinkSerializer < ActiveModel::Serializer
204
- cache only: [:id]
205
- attributes :id
206
- end
207
- end
208
-
209
- class VirtualValue < Model; end
210
- class VirtualValueSerializer < ActiveModel::Serializer
211
- attributes :id
212
- has_many :reviews, virtual_value: [{ type: 'reviews', id: '1' },
213
- { type: 'reviews', id: '2' }]
214
- has_one :maker, virtual_value: { type: 'makers', id: '1' }
215
-
216
- def reviews
217
- end
218
-
219
- def maker
220
- end
221
- end
222
-
223
- class PaginatedSerializer < ActiveModel::Serializer::CollectionSerializer
224
- def json_key
225
- 'paginated'
226
- end
227
- end