-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathresource_tree.rb
More file actions
236 lines (197 loc) · 8.6 KB
/
Copy pathresource_tree.rb
File metadata and controls
236 lines (197 loc) · 8.6 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# frozen_string_literal: true
module JSONAPI
# A tree structure representing the resource structure of the requested resource(s). This is an intermediate structure
# used to keep track of the resources, by identity, found at different included relationships. It will be flattened and
# the resource instances will be fetched from the cache or the record store.
class ResourceTree
attr_reader :fragments, :related_resource_trees
# Gets the related Resource Id Tree for a relationship, and creates it first if it does not exist
#
# @param relationship [JSONAPI::Relationship]
#
# @return [JSONAPI::RelatedResourceTree] the new or existing resource id tree for the requested relationship
def get_related_resource_tree(relationship)
relationship_name = relationship.name.to_sym
@related_resource_trees[relationship_name] ||= RelatedResourceTree.new(relationship, self)
end
# Adds each Resource Fragment to the Resources hash
#
# @param fragments [Hash]
# @param include_related [Hash]
#
# @return [null]
def add_resource_fragments(fragments, include_related)
fragments.each_value do |fragment|
add_resource_fragment(fragment, include_related)
end
end
# Adds a Resource Fragment to the fragments hash
#
# @param fragment [JSONAPI::ResourceFragment]
# @param include_related [Hash]
#
# @return [null]
def add_resource_fragment(fragment, include_related)
init_included_relationships(fragment, include_related)
@fragments[fragment.identity] = fragment
end
# Adds each Resource to the fragments hash
#
# @param resource [Hash]
# @param include_related [Hash]
#
# @return [null]
def add_resources(resources, include_related)
resources.each do |resource|
add_resource_fragment(JSONAPI::ResourceFragment.new(resource.identity, resource: resource), include_related)
end
end
# Adds a Resource to the fragments hash
#
# @param fragment [JSONAPI::ResourceFragment]
# @param include_related [Hash]
#
# @return [null]
def add_resource(resource, include_related)
add_resource_fragment(JSONAPI::ResourceFragment.new(resource.identity, resource: resource), include_related)
end
private
def init_included_relationships(fragment, include_related)
include_related && include_related.each_key do |relationship_name|
fragment.initialize_related(relationship_name)
end
end
def load_included(resource_klass, source_resource_tree, include_related, options)
include_related.try(:each_key) do |key|
relationship = resource_klass._relationship(key)
relationship_name = relationship.name.to_sym
find_related_resource_options = options.except(:filters, :sort_criteria, :paginator)
find_related_resource_options[:sort_criteria] = relationship.resource_klass.default_sort
find_related_resource_options[:cache] = resource_klass.caching?
related_fragments = resource_klass.find_included_fragments(source_resource_tree.fragments.values,
relationship_name,
find_related_resource_options)
related_resource_tree = source_resource_tree.get_related_resource_tree(relationship)
related_resource_tree.add_resource_fragments(related_fragments, include_related[key][:include_related])
# Now recursively get the related resources for the currently found resources
load_included(relationship.resource_klass,
related_resource_tree,
include_related[relationship_name][:include_related],
options)
end
end
def add_resources_to_tree(resource_klass,
tree,
resources,
include_related,
source_rid: nil,
source_relationship_name: nil,
connect_source_identity: true)
fragments = {}
resources.each do |resource|
next unless resource
# fragments[resource.identity] ||= ResourceFragment.new(resource.identity, resource: resource)
# resource_fragment = fragments[resource.identity]
# ToDo: revert when not needed for testing
resource_fragment = if fragments[resource.identity]
fragments[resource.identity]
else
fragments[resource.identity] = ResourceFragment.new(resource.identity, resource: resource)
fragments[resource.identity]
end
if resource.class.caching?
resource_fragment.cache = resource.cache_field_value
end
linkage_relationships = resource_klass.to_one_relationships_for_linkage(resource.class, include_related)
linkage_relationships.each do |relationship_name|
related_resource = resource.send(relationship_name)
resource_fragment.add_related_identity(relationship_name, related_resource&.identity)
end
if source_rid && connect_source_identity
resource_fragment.add_related_from(source_rid)
source_klass = source_rid.resource_klass
related_relationship_name = source_klass._relationships[source_relationship_name].inverse_relationship
if related_relationship_name
resource_fragment.add_related_identity(related_relationship_name, source_rid)
end
end
end
tree.add_resource_fragments(fragments, include_related)
end
end
class PrimaryResourceTree < ResourceTree
# Creates a PrimaryResourceTree with no resources and no related ResourceTrees
def initialize(fragments: nil, resources: nil, resource: nil, include_related: nil, options: nil)
@fragments ||= {}
@related_resource_trees ||= {}
if fragments || resources || resource
if fragments
add_resource_fragments(fragments, include_related)
end
if resources
add_resources(resources, include_related)
end
if resource
add_resource(resource, include_related)
end
complete_includes!(include_related, options)
end
end
# Adds a Resource Fragment to the fragments hash
#
# @param fragment [JSONAPI::ResourceFragment]
# @param include_related [Hash]
#
# @return [null]
def add_resource_fragment(fragment, include_related)
fragment.primary = true
super(fragment, include_related)
end
def complete_includes!(include_related, options)
# ToDo: can we skip if more than one resource_klass found?
resource_klasses = Set.new
@fragments.each_key { |identity| resource_klasses << identity.resource_klass }
resource_klasses.each { |resource_klass| load_included(resource_klass, self, include_related, options)}
self
end
end
class RelatedResourceTree < ResourceTree
attr_reader :parent_relationship, :source_resource_tree
# Creates a RelatedResourceTree with no resources and no related ResourceTrees. A connection to the parent
# ResourceTree is maintained.
#
# @param parent_relationship [JSONAPI::Relationship]
# @param source_resource_tree [JSONAPI::ResourceTree]
#
# @return [JSONAPI::RelatedResourceTree] the new or existing resource id tree for the requested relationship
def initialize(parent_relationship, source_resource_tree)
@fragments ||= {}
@related_resource_trees ||= {}
@parent_relationship = parent_relationship
@parent_relationship_name = parent_relationship.name.to_sym
@source_resource_tree = source_resource_tree
end
# Adds a Resource Fragment to the fragments hash
#
# @param fragment [JSONAPI::ResourceFragment]
# @param include_related [Hash]
#
# @return [null]
def add_resource_fragment(fragment, include_related)
init_included_relationships(fragment, include_related)
fragment.related_from.each do |rid|
@source_resource_tree.fragments[rid].add_related_identity(parent_relationship.name, fragment.identity)
end
if @fragments[fragment.identity]
@fragments[fragment.identity].related_from.merge(fragment.related_from)
fragment.related.each_pair do |relationship_name, rids|
if rids
@fragments[fragment.identity].merge_related_identities(relationship_name, rids)
end
end
else
@fragments[fragment.identity] = fragment
end
end
end
end