-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
json_api.rb
183 lines (151 loc) · 5.99 KB
/
json_api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
require 'active_model/serializer/adapter/json_api/fragment_cache'
require 'active_model/serializer/adapter/json_api/pagination_links'
module ActiveModel
class Serializer
class Adapter
class JsonApi < Adapter
def initialize(serializer, options = {})
super
@hash = { data: [] }
if fields = options.delete(:fields)
@fieldset = ActiveModel::Serializer::Fieldset.new(fields, serializer.json_key)
else
@fieldset = options[:fieldset]
end
end
def serializable_hash(options = nil)
options ||= {}
if serializer.respond_to?(:each)
serializer.each do |s|
result = self.class.new(s, @options.merge(fieldset: @fieldset)).serializable_hash(options)
@hash[:data] << result[:data]
if result[:included]
@hash[:included] ||= []
@hash[:included] |= result[:included]
end
end
add_links(options)
else
@hash[:data] = attributes_for_serializer(serializer, options)
add_resource_relationships(@hash[:data], serializer)
end
@hash
end
def fragment_cache(cached_hash, non_cached_hash)
root = false if @options.include?(:include)
JsonApi::FragmentCache.new().fragment_cache(root, cached_hash, non_cached_hash)
end
private
def add_relationships(resource, name, serializers)
resource[:relationships] ||= {}
resource[:relationships][name] ||= { data: [] }
resource[:relationships][name][:data] += serializers.map { |serializer| { type: serializer.json_api_type, id: serializer.id.to_s } }
end
def add_relationship(resource, name, serializer, val=nil)
resource[:relationships] ||= {}
resource[:relationships][name] = { data: val }
if serializer && serializer.object
resource[:relationships][name][:data] = { type: serializer.json_api_type, id: serializer.id.to_s }
end
end
def add_included(resource_name, serializers, parent = nil)
unless serializers.respond_to?(:each)
return unless serializers.object
serializers = Array(serializers)
end
resource_path = [parent, resource_name].compact.join('.')
if include_assoc?(resource_path)
@hash[:included] ||= []
serializers.each do |serializer|
attrs = attributes_for_serializer(serializer, @options)
add_resource_relationships(attrs, serializer, add_included: false)
@hash[:included].push(attrs) unless @hash[:included].include?(attrs)
end
end
serializers.each do |serializer|
serializer.associations.each do |association|
serializer = association.serializer
add_included(association.key, serializer, resource_path) if serializer
end if include_nested_assoc? resource_path
end
end
def attributes_for_serializer(serializer, options)
if serializer.respond_to?(:each)
result = []
serializer.each do |object|
result << resource_object_for(object, options)
end
else
result = resource_object_for(serializer, options)
end
result
end
def resource_object_for(serializer, options)
options[:fields] = @fieldset && @fieldset.fields_for(serializer)
options[:required_fields] = [:id, :json_api_type]
cache_check(serializer) do
attributes = serializer.attributes(options)
result = {
id: attributes.delete(:id).to_s,
type: attributes.delete(:json_api_type)
}
result[:attributes] = attributes if attributes.any?
result
end
end
def include_assoc?(assoc)
return false unless @options[:include]
check_assoc("#{assoc}$")
end
def include_nested_assoc?(assoc)
return false unless @options[:include]
check_assoc("#{assoc}.")
end
def check_assoc(assoc)
include_opt = @options[:include]
include_opt = include_opt.split(',') if include_opt.is_a?(String)
include_opt.any? do |s|
s.match(/^#{assoc.gsub('.', '\.')}/)
end
end
def add_resource_relationships(attrs, serializer, options = {})
options[:add_included] = options.fetch(:add_included, true)
serializer.associations.each do |association|
key = association.key
serializer = association.serializer
opts = association.options
attrs[:relationships] ||= {}
if serializer.respond_to?(:each)
add_relationships(attrs, key, serializer)
else
if opts[:virtual_value]
add_relationship(attrs, key, nil, opts[:virtual_value])
else
add_relationship(attrs, key, serializer)
end
end
if options[:add_included]
Array(serializer).each do |s|
add_included(key, s)
end
end
end
end
def add_links(options)
links = @hash.fetch(:links) { {} }
resources = serializer.instance_variable_get(:@resource)
@hash[:links] = add_pagination_links(links, resources, options) if is_paginated?(resources)
end
def add_pagination_links(links, resources, options)
pagination_links = JsonApi::PaginationLinks.new(resources, options[:context]).serializable_hash(options)
links.update(pagination_links)
end
def is_paginated?(resource)
resource.respond_to?(:current_page) &&
resource.respond_to?(:total_pages) &&
resource.respond_to?(:size)
end
end
end
end
end