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) ⇒ Object
-
#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) ⇒ 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) ⇒ Object
-
#resource ⇒ Object
-
#resource_class ⇒ Object
-
#resource_class_loaded? ⇒ Boolean
private
-
#resource_ids_blocker ⇒ Object
-
#resource_ids_from_foreign_key? ⇒ Boolean
-
#scope(parents) ⇒ Object
-
#shared_remote? ⇒ Boolean
The parent resource is a remote, AND the sideload is a remote to the same endpoint.
-
#single? ⇒ Boolean
-
#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
363
364
365
366
367
368
369
370
|
# File 'lib/graphiti/sideload.rb', line 363
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
|
372
373
374
375
376
377
378
379
|
# File 'lib/graphiti/sideload.rb', line 372
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
# File 'lib/graphiti/sideload.rb', line 318
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
238
239
240
|
# File 'lib/graphiti/sideload.rb', line 238
def assign_each(parent, children)
raise "Override #assign_each in subclass"
end
|
#associate(parent, child) ⇒ Object
394
395
396
|
# File 'lib/graphiti/sideload.rb', line 394
def associate(parent, child)
parent_resource.associate(parent, child, association_name, type)
end
|
#associate_all(parent, children) ⇒ Object
390
391
392
|
# File 'lib/graphiti/sideload.rb', line 390
def associate_all(parent, children)
parent_resource.associate_all(parent, children, association_name, type)
end
|
#association_name ⇒ Object
225
226
227
|
# File 'lib/graphiti/sideload.rb', line 225
def association_name
@as || name
end
|
#base_scope ⇒ Object
255
256
257
258
259
260
261
|
# File 'lib/graphiti/sideload.rb', line 255
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
# File 'lib/graphiti/sideload.rb', line 263
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
313
314
315
316
|
# File 'lib/graphiti/sideload.rb', line 313
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
169
170
171
|
# File 'lib/graphiti/sideload.rb', line 169
def default_render_resource_ids?
false
end
|
#description ⇒ Object
250
251
252
253
|
# File 'lib/graphiti/sideload.rb', line 250
def description
return @description if @description.present?
parent_resource_class.resolve_i18n_field_description(name, field_type: :relationships)
end
|
#disassociate(parent, child) ⇒ Object
398
399
400
|
# File 'lib/graphiti/sideload.rb', line 398
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
381
382
383
384
385
386
387
388
|
# File 'lib/graphiti/sideload.rb', line 381
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
221
222
223
|
# File 'lib/graphiti/sideload.rb', line 221
def foreign_key
@foreign_key ||= infer_foreign_key
end
|
#future_resolve(parents, query, graph_parent) ⇒ Object
351
352
353
354
355
356
357
358
359
360
361
|
# File 'lib/graphiti/sideload.rb', line 351
def future_resolve(parents, query, graph_parent)
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)
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
402
403
404
405
406
407
|
# File 'lib/graphiti/sideload.rb', line 402
def ids_for_parents(parents)
parent_ids = parents.map(&primary_key)
parent_ids.compact!
parent_ids.uniq!
parent_ids
end
|
#infer_foreign_key ⇒ Object
297
298
299
300
301
302
|
# File 'lib/graphiti/sideload.rb', line 297
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
181
182
183
|
# File 'lib/graphiti/sideload.rb', line 181
def link?
link_mode != false
end
|
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/graphiti/sideload.rb', line 189
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
185
186
187
|
# File 'lib/graphiti/sideload.rb', line 185
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.
174
175
176
177
178
179
|
# File 'lib/graphiti/sideload.rb', line 174
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) ⇒ Object
288
289
290
291
292
293
294
|
# File 'lib/graphiti/sideload.rb', line 288
def load(parents, query, graph_parent)
if Scope.resolve_synchronously?
build_resource_proxy(parents, query, graph_parent).to_a
else
future_load(parents, query, graph_parent).value!
end
end
|
#load_params(parents, query) ⇒ Object
246
247
248
|
# File 'lib/graphiti/sideload.rb', line 246
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
308
309
310
|
# File 'lib/graphiti/sideload.rb', line 308
def parent_resource
@parent_resource ||= parent_resource_class.new
end
|
409
410
411
|
# File 'lib/graphiti/sideload.rb', line 409
def performant_assign?
!self.class.assign_each_proc
end
|
#polymorphic_child? ⇒ Boolean
213
214
215
|
# File 'lib/graphiti/sideload.rb', line 213
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
209
210
211
|
# File 'lib/graphiti/sideload.rb', line 209
def polymorphic_parent?
resource.polymorphic?
end
|
#primary_key ⇒ Object
217
218
219
|
# File 'lib/graphiti/sideload.rb', line 217
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
163
164
165
166
167
|
# File 'lib/graphiti/sideload.rb', line 163
def render_resource_ids?
return !!@render_resource_ids unless @render_resource_ids.nil?
default_render_resource_ids?
end
|
#resolve(parents, query, graph_parent) ⇒ Object
343
344
345
346
347
348
349
|
# File 'lib/graphiti/sideload.rb', line 343
def resolve(parents, query, graph_parent)
if Scope.resolve_synchronously?
sync_resolve(parents, query, graph_parent)
else
future_resolve(parents, query, graph_parent).value!
end
end
|
304
305
306
|
# File 'lib/graphiti/sideload.rb', line 304
def resource
@resource ||= resource_class.new
end
|
#resource_class ⇒ Object
229
230
231
232
|
# File 'lib/graphiti/sideload.rb', line 229
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.
414
415
416
417
418
419
|
# File 'lib/graphiti/sideload.rb', line 414
def resource_class_loaded?
resource_class
true
rescue Graphiti::Errors::ResourceNotFound
false
end
|
#resource_ids_blocker ⇒ Object
159
160
161
|
# File 'lib/graphiti/sideload.rb', line 159
def resource_ids_blocker
:no_foreign_key_on_parent
end
|
#resource_ids_from_foreign_key? ⇒ Boolean
155
156
157
|
# File 'lib/graphiti/sideload.rb', line 155
def resource_ids_from_foreign_key?
resource_ids_blocker.nil?
end
|
#scope(parents) ⇒ Object
234
235
236
|
# File 'lib/graphiti/sideload.rb', line 234
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
204
205
206
207
|
# File 'lib/graphiti/sideload.rb', line 204
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
|
242
243
244
|
# File 'lib/graphiti/sideload.rb', line 242
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
|