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.
- checksums.yaml +4 -4
- data/.gitignore +6 -0
- data/.rubocop.yml +86 -0
- data/.rubocop_todo.yml +240 -0
- data/.simplecov +111 -0
- data/.travis.yml +33 -22
- data/CHANGELOG.md +358 -6
- data/CONTRIBUTING.md +220 -0
- data/Gemfile +46 -1
- data/{MIT-LICENSE.txt → LICENSE.txt} +3 -2
- data/README.md +81 -591
- data/Rakefile +68 -11
- data/active_model_serializers.gemspec +57 -23
- data/appveyor.yml +27 -0
- data/docs/ARCHITECTURE.md +120 -0
- data/docs/DESIGN.textile +8 -0
- data/docs/README.md +35 -0
- data/docs/general/adapters.md +162 -0
- data/docs/general/caching.md +52 -0
- data/docs/general/configuration_options.md +27 -0
- data/docs/general/getting_started.md +98 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/logging.md +14 -0
- data/docs/general/rendering.md +153 -0
- data/docs/general/serializers.md +207 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +121 -0
- data/docs/howto/add_root_key.md +51 -0
- data/docs/howto/outside_controller_use.md +58 -0
- data/docs/howto/test.md +152 -0
- data/docs/integrations/ember-and-json-api.md +112 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/jsonapi/schema.md +140 -0
- data/lib/action_controller/serialization.rb +41 -37
- data/lib/active_model/serializable_resource.rb +72 -0
- data/lib/active_model/serializer/adapter/attributes.rb +66 -0
- data/lib/active_model/serializer/adapter/base.rb +58 -0
- data/lib/active_model/serializer/adapter/cached_serializer.rb +45 -0
- data/lib/active_model/serializer/adapter/fragment_cache.rb +111 -0
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +13 -0
- data/lib/active_model/serializer/adapter/json.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +207 -0
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +21 -0
- data/lib/active_model/serializer/adapter/json_api/link.rb +44 -0
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +58 -0
- data/lib/active_model/serializer/adapter/json_api.rb +223 -0
- data/lib/active_model/serializer/adapter/null.rb +11 -0
- data/lib/active_model/serializer/adapter.rb +91 -0
- data/lib/active_model/serializer/array_serializer.rb +9 -0
- data/lib/active_model/serializer/association.rb +20 -0
- data/lib/active_model/serializer/associations.rb +87 -220
- data/lib/active_model/serializer/attribute.rb +25 -0
- data/lib/active_model/serializer/attributes.rb +82 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +10 -0
- data/lib/active_model/serializer/caching.rb +100 -0
- data/lib/active_model/serializer/collection_reflection.rb +7 -0
- data/lib/active_model/serializer/collection_serializer.rb +47 -0
- data/lib/active_model/serializer/configuration.rb +28 -0
- data/lib/active_model/serializer/field.rb +56 -0
- data/lib/active_model/serializer/fieldset.rb +31 -0
- data/lib/active_model/serializer/has_many_reflection.rb +10 -0
- data/lib/active_model/serializer/has_one_reflection.rb +10 -0
- data/lib/active_model/serializer/include_tree.rb +111 -0
- data/lib/active_model/serializer/links.rb +33 -0
- data/lib/active_model/serializer/lint.rb +142 -0
- data/lib/active_model/serializer/reflection.rb +91 -0
- data/lib/active_model/serializer/singular_reflection.rb +7 -0
- data/lib/active_model/serializer/type.rb +25 -0
- data/lib/active_model/{serializers → serializer}/version.rb +1 -1
- data/lib/active_model/serializer.rb +99 -479
- data/lib/active_model_serializers/callbacks.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +13 -0
- data/lib/active_model_serializers/logging.rb +119 -0
- data/lib/active_model_serializers/model.rb +39 -0
- data/lib/active_model_serializers/railtie.rb +38 -0
- data/lib/active_model_serializers/serialization_context.rb +10 -0
- data/lib/active_model_serializers/test/schema.rb +103 -0
- data/lib/active_model_serializers/test/serializer.rb +125 -0
- data/lib/active_model_serializers/test.rb +7 -0
- data/lib/active_model_serializers.rb +20 -92
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +10 -0
- data/lib/generators/rails/serializer_generator.rb +36 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +14 -0
- data/lib/grape/formatters/active_model_serializers.rb +15 -0
- data/lib/grape/helpers/active_model_serializers.rb +16 -0
- data/test/action_controller/adapter_selector_test.rb +53 -0
- data/test/action_controller/explicit_serializer_test.rb +134 -0
- data/test/action_controller/json/include_test.rb +167 -0
- data/test/action_controller/json_api/deserialization_test.rb +59 -0
- data/test/action_controller/json_api/linked_test.rb +196 -0
- data/test/action_controller/json_api/pagination_test.rb +116 -0
- data/test/{serialization_scope_name_test.rb → action_controller/serialization_scope_name_test.rb} +11 -15
- data/test/action_controller/serialization_test.rb +435 -0
- data/test/active_model_serializers/logging_test.rb +77 -0
- data/test/active_model_serializers/model_test.rb +9 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +57 -0
- data/test/active_model_serializers/serialization_context_test.rb +18 -0
- data/test/active_model_serializers/test/schema_test.rb +128 -0
- data/test/active_model_serializers/test/serializer_test.rb +63 -0
- data/test/active_record_test.rb +9 -0
- data/test/adapter/fragment_cache_test.rb +38 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +92 -0
- data/test/adapter/json/has_many_test.rb +47 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +97 -0
- data/test/adapter/json_api/fields_test.rb +89 -0
- data/test/adapter/json_api/has_many_embed_ids_test.rb +45 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +145 -0
- data/test/adapter/json_api/has_one_test.rb +81 -0
- data/test/adapter/json_api/json_api_test.rb +37 -0
- data/test/adapter/json_api/linked_test.rb +394 -0
- data/test/adapter/json_api/links_test.rb +68 -0
- data/test/adapter/json_api/pagination_links_test.rb +115 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/resource_type_config_test.rb +71 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_test.rb +47 -0
- data/test/adapter/null_test.rb +25 -0
- data/test/adapter_test.rb +42 -0
- data/test/array_serializer_test.rb +36 -73
- data/test/collection_serializer_test.rb +100 -0
- data/test/fixtures/active_record.rb +56 -0
- data/test/fixtures/poro.rb +229 -0
- data/test/generators/scaffold_controller_generator_test.rb +24 -0
- data/test/generators/serializer_generator_test.rb +57 -0
- data/test/grape_test.rb +82 -0
- data/test/include_tree/from_include_args_test.rb +26 -0
- data/test/include_tree/from_string_test.rb +94 -0
- data/test/include_tree/include_args_to_hash_test.rb +64 -0
- data/test/lint_test.rb +40 -0
- data/test/logger_test.rb +18 -0
- data/test/poro_test.rb +9 -0
- data/test/serializable_resource_test.rb +27 -0
- data/test/serializers/adapter_for_test.rb +166 -0
- data/test/serializers/association_macros_test.rb +36 -0
- data/test/serializers/associations_test.rb +267 -0
- data/test/serializers/attribute_test.rb +123 -0
- data/test/serializers/attributes_test.rb +52 -0
- data/test/serializers/cache_test.rb +209 -0
- data/test/serializers/configuration_test.rb +32 -0
- data/test/serializers/fieldset_test.rb +14 -0
- data/test/serializers/meta_test.rb +130 -0
- data/test/serializers/options_test.rb +21 -0
- data/test/serializers/root_test.rb +21 -0
- data/test/serializers/serializer_for_test.rb +134 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +77 -0
- data/test/support/rails5_shims.rb +29 -0
- data/test/support/rails_app.rb +25 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/schemas/active_model_serializers/test/schema_test/my/show.json +6 -0
- data/test/support/schemas/custom/show.json +7 -0
- data/test/support/schemas/hyper_schema.json +93 -0
- data/test/support/schemas/render_using_json_api.json +43 -0
- data/test/support/schemas/simple_json_pointers.json +10 -0
- data/test/support/serialization_testing.rb +53 -0
- data/test/support/simplecov.rb +6 -0
- data/test/support/stream_capture.rb +50 -0
- data/test/support/test_case.rb +19 -0
- data/test/test_helper.rb +55 -24
- metadata +358 -42
- data/DESIGN.textile +0 -586
- data/Gemfile.edge +0 -9
- data/bench/perf.rb +0 -43
- data/cruft.md +0 -19
- data/lib/active_model/array_serializer.rb +0 -104
- data/lib/active_record/serializer_override.rb +0 -16
- data/lib/generators/resource_override.rb +0 -13
- data/lib/generators/serializer/USAGE +0 -9
- data/lib/generators/serializer/serializer_generator.rb +0 -42
- data/lib/generators/serializer/templates/serializer.rb +0 -19
- data/test/association_test.rb +0 -592
- data/test/caching_test.rb +0 -96
- data/test/generators_test.rb +0 -85
- data/test/no_serialization_scope_test.rb +0 -34
- data/test/serialization_test.rb +0 -392
- data/test/serializer_support_test.rb +0 -51
- data/test/serializer_test.rb +0 -1465
- data/test/test_fakes.rb +0 -217
@@ -0,0 +1,58 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
module Adapter
|
4
|
+
class JsonApi < Base
|
5
|
+
class PaginationLinks
|
6
|
+
FIRST_PAGE = 1
|
7
|
+
|
8
|
+
attr_reader :collection, :context
|
9
|
+
|
10
|
+
def initialize(collection, context)
|
11
|
+
@collection = collection
|
12
|
+
@context = context
|
13
|
+
end
|
14
|
+
|
15
|
+
def serializable_hash(options = {})
|
16
|
+
pages_from.each_with_object({}) do |(key, value), hash|
|
17
|
+
params = query_parameters.merge(page: { number: value, size: collection.size }).to_query
|
18
|
+
|
19
|
+
hash[key] = "#{url(options)}?#{params}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def pages_from
|
26
|
+
return {} if collection.total_pages == FIRST_PAGE
|
27
|
+
|
28
|
+
{}.tap do |pages|
|
29
|
+
pages[:self] = collection.current_page
|
30
|
+
|
31
|
+
unless collection.current_page == FIRST_PAGE
|
32
|
+
pages[:first] = FIRST_PAGE
|
33
|
+
pages[:prev] = collection.current_page - FIRST_PAGE
|
34
|
+
end
|
35
|
+
|
36
|
+
unless collection.current_page == collection.total_pages
|
37
|
+
pages[:next] = collection.current_page + FIRST_PAGE
|
38
|
+
pages[:last] = collection.total_pages
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def url(options)
|
44
|
+
@url ||= options.fetch(:links, {}).fetch(:self, nil) || request_url
|
45
|
+
end
|
46
|
+
|
47
|
+
def request_url
|
48
|
+
@request_url ||= context.request_url
|
49
|
+
end
|
50
|
+
|
51
|
+
def query_parameters
|
52
|
+
@query_parameters ||= context.query_parameters
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,223 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
module Adapter
|
4
|
+
class JsonApi < Base
|
5
|
+
extend ActiveSupport::Autoload
|
6
|
+
autoload :PaginationLinks
|
7
|
+
autoload :FragmentCache
|
8
|
+
autoload :Link
|
9
|
+
autoload :Deserialization
|
10
|
+
|
11
|
+
# TODO: if we like this abstraction and other API objects to it,
|
12
|
+
# then extract to its own file and require it.
|
13
|
+
module ApiObjects
|
14
|
+
module JsonApi
|
15
|
+
ActiveModelSerializers.config.jsonapi_version = '1.0'
|
16
|
+
ActiveModelSerializers.config.jsonapi_toplevel_meta = {}
|
17
|
+
# Make JSON API top-level jsonapi member opt-in
|
18
|
+
# ref: http://jsonapi.org/format/#document-top-level
|
19
|
+
ActiveModelSerializers.config.jsonapi_include_toplevel_object = false
|
20
|
+
|
21
|
+
module_function
|
22
|
+
|
23
|
+
def add!(hash)
|
24
|
+
hash.merge!(object) if include_object?
|
25
|
+
end
|
26
|
+
|
27
|
+
def include_object?
|
28
|
+
ActiveModelSerializers.config.jsonapi_include_toplevel_object
|
29
|
+
end
|
30
|
+
|
31
|
+
# TODO: see if we can cache this
|
32
|
+
def object
|
33
|
+
object = {
|
34
|
+
jsonapi: {
|
35
|
+
version: ActiveModelSerializers.config.jsonapi_version,
|
36
|
+
meta: ActiveModelSerializers.config.jsonapi_toplevel_meta
|
37
|
+
}
|
38
|
+
}
|
39
|
+
object[:jsonapi].reject! { |_, v| v.blank? }
|
40
|
+
|
41
|
+
object
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize(serializer, options = {})
|
47
|
+
super
|
48
|
+
@include_tree = IncludeTree.from_include_args(options[:include])
|
49
|
+
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
|
50
|
+
end
|
51
|
+
|
52
|
+
def serializable_hash(options = nil)
|
53
|
+
options ||= {}
|
54
|
+
|
55
|
+
hash =
|
56
|
+
if serializer.respond_to?(:each)
|
57
|
+
serializable_hash_for_collection(options)
|
58
|
+
else
|
59
|
+
serializable_hash_for_single_resource
|
60
|
+
end
|
61
|
+
|
62
|
+
ApiObjects::JsonApi.add!(hash)
|
63
|
+
|
64
|
+
if instance_options[:links]
|
65
|
+
hash[:links] ||= {}
|
66
|
+
hash[:links].update(instance_options[:links])
|
67
|
+
end
|
68
|
+
|
69
|
+
hash
|
70
|
+
end
|
71
|
+
|
72
|
+
def fragment_cache(cached_hash, non_cached_hash)
|
73
|
+
root = false if instance_options.include?(:include)
|
74
|
+
ActiveModel::Serializer::Adapter::JsonApi::FragmentCache.new.fragment_cache(root, cached_hash, non_cached_hash)
|
75
|
+
end
|
76
|
+
|
77
|
+
protected
|
78
|
+
|
79
|
+
attr_reader :fieldset
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def serializable_hash_for_collection(options)
|
84
|
+
hash = { data: [] }
|
85
|
+
included = []
|
86
|
+
serializer.each do |s|
|
87
|
+
result = self.class.new(s, instance_options.merge(fieldset: fieldset)).serializable_hash(options)
|
88
|
+
hash[:data] << result[:data]
|
89
|
+
next unless result[:included]
|
90
|
+
|
91
|
+
included |= result[:included]
|
92
|
+
end
|
93
|
+
|
94
|
+
included.delete_if { |resource| hash[:data].include?(resource) }
|
95
|
+
hash[:included] = included if included.any?
|
96
|
+
|
97
|
+
if serializer.paginated?
|
98
|
+
hash[:links] ||= {}
|
99
|
+
hash[:links].update(pagination_links_for(serializer, options))
|
100
|
+
end
|
101
|
+
|
102
|
+
hash
|
103
|
+
end
|
104
|
+
|
105
|
+
def serializable_hash_for_single_resource
|
106
|
+
primary_data = resource_object_for(serializer)
|
107
|
+
|
108
|
+
hash = { data: primary_data }
|
109
|
+
|
110
|
+
included = included_resources(@include_tree, [primary_data])
|
111
|
+
hash[:included] = included if included.any?
|
112
|
+
|
113
|
+
hash
|
114
|
+
end
|
115
|
+
|
116
|
+
def resource_identifier_type_for(serializer)
|
117
|
+
return serializer._type if serializer._type
|
118
|
+
if ActiveModelSerializers.config.jsonapi_resource_type == :singular
|
119
|
+
serializer.object.class.model_name.singular
|
120
|
+
else
|
121
|
+
serializer.object.class.model_name.plural
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def resource_identifier_id_for(serializer)
|
126
|
+
if serializer.respond_to?(:id)
|
127
|
+
serializer.id
|
128
|
+
else
|
129
|
+
serializer.object.id
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def resource_identifier_for(serializer)
|
134
|
+
type = resource_identifier_type_for(serializer)
|
135
|
+
id = resource_identifier_id_for(serializer)
|
136
|
+
|
137
|
+
{ id: id.to_s, type: type }
|
138
|
+
end
|
139
|
+
|
140
|
+
def attributes_for(serializer, fields)
|
141
|
+
serializer.attributes(fields).except(:id)
|
142
|
+
end
|
143
|
+
|
144
|
+
def resource_object_for(serializer)
|
145
|
+
resource_object = cache_check(serializer) do
|
146
|
+
resource_object = resource_identifier_for(serializer)
|
147
|
+
|
148
|
+
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
|
149
|
+
attributes = attributes_for(serializer, requested_fields)
|
150
|
+
resource_object[:attributes] = attributes if attributes.any?
|
151
|
+
resource_object
|
152
|
+
end
|
153
|
+
|
154
|
+
relationships = relationships_for(serializer)
|
155
|
+
resource_object[:relationships] = relationships if relationships.any?
|
156
|
+
|
157
|
+
links = links_for(serializer)
|
158
|
+
resource_object[:links] = links if links.any?
|
159
|
+
|
160
|
+
resource_object
|
161
|
+
end
|
162
|
+
|
163
|
+
def relationship_value_for(serializer, options = {})
|
164
|
+
if serializer.respond_to?(:each)
|
165
|
+
serializer.map { |s| resource_identifier_for(s) }
|
166
|
+
else
|
167
|
+
if options[:virtual_value]
|
168
|
+
options[:virtual_value]
|
169
|
+
elsif serializer && serializer.object
|
170
|
+
resource_identifier_for(serializer)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def relationships_for(serializer)
|
176
|
+
resource_type = resource_identifier_type_for(serializer)
|
177
|
+
requested_associations = fieldset.fields_for(resource_type) || '*'
|
178
|
+
include_tree = IncludeTree.from_include_args(requested_associations)
|
179
|
+
serializer.associations(include_tree).each_with_object({}) do |association, hash|
|
180
|
+
hash[association.key] = { data: relationship_value_for(association.serializer, association.options) }
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def included_resources(include_tree, primary_data)
|
185
|
+
included = []
|
186
|
+
|
187
|
+
serializer.associations(include_tree).each do |association|
|
188
|
+
add_included_resources_for(association.serializer, include_tree[association.key], primary_data, included)
|
189
|
+
end
|
190
|
+
|
191
|
+
included
|
192
|
+
end
|
193
|
+
|
194
|
+
def add_included_resources_for(serializer, include_tree, primary_data, included)
|
195
|
+
if serializer.respond_to?(:each)
|
196
|
+
serializer.each { |s| add_included_resources_for(s, include_tree, primary_data, included) }
|
197
|
+
else
|
198
|
+
return unless serializer && serializer.object
|
199
|
+
|
200
|
+
resource_object = resource_object_for(serializer)
|
201
|
+
|
202
|
+
return if included.include?(resource_object) || primary_data.include?(resource_object)
|
203
|
+
included.push(resource_object)
|
204
|
+
|
205
|
+
serializer.associations(include_tree).each do |association|
|
206
|
+
add_included_resources_for(association.serializer, include_tree[association.key], primary_data, included)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
def links_for(serializer)
|
212
|
+
serializer._links.each_with_object({}) do |(name, value), hash|
|
213
|
+
hash[name] = Link.new(serializer, value).as_json
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
def pagination_links_for(serializer, options)
|
218
|
+
JsonApi::PaginationLinks.new(serializer.object, options[:serialization_context]).serializable_hash(options)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
module Adapter
|
4
|
+
UnknownAdapterError = Class.new(ArgumentError)
|
5
|
+
ADAPTER_MAP = {}
|
6
|
+
private_constant :ADAPTER_MAP if defined?(private_constant)
|
7
|
+
require 'active_model/serializer/adapter/fragment_cache'
|
8
|
+
require 'active_model/serializer/adapter/cached_serializer'
|
9
|
+
|
10
|
+
class << self # All methods are class functions
|
11
|
+
def new(*args)
|
12
|
+
fail ArgumentError, 'Adapters inherit from Adapter::Base.' \
|
13
|
+
"Adapter.new called with args: '#{args.inspect}', from" \
|
14
|
+
"'caller[0]'."
|
15
|
+
end
|
16
|
+
|
17
|
+
def create(resource, options = {})
|
18
|
+
override = options.delete(:adapter)
|
19
|
+
klass = override ? adapter_class(override) : ActiveModel::Serializer.adapter
|
20
|
+
klass.new(resource, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @see ActiveModel::Serializer::Adapter.lookup
|
24
|
+
def adapter_class(adapter)
|
25
|
+
ActiveModel::Serializer::Adapter.lookup(adapter)
|
26
|
+
end
|
27
|
+
|
28
|
+
# @return Hash<adapter_name, adapter_class>
|
29
|
+
def adapter_map
|
30
|
+
ADAPTER_MAP
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Array<Symbol>] list of adapter names
|
34
|
+
def adapters
|
35
|
+
adapter_map.keys.sort
|
36
|
+
end
|
37
|
+
|
38
|
+
# Adds an adapter 'klass' with 'name' to the 'adapter_map'
|
39
|
+
# Names are stringified and underscored
|
40
|
+
# @param name [Symbol, String, Class] name of the registered adapter
|
41
|
+
# @param klass [Class] adapter class itself, optional if name is the class
|
42
|
+
# @example
|
43
|
+
# AMS::Adapter.register(:my_adapter, MyAdapter)
|
44
|
+
# @note The registered name strips out 'ActiveModel::Serializer::Adapter::'
|
45
|
+
# so that registering 'ActiveModel::Serializer::Adapter::Json' and
|
46
|
+
# 'Json' will both register as 'json'.
|
47
|
+
def register(name, klass = name)
|
48
|
+
name = name.to_s.gsub(/\AActiveModel::Serializer::Adapter::/, ''.freeze)
|
49
|
+
adapter_map.update(name.underscore => klass)
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
# @param adapter [String, Symbol, Class] name to fetch adapter by
|
54
|
+
# @return [ActiveModel::Serializer::Adapter] subclass of Adapter
|
55
|
+
# @raise [UnknownAdapterError]
|
56
|
+
def lookup(adapter)
|
57
|
+
# 1. return if is a class
|
58
|
+
return adapter if adapter.is_a?(Class)
|
59
|
+
adapter_name = adapter.to_s.underscore
|
60
|
+
# 2. return if registered
|
61
|
+
adapter_map.fetch(adapter_name) do
|
62
|
+
# 3. try to find adapter class from environment
|
63
|
+
adapter_class = find_by_name(adapter_name)
|
64
|
+
register(adapter_name, adapter_class)
|
65
|
+
adapter_class
|
66
|
+
end
|
67
|
+
rescue NameError, ArgumentError => e
|
68
|
+
failure_message =
|
69
|
+
"NameError: #{e.message}. Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}"
|
70
|
+
raise UnknownAdapterError, failure_message, e.backtrace
|
71
|
+
end
|
72
|
+
|
73
|
+
# @api private
|
74
|
+
def find_by_name(adapter_name)
|
75
|
+
adapter_name = adapter_name.to_s.classify.tr('API', 'Api')
|
76
|
+
"ActiveModel::Serializer::Adapter::#{adapter_name}".safe_constantize ||
|
77
|
+
"ActiveModel::Serializer::Adapter::#{adapter_name.pluralize}".safe_constantize or # rubocop:disable Style/AndOr
|
78
|
+
fail UnknownAdapterError
|
79
|
+
end
|
80
|
+
private :find_by_name
|
81
|
+
end
|
82
|
+
|
83
|
+
# Gotta be at the bottom to use the code above it :(
|
84
|
+
require 'active_model/serializer/adapter/base'
|
85
|
+
require 'active_model/serializer/adapter/null'
|
86
|
+
require 'active_model/serializer/adapter/attributes'
|
87
|
+
require 'active_model/serializer/adapter/json'
|
88
|
+
require 'active_model/serializer/adapter/json_api'
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'active_model/serializer/collection_serializer'
|
2
|
+
class ActiveModel::Serializer
|
3
|
+
class ArraySerializer < CollectionSerializer
|
4
|
+
def initialize(*)
|
5
|
+
warn "Calling deprecated ArraySerializer in #{caller[0..2].join(', ')}. Please use CollectionSerializer"
|
6
|
+
super
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Serializer
|
3
|
+
# This class hold all information about serializer's association.
|
4
|
+
#
|
5
|
+
# @param [Symbol] name
|
6
|
+
# @param [ActiveModel::Serializer] serializer
|
7
|
+
# @param [Hash{Symbol => Object}] options
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# Association.new(:comments, CommentSummarySerializer)
|
11
|
+
#
|
12
|
+
Association = Struct.new(:name, :serializer, :options) do
|
13
|
+
# @return [Symbol]
|
14
|
+
#
|
15
|
+
def key
|
16
|
+
options.fetch(:key, name)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|