Class: Graphiti::Sideload
Defined Under Namespace
Classes: BelongsTo, HasMany, HasOne, ManyToMany, PolymorphicBelongsTo
Constant Summary
collapse
- HOOK_ACTIONS =
[:save, :create, :update, :destroy, :disassociate]
- TYPES =
[:has_many, :belongs_to, :has_one, :many_to_many]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#assign(parents, children) ⇒ Object
-
#assign_each(parent, children) ⇒ Object
-
#associate(parent, child) ⇒ Object
-
#associate_all(parent, children) ⇒ Object
-
#association_name ⇒ Object
-
#base_scope ⇒ Object
-
#build_resource_proxy(parents, query, graph_parent) ⇒ Object
-
#clear_resources ⇒ Object
-
#create_remote_resource ⇒ Object
-
#customized_base_scope? ⇒ Boolean
-
#default_render_resource_ids? ⇒ Boolean
-
#description ⇒ Object
-
#disassociate(parent, child) ⇒ Object
-
#errors ⇒ Object
-
#fire_hooks!(parent, objects, method) ⇒ Object
-
#foreign_key ⇒ Object
-
#future_resolve(parents, query, graph_parent, &proxy_block) ⇒ Object
A scope_proc builds a Scope rather than a proxy, and a Scope's cache key omits what a proxy's carries.
-
#guarded? ⇒ Boolean
-
#ids_for_parents(parents) ⇒ Object
-
#infer_foreign_key ⇒ Object
-
#initialize(name, opts) ⇒ Sideload
constructor
A new instance of Sideload.
-
#link? ⇒ Boolean
-
#link_extra_fields ⇒ Object
-
#link_filter(parents) ⇒ Object
-
#link_mode ⇒ Object
A custom link block means the author wants the link, so a false default does not silence it.
-
#load(parents, query, graph_parent, &proxy_block) ⇒ Object
-
#load_params(parents, query) ⇒ Object
-
#non_default_options ⇒ Object
-
#parent_resource ⇒ Object
-
#performant_assign? ⇒ Boolean
-
#polymorphic_child? ⇒ Boolean
-
#polymorphic_has_many? ⇒ Boolean
-
#polymorphic_has_one? ⇒ Boolean
-
#polymorphic_parent? ⇒ Boolean
-
#primary_key ⇒ Object
-
#readable? ⇒ Boolean
-
#readable_guard_name ⇒ Object
-
#readable_guarded? ⇒ Boolean
-
#remote? ⇒ Boolean
-
#render_resource_ids? ⇒ Boolean
-
#resolve(parents, query, graph_parent, &proxy_block) ⇒ Object
-
#resource ⇒ Object
-
#resource_class ⇒ Object
-
#resource_class_loaded? ⇒ Boolean
private
-
#resource_ids_blocker ⇒ Object
-
#resource_ids_from_foreign_key? ⇒ Boolean
Every check behind the blocker is static sideload configuration, and this is asked once per rendered record per relationship.
-
#scope(parents) ⇒ Object
-
#shared_remote? ⇒ Boolean
The parent resource is a remote, AND the sideload is a remote to the same endpoint.
-
#single? ⇒ Boolean
-
#sync_load(parents, query, graph_parent, &proxy_block) ⇒ Object
-
#sync_resolve(parents, query, graph_parent, &proxy_block) ⇒ Object
Called by a scope that already decided to stay inline, so it must not consult the pool again.
-
#type ⇒ Object
-
#writable? ⇒ Boolean
Constructor Details
#initialize(name, opts) ⇒ Sideload
Returns a new instance of Sideload.
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
|
# File 'lib/graphiti/sideload.rb', line 20
def initialize(name, opts)
@name = name
validate_options!(opts)
translate_deprecated_options!(opts)
@parent_resource_class = opts[:parent_resource]
@resource_class_name = opts[:resource]
@primary_key = opts[:primary_key]
@foreign_key = opts[:foreign_key]
@type = opts[:type]
@base_scope = opts[:base_scope]
@readable = opts[:readable]
@writable = opts[:writable]
@as = opts[:as]
@link = opts[:link]
unless @link.nil? || Resource::LINK_MODES.include?(@link)
raise Errors::InvalidLinkRendering.new(@parent_resource_class, :"#{name} link", @link)
end
@single = opts[:single]
@remote = opts[:remote]
apply_belongs_to_many_filter if type == :many_to_many
@description = opts[:description]
@polymorphic_as = opts[:polymorphic_as]
@group_name = opts[:group_name]
@polymorphic_child = opts[:polymorphic_child]
@parent = opts[:parent]
@render_resource_ids = opts[:resource_ids]
if polymorphic_child?
parent.resource.polymorphic << resource_class
end
if remote?
@resource_class = create_remote_resource
end
end
|
Instance Attribute Details
#group_name ⇒ Object
Returns the value of attribute group_name.
6
7
8
|
# File 'lib/graphiti/sideload.rb', line 6
def group_name
@group_name
end
|
Returns the value of attribute link.
6
7
8
|
# File 'lib/graphiti/sideload.rb', line 6
def link
@link
end
|
Returns the value of attribute name.
6
7
8
|
# File 'lib/graphiti/sideload.rb', line 6
def name
@name
end
|
Returns the value of attribute parent.
6
7
8
|
# File 'lib/graphiti/sideload.rb', line 6
def parent
@parent
end
|
#parent_resource_class ⇒ Object
Returns the value of attribute parent_resource_class.
6
7
8
|
# File 'lib/graphiti/sideload.rb', line 6
def parent_resource_class
@parent_resource_class
end
|
#polymorphic_as ⇒ Object
Returns the value of attribute polymorphic_as.
6
7
8
|
# File 'lib/graphiti/sideload.rb', line 6
def polymorphic_as
@polymorphic_as
end
|
Class Method Details
.after_save(only: [], except: [], &blk) ⇒ Object
388
389
390
391
392
393
394
395
|
# File 'lib/graphiti/sideload.rb', line 388
def self.after_save(only: [], except: [], &blk)
actions = HOOK_ACTIONS - except
actions = only & actions
actions = [:save] if only.empty? && except.empty?
actions.each do |a|
hooks[:"after_#{a}"] << blk
end
end
|
.assign(&blk) ⇒ Object
64
65
66
|
# File 'lib/graphiti/sideload.rb', line 64
def self.assign(&blk)
self.assign_proc = blk
end
|
.assign_each(&blk) ⇒ Object
68
69
70
|
# File 'lib/graphiti/sideload.rb', line 68
def self.assign_each(&blk)
self.assign_each_proc = blk
end
|
397
398
399
400
401
402
403
404
|
# File 'lib/graphiti/sideload.rb', line 397
def self.hooks
@hooks ||= {}.tap do |h|
HOOK_ACTIONS.each do |a|
h[:"after_#{a}"] = []
h[:"before_#{a}"] = []
end
end
end
|
.link(&blk) ⇒ Object
80
81
82
|
# File 'lib/graphiti/sideload.rb', line 80
def self.link(&blk)
self.link_proc = blk
end
|
.params(&blk) ⇒ Object
72
73
74
|
# File 'lib/graphiti/sideload.rb', line 72
def self.params(&blk)
self.params_proc = blk
end
|
.pre_load(&blk) ⇒ Object
76
77
78
|
# File 'lib/graphiti/sideload.rb', line 76
def self.pre_load(&blk)
self.pre_load_proc = blk
end
|
.scope(&blk) ⇒ Object
60
61
62
|
# File 'lib/graphiti/sideload.rb', line 60
def self.scope(&blk)
self.scope_proc = blk
end
|
Instance Method Details
#assign(parents, children) ⇒ Object
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
# File 'lib/graphiti/sideload.rb', line 328
def assign(parents, children)
track_associated = type == :has_one
associated = [] if track_associated
if performant_assign?
map = child_map(children)
end
parents.each do |parent|
relevant_children = if performant_assign?
children_for(parent, map) || []
else
fire_assign_each(parent, children)
end
if relevant_children.is_a?(Array)
associated |= relevant_children if track_associated
associate_all(parent, relevant_children)
else
associated << relevant_children if track_associated && relevant_children
associate(parent, relevant_children)
end
end
children.replace(associated) if track_associated
end
|
#assign_each(parent, children) ⇒ Object
242
243
244
|
# File 'lib/graphiti/sideload.rb', line 242
def assign_each(parent, children)
raise "Override #assign_each in subclass"
end
|
#associate(parent, child) ⇒ Object
419
420
421
|
# File 'lib/graphiti/sideload.rb', line 419
def associate(parent, child)
parent_resource.associate(parent, child, association_name, type)
end
|
#associate_all(parent, children) ⇒ Object
415
416
417
|
# File 'lib/graphiti/sideload.rb', line 415
def associate_all(parent, children)
parent_resource.associate_all(parent, children, association_name, type)
end
|
#association_name ⇒ Object
229
230
231
|
# File 'lib/graphiti/sideload.rb', line 229
def association_name
@as || name
end
|
#base_scope ⇒ Object
259
260
261
262
263
264
265
|
# File 'lib/graphiti/sideload.rb', line 259
def base_scope
if @base_scope
@base_scope.respond_to?(:call) ? @base_scope.call : @base_scope
else
resource.base_scope
end
end
|
#build_resource_proxy(parents, query, graph_parent) ⇒ Object
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
# File 'lib/graphiti/sideload.rb', line 267
def build_resource_proxy(parents, query, graph_parent)
params = nil
opts = nil
proxy = nil
with_error_handling Errors::SideloadParamsError do
params = load_params(parents, query)
params_proc&.call(params, parents, context)
return [] if blank_query?(params)
opts = load_options(parents, query)
opts[:sideload] = self
opts[:parent] = graph_parent
end
with_error_handling(Errors::SideloadQueryBuildingError) do
scope = base_scope
scope[:foreign_key] = foreign_key if remote?
proxy = resource.class._all(params, opts, scope)
pre_load_proc&.call(proxy, parents)
end
proxy
end
|
#clear_resources ⇒ Object
323
324
325
326
|
# File 'lib/graphiti/sideload.rb', line 323
def clear_resources
@resource = nil
@parent_resource = nil
end
|
#create_remote_resource ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/graphiti/sideload.rb', line 84
def create_remote_resource
remote_url = @remote
klass = Class.new(Graphiti::Resource) {
self.adapter = Graphiti::Adapters::GraphitiAPI
self.model = OpenStruct
self.remote = remote_url
self.validate_requests = false
self.validate_links = false
}
name = "#{parent_resource_class.name}.#{@name}.remote"
klass.class_eval("def self.name;'#{name}';end", __FILE__, __LINE__)
klass
end
|
#customized_base_scope? ⇒ Boolean
139
140
141
|
# File 'lib/graphiti/sideload.rb', line 139
def customized_base_scope?
!!@base_scope
end
|
#default_render_resource_ids? ⇒ Boolean
173
174
175
|
# File 'lib/graphiti/sideload.rb', line 173
def default_render_resource_ids?
false
end
|
#description ⇒ Object
254
255
256
257
|
# File 'lib/graphiti/sideload.rb', line 254
def description
return @description if @description.present?
parent_resource_class.resolve_i18n_field_description(name, field_type: :relationships)
end
|
#disassociate(parent, child) ⇒ Object
423
424
425
|
# File 'lib/graphiti/sideload.rb', line 423
def disassociate(parent, child)
parent_resource.disassociate(parent, child, association_name, type)
end
|
98
99
100
|
# File 'lib/graphiti/sideload.rb', line 98
def errors
@errors ||= []
end
|
#fire_hooks!(parent, objects, method) ⇒ Object
406
407
408
409
410
411
412
413
|
# File 'lib/graphiti/sideload.rb', line 406
def fire_hooks!(parent, objects, method)
return unless self.class.hooks
all = self.class.hooks[:"after_#{method}"] + self.class.hooks[:after_save]
all.compact.each do |hook|
resource.instance_exec(parent, objects, &hook)
end
end
|
#foreign_key ⇒ Object
225
226
227
|
# File 'lib/graphiti/sideload.rb', line 225
def foreign_key
@foreign_key ||= infer_foreign_key
end
|
#future_resolve(parents, query, graph_parent, &proxy_block) ⇒ Object
A scope_proc builds a Scope rather than a proxy, and a Scope's cache key omits what a proxy's carries.
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/graphiti/sideload.rb', line 376
def future_resolve(parents, query, graph_parent, &proxy_block)
assert_singular!(parents)
if self.class.scope_proc
build_sideload_scope(parents, query, graph_parent).future_resolve do |sideload_results|
fire_assign(parents, sideload_results)
end
else
future_load(parents, query, graph_parent, &proxy_block)
end
end
|
#guarded? ⇒ Boolean
114
115
116
|
# File 'lib/graphiti/sideload.rb', line 114
def guarded?
dynamic_flag?(@readable) || dynamic_flag?(@writable)
end
|
#ids_for_parents(parents) ⇒ Object
427
428
429
430
431
432
|
# File 'lib/graphiti/sideload.rb', line 427
def ids_for_parents(parents)
parent_ids = parents.map(&primary_key)
parent_ids.compact!
parent_ids.uniq!
parent_ids
end
|
#infer_foreign_key ⇒ Object
307
308
309
310
311
312
|
# File 'lib/graphiti/sideload.rb', line 307
def infer_foreign_key
model = parent_resource_class.model
namespace = namespace_for(model)
model_name = model.name.gsub("#{namespace}::", "")
:"#{model_name.underscore}_id"
end
|
#link? ⇒ Boolean
185
186
187
|
# File 'lib/graphiti/sideload.rb', line 185
def link?
link_mode != false
end
|
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/graphiti/sideload.rb', line 193
def
return unless context&.respond_to?(:params)
= [association_name, resource.type].find { |param|
context.params.dig(:extra_fields, param)
}
if
= context.params.dig(:extra_fields, )
{resource.type => }
end
end
|
#link_filter(parents) ⇒ Object
189
190
191
|
# File 'lib/graphiti/sideload.rb', line 189
def link_filter(parents)
base_filter(parents)
end
|
#link_mode ⇒ Object
A custom link block means the author wants the link, so a false default does not silence it.
178
179
180
181
182
183
|
# File 'lib/graphiti/sideload.rb', line 178
def link_mode
return @link unless @link.nil?
default = @parent_resource_class.relationship_links
(link_proc && default == false) ? true : default
end
|
#load(parents, query, graph_parent, &proxy_block) ⇒ Object
292
293
294
295
296
297
298
|
# File 'lib/graphiti/sideload.rb', line 292
def load(parents, query, graph_parent, &proxy_block)
if Scope.resolve_synchronously?
sync_load(parents, query, graph_parent, &proxy_block)
else
future_load(parents, query, graph_parent, &proxy_block).value!
end
end
|
#load_params(parents, query) ⇒ Object
250
251
252
|
# File 'lib/graphiti/sideload.rb', line 250
def load_params(parents, query)
raise "Override #load_params in subclass"
end
|
#non_default_options ⇒ Object
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/graphiti/sideload.rb', line 126
def non_default_options
options = {}
options[:as] = association_name if @as
options[:primary_key] = primary_key unless primary_key == :id
options[:single] = true if single?
options[:remote] = @remote if remote?
options[:link] = @link unless @link.nil?
options[:readable] = @readable unless @readable.nil? || @readable == true
options[:writable] = @writable unless @writable.nil? || @writable == true
options[:resource_ids] = @render_resource_ids unless @render_resource_ids.nil?
options
end
|
#parent_resource ⇒ Object
318
319
320
|
# File 'lib/graphiti/sideload.rb', line 318
def parent_resource
@parent_resource ||= parent_resource_class.new
end
|
434
435
436
|
# File 'lib/graphiti/sideload.rb', line 434
def performant_assign?
!self.class.assign_each_proc
end
|
#polymorphic_child? ⇒ Boolean
217
218
219
|
# File 'lib/graphiti/sideload.rb', line 217
def polymorphic_child?
!!@polymorphic_child
end
|
#polymorphic_has_many? ⇒ Boolean
151
152
153
|
# File 'lib/graphiti/sideload.rb', line 151
def polymorphic_has_many?
!!@polymorphic_as
end
|
#polymorphic_has_one? ⇒ Boolean
147
148
149
|
# File 'lib/graphiti/sideload.rb', line 147
def polymorphic_has_one?
!!@polymorphic_as
end
|
#polymorphic_parent? ⇒ Boolean
213
214
215
|
# File 'lib/graphiti/sideload.rb', line 213
def polymorphic_parent?
resource.polymorphic?
end
|
#primary_key ⇒ Object
221
222
223
|
# File 'lib/graphiti/sideload.rb', line 221
def primary_key
@primary_key ||= :id
end
|
#readable? ⇒ Boolean
106
107
108
|
# File 'lib/graphiti/sideload.rb', line 106
def readable?
evaluate_flag @readable
end
|
#readable_guard_name ⇒ Object
122
123
124
|
# File 'lib/graphiti/sideload.rb', line 122
def readable_guard_name
@readable.to_sym if @readable.is_a?(Symbol) || @readable.is_a?(String)
end
|
#readable_guarded? ⇒ Boolean
118
119
120
|
# File 'lib/graphiti/sideload.rb', line 118
def readable_guarded?
dynamic_flag?(@readable)
end
|
#remote? ⇒ Boolean
102
103
104
|
# File 'lib/graphiti/sideload.rb', line 102
def remote?
!!@remote
end
|
#render_resource_ids? ⇒ Boolean
167
168
169
170
171
|
# File 'lib/graphiti/sideload.rb', line 167
def render_resource_ids?
return !!@render_resource_ids unless @render_resource_ids.nil?
default_render_resource_ids?
end
|
#resolve(parents, query, graph_parent, &proxy_block) ⇒ Object
353
354
355
356
357
358
359
|
# File 'lib/graphiti/sideload.rb', line 353
def resolve(parents, query, graph_parent, &proxy_block)
if Scope.resolve_synchronously?
sync_resolve(parents, query, graph_parent, &proxy_block)
else
future_resolve(parents, query, graph_parent, &proxy_block).value!
end
end
|
314
315
316
|
# File 'lib/graphiti/sideload.rb', line 314
def resource
@resource ||= resource_class.new
end
|
#resource_class ⇒ Object
233
234
235
236
|
# File 'lib/graphiti/sideload.rb', line 233
def resource_class
@resource_class ||= (@resource_class_name.is_a?(String) ? @resource_class_name.constantize : @resource_class_name) ||
infer_resource_class
end
|
#resource_class_loaded? ⇒ Boolean
This method is part of a private API.
You should avoid using this method if possible, as it may be removed or be changed in the future.
439
440
441
442
443
444
|
# File 'lib/graphiti/sideload.rb', line 439
def resource_class_loaded?
resource_class
true
rescue Graphiti::Errors::ResourceNotFound
false
end
|
#resource_ids_blocker ⇒ Object
163
164
165
|
# File 'lib/graphiti/sideload.rb', line 163
def resource_ids_blocker
:no_foreign_key_on_parent
end
|
#resource_ids_from_foreign_key? ⇒ Boolean
Every check behind the blocker is static sideload configuration, and this
is asked once per rendered record per relationship.
157
158
159
160
161
|
# File 'lib/graphiti/sideload.rb', line 157
def resource_ids_from_foreign_key?
return @resource_ids_from_foreign_key unless @resource_ids_from_foreign_key.nil?
@resource_ids_from_foreign_key = resource_ids_blocker.nil?
end
|
#scope(parents) ⇒ Object
238
239
240
|
# File 'lib/graphiti/sideload.rb', line 238
def scope(parents)
raise "No #scope defined for sideload with name '#{name}'. Make sure to define this in your adapter, or pass a block that defines the scope."
end
|
#shared_remote? ⇒ Boolean
The parent resource is a remote,
AND the sideload is a remote to the same endpoint
208
209
210
211
|
# File 'lib/graphiti/sideload.rb', line 208
def shared_remote?
resource.remote? &&
resource.remote_base_url = parent_resource_class.remote_base_url
end
|
#single? ⇒ Boolean
143
144
145
|
# File 'lib/graphiti/sideload.rb', line 143
def single?
!!@single
end
|
#sync_load(parents, query, graph_parent, &proxy_block) ⇒ Object
300
301
302
303
304
|
# File 'lib/graphiti/sideload.rb', line 300
def sync_load(parents, query, graph_parent, &proxy_block)
proxy = build_resource_proxy(parents, query, graph_parent)
proxy_block&.call(proxy)
proxy.to_a
end
|
#sync_resolve(parents, query, graph_parent, &proxy_block) ⇒ Object
Called by a scope that already decided to stay inline, so it must not consult the pool again.
A scope_proc builds a Scope rather than a proxy, and that nested scope decides for itself.
363
364
365
366
367
368
369
370
371
372
373
|
# File 'lib/graphiti/sideload.rb', line 363
def sync_resolve(parents, query, graph_parent, &proxy_block)
assert_singular!(parents)
if self.class.scope_proc
build_sideload_scope(parents, query, graph_parent).resolve do |sideload_results|
fire_assign(parents, sideload_results)
end
else
sync_load(parents, query, graph_parent, &proxy_block)
end
end
|
246
247
248
|
# File 'lib/graphiti/sideload.rb', line 246
def type
@type || raise("Override #type in subclass. Should be one of #{TYPES.inspect}")
end
|
#writable? ⇒ Boolean
110
111
112
|
# File 'lib/graphiti/sideload.rb', line 110
def writable?
evaluate_flag @writable
end
|