active_model_serializers 0.10.0.rc4 → 0.10.0.rc5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE.md +29 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +15 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +19 -1
- data/.rubocop_todo.yml +30 -103
- data/.simplecov +0 -1
- data/.travis.yml +20 -8
- data/CHANGELOG.md +89 -5
- data/CONTRIBUTING.md +54 -179
- data/Gemfile +7 -2
- data/{LICENSE.txt → MIT-LICENSE} +0 -0
- data/README.md +27 -5
- data/Rakefile +44 -16
- data/active_model_serializers.gemspec +9 -1
- data/appveyor.yml +1 -0
- data/bin/bench +171 -0
- data/bin/bench_regression +316 -0
- data/bin/serve_benchmark +39 -0
- data/docs/ARCHITECTURE.md +13 -7
- data/docs/README.md +5 -1
- data/docs/STYLE.md +58 -0
- data/docs/general/adapters.md +99 -16
- data/docs/general/configuration_options.md +87 -14
- data/docs/general/deserialization.md +100 -0
- data/docs/general/getting_started.md +35 -0
- data/docs/general/instrumentation.md +1 -1
- data/docs/general/key_transforms.md +40 -0
- data/docs/general/rendering.md +115 -13
- data/docs/general/serializers.md +138 -6
- data/docs/howto/add_pagination_links.md +36 -18
- data/docs/howto/outside_controller_use.md +4 -4
- data/docs/howto/passing_arbitrary_options.md +27 -0
- data/docs/jsonapi/errors.md +56 -0
- data/docs/jsonapi/schema.md +29 -18
- data/docs/rfcs/0000-namespace.md +106 -0
- data/docs/rfcs/template.md +15 -0
- data/lib/action_controller/serialization.rb +10 -19
- data/lib/active_model/serializable_resource.rb +4 -65
- data/lib/active_model/serializer.rb +73 -18
- data/lib/active_model/serializer/adapter.rb +15 -82
- data/lib/active_model/serializer/adapter/attributes.rb +5 -56
- data/lib/active_model/serializer/adapter/base.rb +5 -47
- data/lib/active_model/serializer/adapter/json.rb +6 -12
- data/lib/active_model/serializer/adapter/json_api.rb +5 -213
- data/lib/active_model/serializer/adapter/null.rb +7 -3
- data/lib/active_model/serializer/array_serializer.rb +3 -3
- data/lib/active_model/serializer/association.rb +4 -5
- data/lib/active_model/serializer/attributes.rb +1 -1
- data/lib/active_model/serializer/caching.rb +56 -5
- data/lib/active_model/serializer/collection_serializer.rb +30 -13
- data/lib/active_model/serializer/configuration.rb +7 -0
- data/lib/active_model/serializer/error_serializer.rb +10 -0
- data/lib/active_model/serializer/errors_serializer.rb +27 -0
- data/lib/active_model/serializer/links.rb +4 -2
- data/lib/active_model/serializer/lint.rb +14 -0
- data/lib/active_model/serializer/meta.rb +29 -0
- data/lib/active_model/serializer/null.rb +17 -0
- data/lib/active_model/serializer/reflection.rb +57 -1
- data/lib/active_model/serializer/type.rb +1 -1
- data/lib/active_model/serializer/version.rb +1 -1
- data/lib/active_model_serializers.rb +17 -0
- data/lib/active_model_serializers/adapter.rb +92 -0
- data/lib/active_model_serializers/adapter/attributes.rb +94 -0
- data/lib/active_model_serializers/adapter/base.rb +90 -0
- data/lib/active_model_serializers/adapter/json.rb +11 -0
- data/lib/active_model_serializers/adapter/json_api.rb +513 -0
- data/lib/active_model_serializers/adapter/json_api/deserialization.rb +213 -0
- data/lib/active_model_serializers/adapter/json_api/error.rb +96 -0
- data/lib/active_model_serializers/adapter/json_api/jsonapi.rb +49 -0
- data/lib/active_model_serializers/adapter/json_api/link.rb +83 -0
- data/lib/active_model_serializers/adapter/json_api/meta.rb +37 -0
- data/lib/active_model_serializers/adapter/json_api/pagination_links.rb +57 -0
- data/lib/active_model_serializers/adapter/json_api/relationship.rb +52 -0
- data/lib/active_model_serializers/adapter/json_api/resource_identifier.rb +37 -0
- data/lib/active_model_serializers/adapter/null.rb +10 -0
- data/lib/active_model_serializers/cached_serializer.rb +87 -0
- data/lib/active_model_serializers/callbacks.rb +1 -1
- data/lib/active_model_serializers/deprecate.rb +55 -0
- data/lib/active_model_serializers/deserialization.rb +2 -2
- data/lib/active_model_serializers/fragment_cache.rb +118 -0
- data/lib/active_model_serializers/json_pointer.rb +14 -0
- data/lib/active_model_serializers/key_transform.rb +70 -0
- data/lib/active_model_serializers/logging.rb +4 -1
- data/lib/active_model_serializers/model.rb +11 -1
- data/lib/active_model_serializers/railtie.rb +9 -1
- data/lib/active_model_serializers/register_jsonapi_renderer.rb +64 -0
- data/lib/active_model_serializers/serializable_resource.rb +81 -0
- data/lib/active_model_serializers/serialization_context.rb +24 -2
- data/lib/active_model_serializers/test/schema.rb +2 -2
- data/lib/grape/formatters/active_model_serializers.rb +1 -1
- data/test/action_controller/adapter_selector_test.rb +1 -1
- data/test/action_controller/json_api/deserialization_test.rb +56 -3
- data/test/action_controller/json_api/errors_test.rb +41 -0
- data/test/action_controller/json_api/linked_test.rb +10 -9
- data/test/action_controller/json_api/pagination_test.rb +2 -2
- data/test/action_controller/json_api/transform_test.rb +180 -0
- data/test/action_controller/serialization_scope_name_test.rb +201 -35
- data/test/action_controller/serialization_test.rb +39 -7
- data/test/active_model_serializers/adapter_for_test.rb +208 -0
- data/test/active_model_serializers/cached_serializer_test.rb +80 -0
- data/test/active_model_serializers/fragment_cache_test.rb +34 -0
- data/test/active_model_serializers/json_pointer_test.rb +20 -0
- data/test/active_model_serializers/key_transform_test.rb +263 -0
- data/test/active_model_serializers/logging_test.rb +8 -8
- data/test/active_model_serializers/railtie_test_isolated.rb +6 -0
- data/test/active_model_serializers/serialization_context_test_isolated.rb +58 -0
- data/test/adapter/deprecation_test.rb +100 -0
- data/test/adapter/json/belongs_to_test.rb +32 -34
- data/test/adapter/json/collection_test.rb +73 -75
- data/test/adapter/json/has_many_test.rb +36 -38
- data/test/adapter/json/transform_test.rb +93 -0
- data/test/adapter/json_api/belongs_to_test.rb +127 -129
- data/test/adapter/json_api/collection_test.rb +80 -82
- data/test/adapter/json_api/errors_test.rb +78 -0
- data/test/adapter/json_api/fields_test.rb +68 -70
- data/test/adapter/json_api/has_many_embed_ids_test.rb +32 -34
- data/test/adapter/json_api/has_many_explicit_serializer_test.rb +75 -77
- data/test/adapter/json_api/has_many_test.rb +121 -123
- data/test/adapter/json_api/has_one_test.rb +59 -61
- data/test/adapter/json_api/json_api_test.rb +28 -30
- data/test/adapter/json_api/linked_test.rb +319 -321
- data/test/adapter/json_api/links_test.rb +75 -50
- data/test/adapter/json_api/pagination_links_test.rb +115 -82
- data/test/adapter/json_api/parse_test.rb +114 -116
- data/test/adapter/json_api/relationship_test.rb +161 -0
- data/test/adapter/json_api/relationships_test.rb +199 -0
- data/test/adapter/json_api/resource_identifier_test.rb +85 -0
- data/test/adapter/json_api/resource_meta_test.rb +100 -0
- data/test/adapter/json_api/toplevel_jsonapi_test.rb +61 -63
- data/test/adapter/json_api/transform_test.rb +500 -0
- data/test/adapter/json_api/type_test.rb +61 -0
- data/test/adapter/json_test.rb +35 -37
- data/test/adapter/null_test.rb +13 -15
- data/test/adapter/polymorphic_test.rb +72 -0
- data/test/adapter_test.rb +27 -29
- data/test/array_serializer_test.rb +7 -8
- data/test/benchmark/app.rb +65 -0
- data/test/benchmark/benchmarking_support.rb +67 -0
- data/test/benchmark/bm_caching.rb +117 -0
- data/test/benchmark/bm_transform.rb +34 -0
- data/test/benchmark/config.ru +3 -0
- data/test/benchmark/controllers.rb +77 -0
- data/test/benchmark/fixtures.rb +167 -0
- data/test/cache_test.rb +388 -0
- data/test/collection_serializer_test.rb +10 -0
- data/test/fixtures/active_record.rb +12 -0
- data/test/fixtures/poro.rb +28 -3
- data/test/grape_test.rb +5 -5
- data/test/lint_test.rb +9 -0
- data/test/serializable_resource_test.rb +59 -3
- data/test/serializers/associations_test.rb +8 -8
- data/test/serializers/attribute_test.rb +7 -7
- data/test/serializers/caching_configuration_test_isolated.rb +170 -0
- data/test/serializers/meta_test.rb +74 -6
- data/test/serializers/read_attribute_for_serialization_test.rb +79 -0
- data/test/serializers/serialization_test.rb +55 -0
- data/test/support/isolated_unit.rb +3 -0
- data/test/support/rails5_shims.rb +26 -8
- data/test/support/rails_app.rb +38 -18
- data/test/support/serialization_testing.rb +5 -5
- data/test/test_helper.rb +6 -10
- metadata +132 -37
- data/docs/DESIGN.textile +7 -1
- data/lib/active_model/serializer/adapter/cached_serializer.rb +0 -45
- data/lib/active_model/serializer/adapter/fragment_cache.rb +0 -111
- data/lib/active_model/serializer/adapter/json/fragment_cache.rb +0 -13
- data/lib/active_model/serializer/adapter/json_api/deserialization.rb +0 -207
- data/lib/active_model/serializer/adapter/json_api/fragment_cache.rb +0 -21
- data/lib/active_model/serializer/adapter/json_api/link.rb +0 -44
- data/lib/active_model/serializer/adapter/json_api/pagination_links.rb +0 -58
- data/test/active_model_serializers/serialization_context_test.rb +0 -18
- data/test/adapter/fragment_cache_test.rb +0 -38
- data/test/adapter/json_api/resource_type_config_test.rb +0 -71
- data/test/serializers/adapter_for_test.rb +0 -166
- data/test/serializers/cache_test.rb +0 -209
- data/test/support/simplecov.rb +0 -6
- data/test/support/stream_capture.rb +0 -50
- data/test/support/test_case.rb +0 -19
@@ -1,45 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Adapter
|
4
|
-
class CachedSerializer
|
5
|
-
def initialize(serializer)
|
6
|
-
@cached_serializer = serializer
|
7
|
-
@klass = @cached_serializer.class
|
8
|
-
end
|
9
|
-
|
10
|
-
def cache_check(adapter_instance)
|
11
|
-
if cached?
|
12
|
-
@klass._cache.fetch(cache_key, @klass._cache_options) do
|
13
|
-
yield
|
14
|
-
end
|
15
|
-
elsif fragment_cached?
|
16
|
-
FragmentCache.new(adapter_instance, @cached_serializer, adapter_instance.instance_options).fetch
|
17
|
-
else
|
18
|
-
yield
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def cached?
|
23
|
-
@klass._cache && !@klass._cache_only && !@klass._cache_except
|
24
|
-
end
|
25
|
-
|
26
|
-
def fragment_cached?
|
27
|
-
@klass._cache_only && !@klass._cache_except || !@klass._cache_only && @klass._cache_except
|
28
|
-
end
|
29
|
-
|
30
|
-
def cache_key
|
31
|
-
parts = []
|
32
|
-
parts << object_cache_key
|
33
|
-
parts << @klass._cache_digest unless @klass._cache_options && @klass._cache_options[:skip_digest]
|
34
|
-
parts.join('/')
|
35
|
-
end
|
36
|
-
|
37
|
-
def object_cache_key
|
38
|
-
object_time_safe = @cached_serializer.object.updated_at
|
39
|
-
object_time_safe = object_time_safe.strftime('%Y%m%d%H%M%S%9N') if object_time_safe.respond_to?(:strftime)
|
40
|
-
(@klass._cache_key) ? "#{@klass._cache_key}/#{@cached_serializer.object.id}-#{object_time_safe}" : @cached_serializer.object.cache_key
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,111 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Adapter
|
4
|
-
class FragmentCache
|
5
|
-
attr_reader :serializer
|
6
|
-
|
7
|
-
def initialize(adapter, serializer, options)
|
8
|
-
@instance_options = options
|
9
|
-
@adapter = adapter
|
10
|
-
@serializer = serializer
|
11
|
-
end
|
12
|
-
|
13
|
-
# TODO: Use Serializable::Resource
|
14
|
-
# TODO: call +constantize+ less
|
15
|
-
# 1. Create a CachedSerializer and NonCachedSerializer from the serializer class
|
16
|
-
# 2. Serialize the above two with the given adapter
|
17
|
-
# 3. Pass their serializations to the adapter +::fragment_cache+
|
18
|
-
def fetch
|
19
|
-
klass = serializer.class
|
20
|
-
# It will split the serializer into two, one that will be cached and one that will not
|
21
|
-
serializers = fragment_serializer(serializer.object.class.name, klass)
|
22
|
-
|
23
|
-
# Instantiate both serializers
|
24
|
-
cached_serializer = serializers[:cached].constantize.new(serializer.object)
|
25
|
-
non_cached_serializer = serializers[:non_cached].constantize.new(serializer.object)
|
26
|
-
|
27
|
-
cached_adapter = adapter.class.new(cached_serializer, instance_options)
|
28
|
-
non_cached_adapter = adapter.class.new(non_cached_serializer, instance_options)
|
29
|
-
|
30
|
-
# Get serializable hash from both
|
31
|
-
cached_hash = cached_adapter.serializable_hash
|
32
|
-
non_cached_hash = non_cached_adapter.serializable_hash
|
33
|
-
|
34
|
-
# Merge both results
|
35
|
-
adapter.fragment_cache(cached_hash, non_cached_hash)
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
|
40
|
-
attr_reader :instance_options, :adapter
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
# Given a serializer class and a hash of its cached and non-cached serializers
|
45
|
-
# 1. Determine cached attributes from serializer class options
|
46
|
-
# 2. Add cached attributes to cached Serializer
|
47
|
-
# 3. Add non-cached attributes to non-cached Serializer
|
48
|
-
def cached_attributes(klass, serializers)
|
49
|
-
attributes = serializer.class._attributes
|
50
|
-
cached_attributes = (klass._cache_only) ? klass._cache_only : attributes.reject { |attr| klass._cache_except.include?(attr) }
|
51
|
-
non_cached_attributes = attributes - cached_attributes
|
52
|
-
|
53
|
-
cached_attributes.each do |attribute|
|
54
|
-
options = serializer.class._attributes_keys[attribute]
|
55
|
-
options ||= {}
|
56
|
-
# Add cached attributes to cached Serializer
|
57
|
-
serializers[:cached].constantize.attribute(attribute, options)
|
58
|
-
end
|
59
|
-
|
60
|
-
non_cached_attributes.each do |attribute|
|
61
|
-
options = serializer.class._attributes_keys[attribute]
|
62
|
-
options ||= {}
|
63
|
-
# Add non-cached attributes to non-cached Serializer
|
64
|
-
serializers[:non_cached].constantize.attribute(attribute, options)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Given a resource name and its serializer's class
|
69
|
-
# 1. Dyanmically creates a CachedSerializer and NonCachedSerializer
|
70
|
-
# for a given class 'name'
|
71
|
-
# 2. Call
|
72
|
-
# CachedSerializer.cache(serializer._cache_options)
|
73
|
-
# CachedSerializer.fragmented(serializer)
|
74
|
-
# NontCachedSerializer.cache(serializer._cache_options)
|
75
|
-
# 3. Build a hash keyed to the +cached+ and +non_cached+ serializers
|
76
|
-
# 4. Call +cached_attributes+ on the serializer class and the above hash
|
77
|
-
# 5. Return the hash
|
78
|
-
#
|
79
|
-
# @example
|
80
|
-
# When +name+ is <tt>User::Admin</tt>
|
81
|
-
# creates the Serializer classes (if they don't exist).
|
82
|
-
# User_AdminCachedSerializer
|
83
|
-
# User_AdminNOnCachedSerializer
|
84
|
-
#
|
85
|
-
def fragment_serializer(name, klass)
|
86
|
-
cached = "#{to_valid_const_name(name)}CachedSerializer"
|
87
|
-
non_cached = "#{to_valid_const_name(name)}NonCachedSerializer"
|
88
|
-
|
89
|
-
Object.const_set cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(cached)
|
90
|
-
Object.const_set non_cached, Class.new(ActiveModel::Serializer) unless Object.const_defined?(non_cached)
|
91
|
-
|
92
|
-
klass._cache_options ||= {}
|
93
|
-
klass._cache_options[:key] = klass._cache_key if klass._cache_key
|
94
|
-
|
95
|
-
cached.constantize.cache(klass._cache_options)
|
96
|
-
|
97
|
-
cached.constantize.fragmented(serializer)
|
98
|
-
non_cached.constantize.fragmented(serializer)
|
99
|
-
|
100
|
-
serializers = { cached: cached, non_cached: non_cached }
|
101
|
-
cached_attributes(klass, serializers)
|
102
|
-
serializers
|
103
|
-
end
|
104
|
-
|
105
|
-
def to_valid_const_name(name)
|
106
|
-
name.gsub('::', '_')
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
@@ -1,207 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Adapter
|
4
|
-
class JsonApi
|
5
|
-
# NOTE(Experimental):
|
6
|
-
# This is an experimental feature. Both the interface and internals could be subject
|
7
|
-
# to changes.
|
8
|
-
module Deserialization
|
9
|
-
InvalidDocument = Class.new(ArgumentError)
|
10
|
-
|
11
|
-
module_function
|
12
|
-
|
13
|
-
# Transform a JSON API document, containing a single data object,
|
14
|
-
# into a hash that is ready for ActiveRecord::Base.new() and such.
|
15
|
-
# Raises InvalidDocument if the payload is not properly formatted.
|
16
|
-
#
|
17
|
-
# @param [Hash|ActionController::Parameters] document
|
18
|
-
# @param [Hash] options
|
19
|
-
# only: Array of symbols of whitelisted fields.
|
20
|
-
# except: Array of symbols of blacklisted fields.
|
21
|
-
# keys: Hash of translated keys (e.g. :author => :user).
|
22
|
-
# polymorphic: Array of symbols of polymorphic fields.
|
23
|
-
# @return [Hash]
|
24
|
-
#
|
25
|
-
# @example
|
26
|
-
# document = {
|
27
|
-
# data: {
|
28
|
-
# id: 1,
|
29
|
-
# type: 'post',
|
30
|
-
# attributes: {
|
31
|
-
# title: 'Title 1',
|
32
|
-
# date: '2015-12-20'
|
33
|
-
# },
|
34
|
-
# associations: {
|
35
|
-
# author: {
|
36
|
-
# data: {
|
37
|
-
# type: 'user',
|
38
|
-
# id: 2
|
39
|
-
# }
|
40
|
-
# },
|
41
|
-
# second_author: {
|
42
|
-
# data: nil
|
43
|
-
# },
|
44
|
-
# comments: {
|
45
|
-
# data: [{
|
46
|
-
# type: 'comment',
|
47
|
-
# id: 3
|
48
|
-
# },{
|
49
|
-
# type: 'comment',
|
50
|
-
# id: 4
|
51
|
-
# }]
|
52
|
-
# }
|
53
|
-
# }
|
54
|
-
# }
|
55
|
-
# }
|
56
|
-
#
|
57
|
-
# parse(document) #=>
|
58
|
-
# # {
|
59
|
-
# # title: 'Title 1',
|
60
|
-
# # date: '2015-12-20',
|
61
|
-
# # author_id: 2,
|
62
|
-
# # second_author_id: nil
|
63
|
-
# # comment_ids: [3, 4]
|
64
|
-
# # }
|
65
|
-
#
|
66
|
-
# parse(document, only: [:title, :date, :author],
|
67
|
-
# keys: { date: :published_at },
|
68
|
-
# polymorphic: [:author]) #=>
|
69
|
-
# # {
|
70
|
-
# # title: 'Title 1',
|
71
|
-
# # published_at: '2015-12-20',
|
72
|
-
# # author_id: '2',
|
73
|
-
# # author_type: 'people'
|
74
|
-
# # }
|
75
|
-
#
|
76
|
-
def parse!(document, options = {})
|
77
|
-
parse(document, options) do |invalid_payload, reason|
|
78
|
-
fail InvalidDocument, "Invalid payload (#{reason}): #{invalid_payload}"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
# Same as parse!, but returns an empty hash instead of raising InvalidDocument
|
83
|
-
# on invalid payloads.
|
84
|
-
def parse(document, options = {})
|
85
|
-
document = document.dup.permit!.to_h if document.is_a?(ActionController::Parameters)
|
86
|
-
|
87
|
-
validate_payload(document) do |invalid_document, reason|
|
88
|
-
yield invalid_document, reason if block_given?
|
89
|
-
return {}
|
90
|
-
end
|
91
|
-
|
92
|
-
primary_data = document['data']
|
93
|
-
attributes = primary_data['attributes'] || {}
|
94
|
-
attributes['id'] = primary_data['id'] if primary_data['id']
|
95
|
-
relationships = primary_data['relationships'] || {}
|
96
|
-
|
97
|
-
filter_fields(attributes, options)
|
98
|
-
filter_fields(relationships, options)
|
99
|
-
|
100
|
-
hash = {}
|
101
|
-
hash.merge!(parse_attributes(attributes, options))
|
102
|
-
hash.merge!(parse_relationships(relationships, options))
|
103
|
-
|
104
|
-
hash
|
105
|
-
end
|
106
|
-
|
107
|
-
# Checks whether a payload is compliant with the JSON API spec.
|
108
|
-
#
|
109
|
-
# @api private
|
110
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
111
|
-
def validate_payload(payload)
|
112
|
-
unless payload.is_a?(Hash)
|
113
|
-
yield payload, 'Expected hash'
|
114
|
-
return
|
115
|
-
end
|
116
|
-
|
117
|
-
primary_data = payload['data']
|
118
|
-
unless primary_data.is_a?(Hash)
|
119
|
-
yield payload, { data: 'Expected hash' }
|
120
|
-
return
|
121
|
-
end
|
122
|
-
|
123
|
-
attributes = primary_data['attributes'] || {}
|
124
|
-
unless attributes.is_a?(Hash)
|
125
|
-
yield payload, { data: { attributes: 'Expected hash or nil' } }
|
126
|
-
return
|
127
|
-
end
|
128
|
-
|
129
|
-
relationships = primary_data['relationships'] || {}
|
130
|
-
unless relationships.is_a?(Hash)
|
131
|
-
yield payload, { data: { relationships: 'Expected hash or nil' } }
|
132
|
-
return
|
133
|
-
end
|
134
|
-
|
135
|
-
relationships.each do |(key, value)|
|
136
|
-
unless value.is_a?(Hash) && value.key?('data')
|
137
|
-
yield payload, { data: { relationships: { key => 'Expected hash with :data key' } } }
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
142
|
-
|
143
|
-
# @api private
|
144
|
-
def filter_fields(fields, options)
|
145
|
-
if (only = options[:only])
|
146
|
-
fields.slice!(*Array(only).map(&:to_s))
|
147
|
-
elsif (except = options[:except])
|
148
|
-
fields.except!(*Array(except).map(&:to_s))
|
149
|
-
end
|
150
|
-
end
|
151
|
-
|
152
|
-
# @api private
|
153
|
-
def field_key(field, options)
|
154
|
-
(options[:keys] || {}).fetch(field.to_sym, field).to_sym
|
155
|
-
end
|
156
|
-
|
157
|
-
# @api private
|
158
|
-
def parse_attributes(attributes, options)
|
159
|
-
attributes
|
160
|
-
.map { |(k, v)| { field_key(k, options) => v } }
|
161
|
-
.reduce({}, :merge)
|
162
|
-
end
|
163
|
-
|
164
|
-
# Given an association name, and a relationship data attribute, build a hash
|
165
|
-
# mapping the corresponding ActiveRecord attribute to the corresponding value.
|
166
|
-
#
|
167
|
-
# @example
|
168
|
-
# parse_relationship(:comments, [{ 'id' => '1', 'type' => 'comments' },
|
169
|
-
# { 'id' => '2', 'type' => 'comments' }],
|
170
|
-
# {})
|
171
|
-
# # => { :comment_ids => ['1', '2'] }
|
172
|
-
# parse_relationship(:author, { 'id' => '1', 'type' => 'users' }, {})
|
173
|
-
# # => { :author_id => '1' }
|
174
|
-
# parse_relationship(:author, nil, {})
|
175
|
-
# # => { :author_id => nil }
|
176
|
-
# @param [Symbol] assoc_name
|
177
|
-
# @param [Hash] assoc_data
|
178
|
-
# @param [Hash] options
|
179
|
-
# @return [Hash{Symbol, Object}]
|
180
|
-
#
|
181
|
-
# @api private
|
182
|
-
def parse_relationship(assoc_name, assoc_data, options)
|
183
|
-
prefix_key = field_key(assoc_name, options).to_s.singularize
|
184
|
-
hash =
|
185
|
-
if assoc_data.is_a?(Array)
|
186
|
-
{ "#{prefix_key}_ids".to_sym => assoc_data.map { |ri| ri['id'] } }
|
187
|
-
else
|
188
|
-
{ "#{prefix_key}_id".to_sym => assoc_data ? assoc_data['id'] : nil }
|
189
|
-
end
|
190
|
-
|
191
|
-
polymorphic = (options[:polymorphic] || []).include?(assoc_name.to_sym)
|
192
|
-
hash.merge!("#{prefix_key}_type".to_sym => assoc_data['type']) if polymorphic
|
193
|
-
|
194
|
-
hash
|
195
|
-
end
|
196
|
-
|
197
|
-
# @api private
|
198
|
-
def parse_relationships(relationships, options)
|
199
|
-
relationships
|
200
|
-
.map { |(k, v)| parse_relationship(k, v['data'], options) }
|
201
|
-
.reduce({}, :merge)
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Adapter
|
4
|
-
class JsonApi
|
5
|
-
class FragmentCache
|
6
|
-
def fragment_cache(root, cached_hash, non_cached_hash)
|
7
|
-
hash = {}
|
8
|
-
core_cached = cached_hash.first
|
9
|
-
core_non_cached = non_cached_hash.first
|
10
|
-
no_root_cache = cached_hash.delete_if { |key, value| key == core_cached[0] }
|
11
|
-
no_root_non_cache = non_cached_hash.delete_if { |key, value| key == core_non_cached[0] }
|
12
|
-
cached_resource = (core_cached[1]) ? core_cached[1].deep_merge(core_non_cached[1]) : core_non_cached[1]
|
13
|
-
hash = (root) ? { root => cached_resource } : cached_resource
|
14
|
-
|
15
|
-
hash.deep_merge no_root_non_cache.deep_merge no_root_cache
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
module ActiveModel
|
2
|
-
class Serializer
|
3
|
-
module Adapter
|
4
|
-
class JsonApi
|
5
|
-
class Link
|
6
|
-
def initialize(serializer, value)
|
7
|
-
@object = serializer.object
|
8
|
-
@scope = serializer.scope
|
9
|
-
|
10
|
-
# Use the return value of the block unless it is nil.
|
11
|
-
if value.respond_to?(:call)
|
12
|
-
@value = instance_eval(&value)
|
13
|
-
else
|
14
|
-
@value = value
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def href(value)
|
19
|
-
@href = value
|
20
|
-
nil
|
21
|
-
end
|
22
|
-
|
23
|
-
def meta(value)
|
24
|
-
@meta = value
|
25
|
-
nil
|
26
|
-
end
|
27
|
-
|
28
|
-
def as_json
|
29
|
-
return @value if @value
|
30
|
-
|
31
|
-
hash = { href: @href }
|
32
|
-
hash.merge!(meta: @meta) if @meta
|
33
|
-
|
34
|
-
hash
|
35
|
-
end
|
36
|
-
|
37
|
-
protected
|
38
|
-
|
39
|
-
attr_reader :object, :scope
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,58 +0,0 @@
|
|
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
|