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.
- checksums.yaml +4 -4
- data/.rubocop.yml +6 -5
- data/.travis.yml +17 -5
- data/CHANGELOG.md +126 -2
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +5 -2
- data/README.md +166 -26
- data/Rakefile +3 -32
- data/active_model_serializers.gemspec +22 -25
- data/appveyor.yml +9 -3
- data/bin/rubocop +38 -0
- data/docs/README.md +2 -1
- data/docs/general/adapters.md +29 -11
- data/docs/general/caching.md +7 -1
- data/docs/general/configuration_options.md +70 -1
- data/docs/general/deserialization.md +1 -1
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +1 -1
- data/docs/general/logging.md +7 -0
- data/docs/general/rendering.md +62 -24
- data/docs/general/serializers.md +121 -13
- data/docs/howto/add_pagination_links.md +16 -17
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +4 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +12 -4
- data/docs/howto/passing_arbitrary_options.md +2 -2
- data/docs/howto/serialize_poro.md +46 -5
- data/docs/howto/test.md +2 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +67 -32
- data/docs/jsonapi/schema.md +1 -1
- data/lib/action_controller/serialization.rb +13 -3
- data/lib/active_model/serializer/adapter/base.rb +2 -0
- data/lib/active_model/serializer/array_serializer.rb +8 -5
- data/lib/active_model/serializer/association.rb +62 -10
- data/lib/active_model/serializer/belongs_to_reflection.rb +4 -3
- data/lib/active_model/serializer/collection_serializer.rb +35 -12
- data/lib/active_model/serializer/{caching.rb → concerns/caching.rb} +82 -115
- data/lib/active_model/serializer/error_serializer.rb +11 -7
- data/lib/active_model/serializer/errors_serializer.rb +25 -20
- data/lib/active_model/serializer/has_many_reflection.rb +3 -3
- data/lib/active_model/serializer/has_one_reflection.rb +1 -4
- data/lib/active_model/serializer/lazy_association.rb +95 -0
- data/lib/active_model/serializer/lint.rb +134 -130
- data/lib/active_model/serializer/reflection.rb +127 -67
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model/serializer.rb +296 -79
- data/lib/active_model_serializers/adapter/attributes.rb +3 -66
- data/lib/active_model_serializers/adapter/base.rb +39 -39
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/link.rb +1 -1
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +8 -1
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +63 -23
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +32 -9
- data/lib/active_model_serializers/adapter/json_api.rb +71 -57
- data/lib/active_model_serializers/adapter.rb +6 -0
- data/lib/active_model_serializers/deprecate.rb +1 -2
- data/lib/active_model_serializers/deserialization.rb +2 -0
- data/lib/active_model_serializers/lookup_chain.rb +80 -0
- data/lib/active_model_serializers/model.rb +109 -28
- data/lib/active_model_serializers/railtie.rb +3 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +44 -31
- data/lib/active_model_serializers/serializable_resource.rb +6 -5
- data/lib/active_model_serializers/serialization_context.rb +10 -3
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/active_model_serializers.rb +15 -0
- data/lib/generators/rails/resource_override.rb +1 -1
- data/lib/generators/rails/serializer_generator.rb +4 -4
- data/lib/grape/active_model_serializers.rb +7 -5
- data/lib/grape/formatters/active_model_serializers.rb +19 -2
- data/lib/grape/helpers/active_model_serializers.rb +1 -0
- data/lib/tasks/rubocop.rake +53 -0
- data/test/action_controller/adapter_selector_test.rb +14 -5
- data/test/action_controller/explicit_serializer_test.rb +5 -4
- data/test/action_controller/json/include_test.rb +106 -27
- data/test/action_controller/json_api/errors_test.rb +8 -9
- data/test/action_controller/json_api/fields_test.rb +66 -0
- data/test/action_controller/json_api/linked_test.rb +29 -24
- data/test/action_controller/json_api/pagination_test.rb +19 -19
- data/test/action_controller/json_api/transform_test.rb +11 -3
- data/test/action_controller/lookup_proc_test.rb +49 -0
- data/test/action_controller/namespace_lookup_test.rb +232 -0
- data/test/action_controller/serialization_scope_name_test.rb +12 -6
- data/test/action_controller/serialization_test.rb +12 -9
- data/test/active_model_serializers/json_pointer_test.rb +15 -13
- data/test/active_model_serializers/model_test.rb +137 -4
- data/test/active_model_serializers/railtie_test_isolated.rb +12 -7
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +161 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +23 -10
- data/test/active_model_serializers/test/schema_test.rb +3 -2
- data/test/adapter/attributes_test.rb +40 -0
- data/test/adapter/json/collection_test.rb +14 -0
- data/test/adapter/json/has_many_test.rb +10 -2
- data/test/adapter/json/transform_test.rb +15 -15
- data/test/adapter/json_api/collection_test.rb +4 -3
- data/test/adapter/json_api/errors_test.rb +17 -19
- data/test/adapter/json_api/fields_test.rb +12 -3
- data/test/adapter/json_api/has_many_test.rb +49 -20
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +183 -0
- data/test/adapter/json_api/json_api_test.rb +5 -7
- data/test/adapter/json_api/linked_test.rb +33 -12
- data/test/adapter/json_api/links_test.rb +4 -2
- data/test/adapter/json_api/pagination_links_test.rb +35 -8
- data/test/adapter/json_api/relationship_test.rb +309 -73
- data/test/adapter/json_api/resource_identifier_test.rb +27 -2
- data/test/adapter/json_api/resource_meta_test.rb +3 -3
- data/test/adapter/json_api/transform_test.rb +263 -253
- data/test/adapter/json_api/type_test.rb +1 -1
- data/test/adapter/json_test.rb +8 -7
- data/test/adapter/null_test.rb +1 -2
- data/test/adapter/polymorphic_test.rb +5 -5
- data/test/adapter_test.rb +1 -1
- data/test/benchmark/app.rb +1 -1
- data/test/benchmark/benchmarking_support.rb +1 -1
- data/test/benchmark/bm_active_record.rb +81 -0
- data/test/benchmark/bm_adapter.rb +38 -0
- data/test/benchmark/bm_caching.rb +16 -16
- data/test/benchmark/bm_lookup_chain.rb +83 -0
- data/test/benchmark/bm_transform.rb +21 -10
- data/test/benchmark/controllers.rb +16 -17
- data/test/benchmark/fixtures.rb +72 -72
- data/test/cache_test.rb +235 -69
- data/test/collection_serializer_test.rb +25 -12
- data/test/fixtures/active_record.rb +45 -10
- data/test/fixtures/poro.rb +124 -181
- data/test/generators/serializer_generator_test.rb +23 -5
- data/test/grape_test.rb +170 -56
- data/test/lint_test.rb +1 -1
- data/test/logger_test.rb +13 -11
- data/test/serializable_resource_test.rb +18 -22
- data/test/serializers/association_macros_test.rb +3 -2
- data/test/serializers/associations_test.rb +178 -49
- data/test/serializers/attribute_test.rb +5 -3
- data/test/serializers/attributes_test.rb +1 -1
- data/test/serializers/caching_configuration_test_isolated.rb +6 -6
- data/test/serializers/fieldset_test.rb +1 -1
- data/test/serializers/meta_test.rb +12 -6
- data/test/serializers/options_test.rb +17 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +3 -3
- data/test/serializers/reflection_test.rb +427 -0
- data/test/serializers/root_test.rb +1 -1
- data/test/serializers/serialization_test.rb +2 -2
- data/test/serializers/serializer_for_test.rb +12 -10
- data/test/serializers/serializer_for_with_namespace_test.rb +88 -0
- data/test/support/isolated_unit.rb +5 -2
- data/test/support/rails5_shims.rb +8 -2
- data/test/support/rails_app.rb +2 -9
- data/test/support/serialization_testing.rb +23 -5
- data/test/test_helper.rb +13 -0
- metadata +105 -42
- data/.rubocop_todo.yml +0 -167
- data/docs/ARCHITECTURE.md +0 -126
- data/lib/active_model/serializer/associations.rb +0 -100
- data/lib/active_model/serializer/attributes.rb +0 -82
- data/lib/active_model/serializer/collection_reflection.rb +0 -7
- data/lib/active_model/serializer/configuration.rb +0 -35
- data/lib/active_model/serializer/include_tree.rb +0 -111
- data/lib/active_model/serializer/links.rb +0 -35
- data/lib/active_model/serializer/meta.rb +0 -29
- data/lib/active_model/serializer/singular_reflection.rb +0 -7
- data/lib/active_model/serializer/type.rb +0 -25
- data/lib/active_model_serializers/key_transform.rb +0 -70
- data/test/active_model_serializers/key_transform_test.rb +0 -263
- data/test/adapter/json_api/relationships_test.rb +0 -199
- data/test/include_tree/from_include_args_test.rb +0 -26
- data/test/include_tree/from_string_test.rb +0 -94
- data/test/include_tree/include_args_to_hash_test.rb +0 -64
data/test/benchmark/fixtures.rb
CHANGED
@@ -1,33 +1,33 @@
|
|
1
1
|
Rails.configuration.serializers = []
|
2
|
-
class
|
2
|
+
class HasOneRelationshipSerializer < ActiveModel::Serializer
|
3
3
|
attributes :id, :first_name, :last_name
|
4
4
|
|
5
|
-
has_many :
|
5
|
+
has_many :primary_resources, embed: :ids
|
6
6
|
has_one :bio
|
7
7
|
end
|
8
|
-
Rails.configuration.serializers <<
|
8
|
+
Rails.configuration.serializers << HasOneRelationshipSerializer
|
9
9
|
|
10
|
-
class
|
10
|
+
class VirtualAttributeSerializer < ActiveModel::Serializer
|
11
11
|
attributes :id, :name
|
12
12
|
end
|
13
|
-
Rails.configuration.serializers <<
|
13
|
+
Rails.configuration.serializers << VirtualAttributeSerializer
|
14
14
|
|
15
|
-
class
|
16
|
-
attributes :id, :body
|
15
|
+
class HasManyRelationshipSerializer < ActiveModel::Serializer
|
16
|
+
attributes :id, :body
|
17
17
|
|
18
|
-
belongs_to :
|
19
|
-
belongs_to :
|
18
|
+
belongs_to :primary_resource
|
19
|
+
belongs_to :has_one_relationship
|
20
20
|
end
|
21
|
-
Rails.configuration.serializers <<
|
21
|
+
Rails.configuration.serializers << HasManyRelationshipSerializer
|
22
22
|
|
23
|
-
class
|
23
|
+
class PrimaryResourceSerializer < ActiveModel::Serializer
|
24
24
|
attributes :id, :title, :body
|
25
25
|
|
26
|
-
has_many :
|
27
|
-
belongs_to :
|
28
|
-
belongs_to :
|
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(:
|
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
|
40
|
-
|
39
|
+
def virtual_attribute
|
40
|
+
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
41
41
|
end
|
42
42
|
end
|
43
|
-
Rails.configuration.serializers <<
|
43
|
+
Rails.configuration.serializers << PrimaryResourceSerializer
|
44
44
|
|
45
|
-
class
|
45
|
+
class CachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
|
46
46
|
cache key: 'writer', skip_digest: true
|
47
47
|
end
|
48
|
-
Rails.configuration.serializers <<
|
48
|
+
Rails.configuration.serializers << CachingHasOneRelationshipSerializer
|
49
49
|
|
50
|
-
class
|
50
|
+
class CachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
|
51
51
|
cache expires_in: 1.day, skip_digest: true
|
52
52
|
end
|
53
|
-
Rails.configuration.serializers <<
|
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
|
57
|
-
cache key: '
|
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 :
|
62
|
-
belongs_to :
|
63
|
-
has_many :
|
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(:
|
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
|
75
|
-
|
74
|
+
def virtual_attribute
|
75
|
+
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
76
76
|
end
|
77
77
|
end
|
78
|
-
Rails.configuration.serializers <<
|
78
|
+
Rails.configuration.serializers << CachingPrimaryResourceSerializer
|
79
79
|
|
80
|
-
class
|
80
|
+
class FragmentCachingHasOneRelationshipSerializer < HasOneRelationshipSerializer
|
81
81
|
cache key: 'writer', only: [:first_name, :last_name], skip_digest: true
|
82
82
|
end
|
83
|
-
Rails.configuration.serializers <<
|
83
|
+
Rails.configuration.serializers << FragmentCachingHasOneRelationshipSerializer
|
84
84
|
|
85
|
-
class
|
86
|
-
cache expires_in: 1.day, except: [:
|
85
|
+
class FragmentCachingHasManyRelationshipSerializer < HasManyRelationshipSerializer
|
86
|
+
cache expires_in: 1.day, except: [:body], skip_digest: true
|
87
87
|
end
|
88
|
-
Rails.configuration.serializers <<
|
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
|
92
|
-
cache key: '
|
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 :
|
97
|
-
belongs_to :
|
98
|
-
has_many :
|
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(:
|
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
|
110
|
-
|
109
|
+
def virtual_attribute
|
110
|
+
VirtualAttribute.new(id: 999, name: 'Free-Range Virtual Attribute')
|
111
111
|
end
|
112
112
|
end
|
113
|
-
Rails.configuration.serializers <<
|
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 :
|
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 :
|
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 :
|
131
|
+
create_table :primary_resources, force: true do |t|
|
132
132
|
t.string :title
|
133
133
|
t.text :body
|
134
|
-
t.references :
|
135
|
-
t.references :
|
134
|
+
t.references :has_one_relationship
|
135
|
+
t.references :virtual_attribute
|
136
136
|
t.timestamps null: false
|
137
137
|
end
|
138
|
-
create_table :
|
138
|
+
create_table :has_many_relationships, force: true do |t|
|
139
139
|
t.text :body
|
140
|
-
t.references :
|
141
|
-
t.references :
|
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
|
147
|
-
belongs_to :
|
148
|
-
belongs_to :
|
146
|
+
class HasManyRelationship < ActiveRecord::Base
|
147
|
+
belongs_to :has_one_relationship
|
148
|
+
belongs_to :primary_resource
|
149
149
|
end
|
150
150
|
|
151
|
-
class
|
152
|
-
has_many :
|
153
|
-
has_many :
|
151
|
+
class HasOneRelationship < ActiveRecord::Base
|
152
|
+
has_many :primary_resources
|
153
|
+
has_many :has_many_relationships
|
154
154
|
end
|
155
155
|
|
156
|
-
class
|
157
|
-
has_many :
|
158
|
-
belongs_to :
|
159
|
-
belongs_to :
|
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
|
163
|
-
has_many :
|
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
|
205
|
-
attr_accessor :id, :body
|
204
|
+
class HasManyRelationship < BenchmarkModel
|
205
|
+
attr_accessor :id, :body
|
206
206
|
end
|
207
207
|
|
208
|
-
class
|
209
|
-
attr_accessor :id, :first_name, :last_name, :
|
208
|
+
class HasOneRelationship < BenchmarkModel
|
209
|
+
attr_accessor :id, :first_name, :last_name, :primary_resources
|
210
210
|
end
|
211
211
|
|
212
|
-
class
|
213
|
-
attr_accessor :id, :title, :body, :
|
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
|
216
|
+
class VirtualAttribute < BenchmarkModel
|
217
217
|
attr_accessor :id, :name
|
218
218
|
end
|
219
219
|
end
|