active_model_serializers_custom 0.10.90
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +35 -0
- data/.rubocop.yml +109 -0
- data/.simplecov +110 -0
- data/.travis.yml +63 -0
- data/CHANGELOG.md +727 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +105 -0
- data/Gemfile +74 -0
- data/MIT-LICENSE +22 -0
- data/README.md +305 -0
- data/Rakefile +76 -0
- data/active_model_serializers.gemspec +64 -0
- data/appveyor.yml +28 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/rubocop +38 -0
- data/bin/serve_benchmark +39 -0
- data/docs/README.md +41 -0
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +269 -0
- data/docs/general/caching.md +58 -0
- data/docs/general/configuration_options.md +185 -0
- data/docs/general/deserialization.md +100 -0
- data/docs/general/fields.md +31 -0
- data/docs/general/getting_started.md +133 -0
- data/docs/general/instrumentation.md +40 -0
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/logging.md +21 -0
- data/docs/general/rendering.md +293 -0
- data/docs/general/serializers.md +495 -0
- data/docs/how-open-source-maintained.jpg +0 -0
- data/docs/howto/add_pagination_links.md +138 -0
- data/docs/howto/add_relationship_links.md +140 -0
- data/docs/howto/add_root_key.md +62 -0
- data/docs/howto/grape_integration.md +42 -0
- data/docs/howto/outside_controller_use.md +66 -0
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/howto/serialize_poro.md +73 -0
- data/docs/howto/test.md +154 -0
- data/docs/howto/upgrade_from_0_8_to_0_10.md +265 -0
- data/docs/integrations/ember-and-json-api.md +147 -0
- data/docs/integrations/grape.md +19 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +151 -0
- data/docs/jsonapi/schema/schema.json +366 -0
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +76 -0
- data/lib/active_model/serializable_resource.rb +13 -0
- data/lib/active_model/serializer.rb +418 -0
- data/lib/active_model/serializer/adapter.rb +26 -0
- data/lib/active_model/serializer/adapter/attributes.rb +17 -0
- data/lib/active_model/serializer/adapter/base.rb +20 -0
- data/lib/active_model/serializer/adapter/json.rb +17 -0
- data/lib/active_model/serializer/adapter/json_api.rb +17 -0
- data/lib/active_model/serializer/adapter/null.rb +17 -0
- data/lib/active_model/serializer/array_serializer.rb +14 -0
- data/lib/active_model/serializer/association.rb +91 -0
- data/lib/active_model/serializer/attribute.rb +27 -0
- data/lib/active_model/serializer/belongs_to_reflection.rb +13 -0
- data/lib/active_model/serializer/collection_serializer.rb +90 -0
- data/lib/active_model/serializer/concerns/caching.rb +304 -0
- data/lib/active_model/serializer/error_serializer.rb +16 -0
- data/lib/active_model/serializer/errors_serializer.rb +34 -0
- data/lib/active_model/serializer/field.rb +92 -0
- data/lib/active_model/serializer/fieldset.rb +33 -0
- data/lib/active_model/serializer/has_many_reflection.rb +12 -0
- data/lib/active_model/serializer/has_one_reflection.rb +9 -0
- data/lib/active_model/serializer/lazy_association.rb +99 -0
- data/lib/active_model/serializer/link.rb +23 -0
- data/lib/active_model/serializer/lint.rb +152 -0
- data/lib/active_model/serializer/null.rb +19 -0
- data/lib/active_model/serializer/reflection.rb +212 -0
- data/lib/active_model/serializer/version.rb +7 -0
- data/lib/active_model_serializers.rb +63 -0
- data/lib/active_model_serializers/adapter.rb +100 -0
- data/lib/active_model_serializers/adapter/attributes.rb +15 -0
- data/lib/active_model_serializers/adapter/base.rb +85 -0
- data/lib/active_model_serializers/adapter/json.rb +23 -0
- data/lib/active_model_serializers/adapter/json_api.rb +535 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +215 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +98 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +51 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +85 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +39 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +90 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +106 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +68 -0
- data/lib/active_model_serializers/adapter/null.rb +11 -0
- data/lib/active_model_serializers/callbacks.rb +57 -0
- data/lib/active_model_serializers/deprecate.rb +56 -0
- data/lib/active_model_serializers/deserialization.rb +17 -0
- data/lib/active_model_serializers/json_pointer.rb +16 -0
- data/lib/active_model_serializers/logging.rb +124 -0
- data/lib/active_model_serializers/lookup_chain.rb +82 -0
- data/lib/active_model_serializers/model.rb +132 -0
- data/lib/active_model_serializers/railtie.rb +52 -0
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +80 -0
- data/lib/active_model_serializers/serializable_resource.rb +84 -0
- data/lib/active_model_serializers/serialization_context.rb +41 -0
- data/lib/active_model_serializers/test.rb +9 -0
- data/lib/active_model_serializers/test/schema.rb +140 -0
- data/lib/active_model_serializers/test/serializer.rb +127 -0
- data/lib/generators/rails/USAGE +6 -0
- data/lib/generators/rails/resource_override.rb +12 -0
- data/lib/generators/rails/serializer_generator.rb +38 -0
- data/lib/generators/rails/templates/serializer.rb.erb +8 -0
- data/lib/grape/active_model_serializers.rb +18 -0
- data/lib/grape/formatters/active_model_serializers.rb +34 -0
- data/lib/grape/helpers/active_model_serializers.rb +19 -0
- data/lib/tasks/rubocop.rake +55 -0
- data/test/action_controller/adapter_selector_test.rb +64 -0
- data/test/action_controller/explicit_serializer_test.rb +137 -0
- data/test/action_controller/json/include_test.rb +248 -0
- data/test/action_controller/json_api/deserialization_test.rb +114 -0
- data/test/action_controller/json_api/errors_test.rb +42 -0
- data/test/action_controller/json_api/fields_test.rb +68 -0
- data/test/action_controller/json_api/linked_test.rb +204 -0
- data/test/action_controller/json_api/pagination_test.rb +126 -0
- data/test/action_controller/json_api/transform_test.rb +191 -0
- data/test/action_controller/lookup_proc_test.rb +51 -0
- data/test/action_controller/namespace_lookup_test.rb +239 -0
- data/test/action_controller/serialization_scope_name_test.rb +237 -0
- data/test/action_controller/serialization_test.rb +480 -0
- data/test/active_model_serializers/adapter_for_test.rb +210 -0
- data/test/active_model_serializers/json_pointer_test.rb +24 -0
- data/test/active_model_serializers/logging_test.rb +79 -0
- data/test/active_model_serializers/model_test.rb +144 -0
- data/test/active_model_serializers/railtie_test_isolated.rb +70 -0
- data/test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb +163 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +73 -0
- data/test/active_model_serializers/test/schema_test.rb +133 -0
- data/test/active_model_serializers/test/serializer_test.rb +64 -0
- data/test/active_record_test.rb +11 -0
- data/test/adapter/attributes_test.rb +42 -0
- data/test/adapter/deprecation_test.rb +102 -0
- data/test/adapter/json/belongs_to_test.rb +47 -0
- data/test/adapter/json/collection_test.rb +106 -0
- data/test/adapter/json/has_many_test.rb +55 -0
- data/test/adapter/json/transform_test.rb +95 -0
- data/test/adapter/json_api/belongs_to_test.rb +157 -0
- data/test/adapter/json_api/collection_test.rb +98 -0
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +98 -0
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +98 -0
- data/test/adapter/json_api/has_many_test.rb +175 -0
- data/test/adapter/json_api/has_one_test.rb +82 -0
- data/test/adapter/json_api/include_data_if_sideloaded_test.rb +215 -0
- data/test/adapter/json_api/json_api_test.rb +35 -0
- data/test/adapter/json_api/linked_test.rb +415 -0
- data/test/adapter/json_api/links_test.rb +112 -0
- data/test/adapter/json_api/pagination_links_test.rb +208 -0
- data/test/adapter/json_api/parse_test.rb +139 -0
- data/test/adapter/json_api/relationship_test.rb +399 -0
- data/test/adapter/json_api/resource_meta_test.rb +102 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +84 -0
- data/test/adapter/json_api/transform_test.rb +514 -0
- data/test/adapter/json_api/type_test.rb +195 -0
- data/test/adapter/json_test.rb +48 -0
- data/test/adapter/null_test.rb +24 -0
- data/test/adapter/polymorphic_test.rb +220 -0
- data/test/adapter_test.rb +69 -0
- data/test/array_serializer_test.rb +24 -0
- data/test/benchmark/app.rb +67 -0
- data/test/benchmark/benchmarking_support.rb +69 -0
- data/test/benchmark/bm_active_record.rb +83 -0
- data/test/benchmark/bm_adapter.rb +40 -0
- data/test/benchmark/bm_caching.rb +121 -0
- data/test/benchmark/bm_lookup_chain.rb +85 -0
- data/test/benchmark/bm_transform.rb +47 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +85 -0
- data/test/benchmark/fixtures.rb +221 -0
- data/test/cache_test.rb +717 -0
- data/test/collection_serializer_test.rb +129 -0
- data/test/fixtures/active_record.rb +115 -0
- data/test/fixtures/poro.rb +227 -0
- data/test/generators/scaffold_controller_generator_test.rb +26 -0
- data/test/generators/serializer_generator_test.rb +77 -0
- data/test/grape_test.rb +198 -0
- data/test/lint_test.rb +51 -0
- data/test/logger_test.rb +22 -0
- data/test/poro_test.rb +11 -0
- data/test/serializable_resource_test.rb +81 -0
- data/test/serializers/association_macros_test.rb +39 -0
- data/test/serializers/associations_test.rb +520 -0
- data/test/serializers/attribute_test.rb +155 -0
- data/test/serializers/attributes_test.rb +54 -0
- data/test/serializers/caching_configuration_test_isolated.rb +172 -0
- data/test/serializers/configuration_test.rb +34 -0
- data/test/serializers/fieldset_test.rb +16 -0
- data/test/serializers/meta_test.rb +204 -0
- data/test/serializers/options_test.rb +34 -0
- data/test/serializers/read_attribute_for_serialization_test.rb +81 -0
- data/test/serializers/reflection_test.rb +481 -0
- data/test/serializers/root_test.rb +23 -0
- data/test/serializers/serialization_test.rb +57 -0
- data/test/serializers/serializer_for_test.rb +138 -0
- data/test/serializers/serializer_for_with_namespace_test.rb +90 -0
- data/test/support/custom_schemas/active_model_serializers/test/schema_test/my/index.json +6 -0
- data/test/support/isolated_unit.rb +86 -0
- data/test/support/rails5_shims.rb +55 -0
- data/test/support/rails_app.rb +40 -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 +81 -0
- data/test/test_helper.rb +72 -0
- metadata +622 -0
@@ -0,0 +1,212 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model/serializer/field'
|
4
|
+
require 'active_model/serializer/association'
|
5
|
+
|
6
|
+
module ActiveModel
|
7
|
+
class Serializer
|
8
|
+
# Holds all the meta-data about an association as it was specified in the
|
9
|
+
# ActiveModel::Serializer class.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# class PostSerializer < ActiveModel::Serializer
|
13
|
+
# has_one :author, serializer: AuthorSerializer
|
14
|
+
# belongs_to :boss, type: :users, foreign_key: :boss_id
|
15
|
+
# has_many :comments
|
16
|
+
# has_many :comments, key: :last_comments do
|
17
|
+
# object.comments.last(1)
|
18
|
+
# end
|
19
|
+
# has_many :secret_meta_data, if: :is_admin?
|
20
|
+
#
|
21
|
+
# has_one :blog do |serializer|
|
22
|
+
# meta count: object.roles.count
|
23
|
+
# serializer.cached_blog
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# private
|
27
|
+
#
|
28
|
+
# def cached_blog
|
29
|
+
# cache_store.fetch("cached_blog:#{object.updated_at}") do
|
30
|
+
# Blog.find(object.blog_id)
|
31
|
+
# end
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# def is_admin?
|
35
|
+
# current_user.admin?
|
36
|
+
# end
|
37
|
+
# end
|
38
|
+
#
|
39
|
+
# Specifically, the association 'comments' is evaluated two different ways:
|
40
|
+
# 1) as 'comments' and named 'comments'.
|
41
|
+
# 2) as 'object.comments.last(1)' and named 'last_comments'.
|
42
|
+
#
|
43
|
+
# PostSerializer._reflections # =>
|
44
|
+
# # {
|
45
|
+
# # author: HasOneReflection.new(:author, serializer: AuthorSerializer),
|
46
|
+
# # comments: HasManyReflection.new(:comments)
|
47
|
+
# # last_comments: HasManyReflection.new(:comments, { key: :last_comments }, #<Block>)
|
48
|
+
# # secret_meta_data: HasManyReflection.new(:secret_meta_data, { if: :is_admin? })
|
49
|
+
# # }
|
50
|
+
#
|
51
|
+
# So you can inspect reflections in your Adapters.
|
52
|
+
class Reflection < Field
|
53
|
+
attr_reader :foreign_key, :type
|
54
|
+
|
55
|
+
def initialize(*)
|
56
|
+
super
|
57
|
+
options[:links] = {}
|
58
|
+
options[:include_data_setting] = Serializer.config.include_data_default
|
59
|
+
options[:meta] = nil
|
60
|
+
@type = options.fetch(:type) do
|
61
|
+
class_name = options.fetch(:class_name, name.to_s.camelize.singularize)
|
62
|
+
class_name.underscore.pluralize.to_sym
|
63
|
+
end
|
64
|
+
@foreign_key = options.fetch(:foreign_key) do
|
65
|
+
if collection?
|
66
|
+
"#{name.to_s.singularize}_ids".to_sym
|
67
|
+
else
|
68
|
+
"#{name}_id".to_sym
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# @api public
|
74
|
+
# @example
|
75
|
+
# has_one :blog do
|
76
|
+
# include_data false
|
77
|
+
# link :self, 'a link'
|
78
|
+
# link :related, 'another link'
|
79
|
+
# link :self, '//example.com/link_author/relationships/bio'
|
80
|
+
# id = object.profile.id
|
81
|
+
# link :related do
|
82
|
+
# "//example.com/profiles/#{id}" if id != 123
|
83
|
+
# end
|
84
|
+
# link :related do
|
85
|
+
# ids = object.likes.map(&:id).join(',')
|
86
|
+
# href "//example.com/likes/#{ids}"
|
87
|
+
# meta ids: ids
|
88
|
+
# end
|
89
|
+
# end
|
90
|
+
def link(name, value = nil)
|
91
|
+
options[:links][name] = block_given? ? Proc.new : value
|
92
|
+
:nil
|
93
|
+
end
|
94
|
+
|
95
|
+
# @api public
|
96
|
+
# @example
|
97
|
+
# has_one :blog do
|
98
|
+
# include_data false
|
99
|
+
# meta(id: object.blog.id)
|
100
|
+
# meta liked: object.likes.any?
|
101
|
+
# link :self do
|
102
|
+
# href object.blog.id.to_s
|
103
|
+
# meta(id: object.blog.id)
|
104
|
+
# end
|
105
|
+
def meta(value = nil)
|
106
|
+
options[:meta] = block_given? ? Proc.new : value
|
107
|
+
:nil
|
108
|
+
end
|
109
|
+
|
110
|
+
# @api public
|
111
|
+
# @example
|
112
|
+
# has_one :blog do
|
113
|
+
# include_data false
|
114
|
+
# link :self, 'a link'
|
115
|
+
# link :related, 'another link'
|
116
|
+
# end
|
117
|
+
#
|
118
|
+
# has_one :blog do
|
119
|
+
# include_data false
|
120
|
+
# link :self, 'a link'
|
121
|
+
# link :related, 'another link'
|
122
|
+
# end
|
123
|
+
#
|
124
|
+
# belongs_to :reviewer do
|
125
|
+
# meta name: 'Dan Brown'
|
126
|
+
# include_data true
|
127
|
+
# end
|
128
|
+
#
|
129
|
+
# has_many :tags, serializer: TagSerializer do
|
130
|
+
# link :self, '//example.com/link_author/relationships/tags'
|
131
|
+
# include_data :if_sideloaded
|
132
|
+
# end
|
133
|
+
def include_data(value = true)
|
134
|
+
options[:include_data_setting] = value
|
135
|
+
:nil
|
136
|
+
end
|
137
|
+
|
138
|
+
def collection?
|
139
|
+
false
|
140
|
+
end
|
141
|
+
|
142
|
+
def include_data?(include_slice)
|
143
|
+
include_data_setting = options[:include_data_setting]
|
144
|
+
case include_data_setting
|
145
|
+
when :if_sideloaded then include_slice.key?(options.fetch(:key, name))
|
146
|
+
when true then true
|
147
|
+
when false then false
|
148
|
+
else fail ArgumentError, "Unknown include_data_setting '#{include_data_setting.inspect}'"
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
# @param serializer [ActiveModel::Serializer]
|
153
|
+
# @yield [ActiveModel::Serializer]
|
154
|
+
# @return [:nil, associated resource or resource collection]
|
155
|
+
def value(serializer, include_slice)
|
156
|
+
# NOTE(BF): This method isn't thread-safe because the _reflections class attribute is not thread-safe
|
157
|
+
# Therefore, when we build associations from reflections, we dup the entire reflection instance.
|
158
|
+
# Better solutions much appreciated!
|
159
|
+
@object = serializer.object
|
160
|
+
@scope = serializer.scope
|
161
|
+
|
162
|
+
block_value = instance_exec(serializer, &block) if block
|
163
|
+
return unless include_data?(include_slice)
|
164
|
+
|
165
|
+
if block && block_value != :nil
|
166
|
+
block_value
|
167
|
+
else
|
168
|
+
serializer.read_attribute_for_serialization(name)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# @api private
|
173
|
+
def foreign_key_on
|
174
|
+
:related
|
175
|
+
end
|
176
|
+
|
177
|
+
# Build association. This method is used internally to
|
178
|
+
# build serializer's association by its reflection.
|
179
|
+
#
|
180
|
+
# @param [Serializer] parent_serializer for given association
|
181
|
+
# @param [Hash{Symbol => Object}] parent_serializer_options
|
182
|
+
#
|
183
|
+
# @example
|
184
|
+
# # Given the following serializer defined:
|
185
|
+
# class PostSerializer < ActiveModel::Serializer
|
186
|
+
# has_many :comments, serializer: CommentSummarySerializer
|
187
|
+
# end
|
188
|
+
#
|
189
|
+
# # Then you instantiate your serializer
|
190
|
+
# post_serializer = PostSerializer.new(post, foo: 'bar') #
|
191
|
+
# # to build association for comments you need to get reflection
|
192
|
+
# comments_reflection = PostSerializer._reflections.detect { |r| r.name == :comments }
|
193
|
+
# # and #build_association
|
194
|
+
# comments_reflection.build_association(post_serializer, foo: 'bar')
|
195
|
+
#
|
196
|
+
# @api private
|
197
|
+
def build_association(parent_serializer, parent_serializer_options, include_slice = {})
|
198
|
+
association_options = {
|
199
|
+
parent_serializer: parent_serializer,
|
200
|
+
parent_serializer_options: parent_serializer_options,
|
201
|
+
include_slice: include_slice
|
202
|
+
}
|
203
|
+
Association.new(self, association_options)
|
204
|
+
end
|
205
|
+
|
206
|
+
protected
|
207
|
+
|
208
|
+
# used in instance exec
|
209
|
+
attr_accessor :object, :scope
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/core_ext/object/with_options'
|
6
|
+
require 'active_support/core_ext/string/inflections'
|
7
|
+
require 'active_support/json'
|
8
|
+
module ActiveModelSerializers
|
9
|
+
extend ActiveSupport::Autoload
|
10
|
+
eager_autoload do
|
11
|
+
autoload :Model
|
12
|
+
autoload :Callbacks
|
13
|
+
autoload :SerializableResource
|
14
|
+
autoload :SerializationContext
|
15
|
+
autoload :Logging
|
16
|
+
autoload :Test
|
17
|
+
autoload :Adapter
|
18
|
+
autoload :JsonPointer
|
19
|
+
autoload :Deprecate
|
20
|
+
autoload :LookupChain
|
21
|
+
autoload :Deserialization
|
22
|
+
end
|
23
|
+
|
24
|
+
class << self; attr_accessor :logger; end
|
25
|
+
self.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(STDOUT))
|
26
|
+
|
27
|
+
def self.config
|
28
|
+
ActiveModel::Serializer.config
|
29
|
+
end
|
30
|
+
|
31
|
+
# The file name and line number of the caller of the caller of this method.
|
32
|
+
def self.location_of_caller
|
33
|
+
caller[1] =~ /(.*?):(\d+).*?$/i
|
34
|
+
file = Regexp.last_match(1)
|
35
|
+
lineno = Regexp.last_match(2).to_i
|
36
|
+
|
37
|
+
[file, lineno]
|
38
|
+
end
|
39
|
+
|
40
|
+
# Memoized default include directive
|
41
|
+
# @return [JSONAPI::IncludeDirective]
|
42
|
+
def self.default_include_directive
|
43
|
+
@default_include_directive ||= JSONAPI::IncludeDirective.new(config.default_includes, allow_wildcard: true)
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.silence_warnings
|
47
|
+
original_verbose = $VERBOSE
|
48
|
+
$VERBOSE = nil
|
49
|
+
yield
|
50
|
+
ensure
|
51
|
+
$VERBOSE = original_verbose
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.eager_load!
|
55
|
+
super
|
56
|
+
ActiveModel::Serializer.eager_load!
|
57
|
+
end
|
58
|
+
|
59
|
+
require 'active_model/serializer/version'
|
60
|
+
require 'active_model/serializer'
|
61
|
+
require 'active_model/serializable_resource'
|
62
|
+
require 'active_model_serializers/railtie' if defined?(::Rails::Railtie)
|
63
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
UnknownAdapterError = Class.new(ArgumentError)
|
6
|
+
ADAPTER_MAP = {} # rubocop:disable Style/MutableConstant
|
7
|
+
private_constant :ADAPTER_MAP if defined?(private_constant)
|
8
|
+
|
9
|
+
class << self # All methods are class functions
|
10
|
+
# :nocov:
|
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
|
+
# :nocov:
|
17
|
+
|
18
|
+
def configured_adapter
|
19
|
+
lookup(ActiveModelSerializers.config.adapter)
|
20
|
+
end
|
21
|
+
|
22
|
+
def create(resource, options = {})
|
23
|
+
override = options.delete(:adapter)
|
24
|
+
klass = override ? adapter_class(override) : configured_adapter
|
25
|
+
klass.new(resource, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
# @see ActiveModelSerializers::Adapter.lookup
|
29
|
+
def adapter_class(adapter)
|
30
|
+
ActiveModelSerializers::Adapter.lookup(adapter)
|
31
|
+
end
|
32
|
+
|
33
|
+
# @return [Hash<adapter_name, adapter_class>]
|
34
|
+
def adapter_map
|
35
|
+
ADAPTER_MAP
|
36
|
+
end
|
37
|
+
|
38
|
+
# @return [Array<Symbol>] list of adapter names
|
39
|
+
def adapters
|
40
|
+
adapter_map.keys.sort!
|
41
|
+
end
|
42
|
+
|
43
|
+
# Adds an adapter 'klass' with 'name' to the 'adapter_map'
|
44
|
+
# Names are stringified and underscored
|
45
|
+
# @param name [Symbol, String, Class] name of the registered adapter
|
46
|
+
# @param klass [Class] adapter class itself, optional if name is the class
|
47
|
+
# @example
|
48
|
+
# AMS::Adapter.register(:my_adapter, MyAdapter)
|
49
|
+
# @note The registered name strips out 'ActiveModelSerializers::Adapter::'
|
50
|
+
# so that registering 'ActiveModelSerializers::Adapter::Json' and
|
51
|
+
# 'Json' will both register as 'json'.
|
52
|
+
def register(name, klass = name)
|
53
|
+
name = name.to_s.gsub(/\AActiveModelSerializers::Adapter::/, ''.freeze)
|
54
|
+
adapter_map[name.underscore] = klass
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def registered_name(adapter_class)
|
59
|
+
ADAPTER_MAP.key adapter_class
|
60
|
+
end
|
61
|
+
|
62
|
+
# @param adapter [String, Symbol, Class] name to fetch adapter by
|
63
|
+
# @return [ActiveModelSerializers::Adapter] subclass of Adapter
|
64
|
+
# @raise [UnknownAdapterError]
|
65
|
+
def lookup(adapter)
|
66
|
+
# 1. return if is a class
|
67
|
+
return adapter if adapter.is_a?(Class)
|
68
|
+
adapter_name = adapter.to_s.underscore
|
69
|
+
# 2. return if registered
|
70
|
+
adapter_map.fetch(adapter_name) do
|
71
|
+
# 3. try to find adapter class from environment
|
72
|
+
adapter_class = find_by_name(adapter_name)
|
73
|
+
register(adapter_name, adapter_class)
|
74
|
+
adapter_class
|
75
|
+
end
|
76
|
+
rescue NameError, ArgumentError => e
|
77
|
+
failure_message =
|
78
|
+
"NameError: #{e.message}. Unknown adapter: #{adapter.inspect}. Valid adapters are: #{adapters}"
|
79
|
+
raise UnknownAdapterError, failure_message, e.backtrace
|
80
|
+
end
|
81
|
+
|
82
|
+
# @api private
|
83
|
+
def find_by_name(adapter_name)
|
84
|
+
adapter_name = adapter_name.to_s.classify.tr('API', 'Api')
|
85
|
+
"ActiveModelSerializers::Adapter::#{adapter_name}".safe_constantize ||
|
86
|
+
"ActiveModelSerializers::Adapter::#{adapter_name.pluralize}".safe_constantize or # rubocop:disable Style/AndOr
|
87
|
+
fail UnknownAdapterError
|
88
|
+
end
|
89
|
+
private :find_by_name
|
90
|
+
end
|
91
|
+
|
92
|
+
# Gotta be at the bottom to use the code above it :(
|
93
|
+
extend ActiveSupport::Autoload
|
94
|
+
autoload :Base
|
95
|
+
autoload :Null
|
96
|
+
autoload :Attributes
|
97
|
+
autoload :Json
|
98
|
+
autoload :JsonApi
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveModelSerializers
|
4
|
+
module Adapter
|
5
|
+
class Attributes < Base
|
6
|
+
def serializable_hash(options = nil)
|
7
|
+
options = serialization_options(options)
|
8
|
+
options[:fields] ||= instance_options[:fields]
|
9
|
+
serialized_hash = serializer.serializable_hash(instance_options, options, self)
|
10
|
+
|
11
|
+
self.class.transform_key_casing!(serialized_hash, instance_options)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'case_transform'
|
4
|
+
|
5
|
+
module ActiveModelSerializers
|
6
|
+
module Adapter
|
7
|
+
class Base
|
8
|
+
# Automatically register adapters when subclassing
|
9
|
+
def self.inherited(subclass)
|
10
|
+
ActiveModelSerializers::Adapter.register(subclass)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Sets the default transform for the adapter.
|
14
|
+
#
|
15
|
+
# @return [Symbol] the default transform for the adapter
|
16
|
+
def self.default_key_transform
|
17
|
+
:unaltered
|
18
|
+
end
|
19
|
+
|
20
|
+
# Determines the transform to use in order of precedence:
|
21
|
+
# adapter option, global config, adapter default.
|
22
|
+
#
|
23
|
+
# @param options [Object]
|
24
|
+
# @return [Symbol] the transform to use
|
25
|
+
def self.transform(options)
|
26
|
+
return options[:key_transform] if options && options[:key_transform]
|
27
|
+
ActiveModelSerializers.config.key_transform || default_key_transform
|
28
|
+
end
|
29
|
+
|
30
|
+
# Transforms the casing of the supplied value.
|
31
|
+
#
|
32
|
+
# @param value [Object] the value to be transformed
|
33
|
+
# @param options [Object] serializable resource options
|
34
|
+
# @return [Symbol] the default transform for the adapter
|
35
|
+
def self.transform_key_casing!(value, options)
|
36
|
+
CaseTransform.send(transform(options), value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.cache_key
|
40
|
+
@cache_key ||= ActiveModelSerializers::Adapter.registered_name(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.fragment_cache(cached_hash, non_cached_hash)
|
44
|
+
non_cached_hash.merge cached_hash
|
45
|
+
end
|
46
|
+
|
47
|
+
attr_reader :serializer, :instance_options
|
48
|
+
|
49
|
+
def initialize(serializer, options = {})
|
50
|
+
@serializer = serializer
|
51
|
+
@instance_options = options
|
52
|
+
end
|
53
|
+
|
54
|
+
# Subclasses that implement this method must first call
|
55
|
+
# options = serialization_options(options)
|
56
|
+
def serializable_hash(_options = nil)
|
57
|
+
fail NotImplementedError, 'This is an abstract method. Should be implemented at the concrete adapter.'
|
58
|
+
end
|
59
|
+
|
60
|
+
def as_json(options = nil)
|
61
|
+
serializable_hash(options)
|
62
|
+
end
|
63
|
+
|
64
|
+
def cache_key
|
65
|
+
self.class.cache_key
|
66
|
+
end
|
67
|
+
|
68
|
+
def fragment_cache(cached_hash, non_cached_hash)
|
69
|
+
self.class.fragment_cache(cached_hash, non_cached_hash)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# see https://github.com/rails-api/active_model_serializers/pull/965
|
75
|
+
# When <tt>options</tt> is +nil+, sets it to +{}+
|
76
|
+
def serialization_options(options)
|
77
|
+
options ||= {} # rubocop:disable Lint/UselessAssignment
|
78
|
+
end
|
79
|
+
|
80
|
+
def root
|
81
|
+
serializer.json_key.to_sym if serializer.json_key
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|