Class: Graphiti::Sideload

Inherits:
Object show all
Defined in:
lib/graphiti/sideload.rb

Direct Known Subclasses

BelongsTo, HasMany

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

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
# File 'lib/graphiti/sideload.rb', line 20

def initialize(name, opts)
  @name = name
  validate_options!(opts)
  @parent_resource_class = opts[:parent_resource]
  @resource_class = 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]
  @single = opts[:single]
  @remote = opts[:remote]
  apply_belongs_to_many_filter if type == :many_to_many

  @description = opts[:description]

  # polymorphic has_many
  @polymorphic_as = opts[:polymorphic_as]
  # polymorphic_belongs_to-specific
  @group_name = opts[:group_name]
  @polymorphic_child = opts[:polymorphic_child]
  @parent = opts[:parent]
  @always_include_resource_ids = opts[:always_include_resource_ids]

  if polymorphic_child?
    parent.resource.polymorphic << resource_class
  end

  if remote?
    @resource_class = create_remote_resource
  end
end

Instance Attribute Details

#group_nameObject (readonly)

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

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/graphiti/sideload.rb', line 6

def parent
  @parent
end

#parent_resource_classObject (readonly)

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_asObject (readonly)

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



310
311
312
313
314
315
316
317
# File 'lib/graphiti/sideload.rb', line 310

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



60
61
62
# File 'lib/graphiti/sideload.rb', line 60

def self.assign(&blk)
  self.assign_proc = blk
end

.assign_each(&blk) ⇒ Object



64
65
66
# File 'lib/graphiti/sideload.rb', line 64

def self.assign_each(&blk)
  self.assign_each_proc = blk
end

.hooksObject



319
320
321
322
323
324
325
326
# File 'lib/graphiti/sideload.rb', line 319

def self.hooks
  @hooks ||= {}.tap do |h|
    HOOK_ACTIONS.each do |a|
      h[:"after_#{a}"] = []
      h[:"before_#{a}"] = []
    end
  end
end


76
77
78
# File 'lib/graphiti/sideload.rb', line 76

def self.link(&blk)
  self.link_proc = blk
end

.params(&blk) ⇒ Object



68
69
70
# File 'lib/graphiti/sideload.rb', line 68

def self.params(&blk)
  self.params_proc = blk
end

.pre_load(&blk) ⇒ Object



72
73
74
# File 'lib/graphiti/sideload.rb', line 72

def self.pre_load(&blk)
  self.pre_load_proc = blk
end

.scope(&blk) ⇒ Object



56
57
58
# File 'lib/graphiti/sideload.rb', line 56

def self.scope(&blk)
  self.scope_proc = blk
end

Instance Method Details

#always_include_resource_ids?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/graphiti/sideload.rb', line 121

def always_include_resource_ids?
  !!@always_include_resource_ids
end

#assign(parents, children) ⇒ 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 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



187
188
189
# File 'lib/graphiti/sideload.rb', line 187

def assign_each(parent, children)
  raise "Override #assign_each in subclass"
end

#associate(parent, child) ⇒ Object



341
342
343
# File 'lib/graphiti/sideload.rb', line 341

def associate(parent, child)
  parent_resource.associate(parent, child, association_name, type)
end

#associate_all(parent, children) ⇒ Object



337
338
339
# File 'lib/graphiti/sideload.rb', line 337

def associate_all(parent, children)
  parent_resource.associate_all(parent, children, association_name, type)
end

#association_nameObject



175
176
177
# File 'lib/graphiti/sideload.rb', line 175

def association_name
  @as || name
end

#base_scopeObject



204
205
206
207
208
209
210
# File 'lib/graphiti/sideload.rb', line 204

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



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/graphiti/sideload.rb', line 212

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_resourcesObject



258
259
260
261
# File 'lib/graphiti/sideload.rb', line 258

def clear_resources
  @resource = nil
  @parent_resource = nil
end

#create_remote_resourceObject



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/graphiti/sideload.rb', line 80

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_endpoints = false
  }
  name = "#{parent_resource_class.name}.#{@name}.remote"
  klass.class_eval("def self.name;'#{name}';end", __FILE__, __LINE__)
  klass
end

#descriptionObject



199
200
201
202
# File 'lib/graphiti/sideload.rb', line 199

def description
  return @description if @description.present?
  parent_resource_class.resolve_i18n_field_description(name, field_type: :relationships)
end

#disassociate(parent, child) ⇒ Object



345
346
347
# File 'lib/graphiti/sideload.rb', line 345

def disassociate(parent, child)
  parent_resource.disassociate(parent, child, association_name, type)
end

#errorsObject



93
94
95
# File 'lib/graphiti/sideload.rb', line 93

def errors
  @errors ||= []
end

#fire_hooks!(parent, objects, method) ⇒ Object



328
329
330
331
332
333
334
335
# File 'lib/graphiti/sideload.rb', line 328

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_keyObject



171
172
173
# File 'lib/graphiti/sideload.rb', line 171

def foreign_key
  @foreign_key ||= infer_foreign_key
end

#ids_for_parents(parents) ⇒ Object



349
350
351
352
353
354
# File 'lib/graphiti/sideload.rb', line 349

def ids_for_parents(parents)
  parent_ids = parents.map(&primary_key)
  parent_ids.compact!
  parent_ids.uniq!
  parent_ids
end

#infer_foreign_keyObject

Override in subclass



242
243
244
245
246
247
# File 'lib/graphiti/sideload.rb', line 242

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

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
# File 'lib/graphiti/sideload.rb', line 125

def link?
  return true if link_proc

  if @link.nil?
    !!@parent_resource_class.autolink
  else
    !!@link
  end
end


139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/graphiti/sideload.rb', line 139

def link_extra_fields
  return unless context&.respond_to?(:params)

  extra_fields_name = [association_name, resource.type].find { |param|
    context.params.dig(:extra_fields, param)
  }

  if extra_fields_name
    extra_fields = context.params.dig(:extra_fields, extra_fields_name)
    {resource.type => extra_fields}
  end
end


135
136
137
# File 'lib/graphiti/sideload.rb', line 135

def link_filter(parents)
  base_filter(parents)
end

#load(parents, query, graph_parent) ⇒ Object



237
238
239
# File 'lib/graphiti/sideload.rb', line 237

def load(parents, query, graph_parent)
  build_resource_proxy(parents, query, graph_parent).to_a
end

#load_params(parents, query) ⇒ Object



195
196
197
# File 'lib/graphiti/sideload.rb', line 195

def load_params(parents, query)
  raise "Override #load_params in subclass"
end

#parent_resourceObject



253
254
255
# File 'lib/graphiti/sideload.rb', line 253

def parent_resource
  @parent_resource ||= parent_resource_class.new
end

#performant_assign?Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/graphiti/sideload.rb', line 356

def performant_assign?
  !self.class.assign_each_proc
end

#polymorphic_child?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/graphiti/sideload.rb', line 163

def polymorphic_child?
  !!@polymorphic_child
end

#polymorphic_has_many?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/graphiti/sideload.rb', line 117

def polymorphic_has_many?
  !!@polymorphic_as
end

#polymorphic_has_one?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/graphiti/sideload.rb', line 113

def polymorphic_has_one?
  !!@polymorphic_as
end

#polymorphic_parent?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/graphiti/sideload.rb', line 159

def polymorphic_parent?
  resource.polymorphic?
end

#primary_keyObject



167
168
169
# File 'lib/graphiti/sideload.rb', line 167

def primary_key
  @primary_key ||= :id
end

#readable?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/graphiti/sideload.rb', line 101

def readable?
  !!@readable
end

#remote?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/graphiti/sideload.rb', line 97

def remote?
  !!@remote
end

#resolve(parents, query, graph_parent) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/graphiti/sideload.rb', line 288

def resolve(parents, query, graph_parent)
  if single? && parents.length > 1
    raise Errors::SingularSideload.new(self, parents.length)
  end

  if self.class.scope_proc
    sideload_scope = fire_scope(parents)
    sideload_scope = Scope.new sideload_scope,
      resource,
      query,
      parent: graph_parent,
      sideload: self,
      sideload_parent_length: parents.length,
      default_paginate: false
    sideload_scope.resolve do |sideload_results|
      fire_assign(parents, sideload_results)
    end
  else
    load(parents, query, graph_parent)
  end
end

#resourceObject



249
250
251
# File 'lib/graphiti/sideload.rb', line 249

def resource
  @resource ||= resource_class.new
end

#resource_classObject



179
180
181
# File 'lib/graphiti/sideload.rb', line 179

def resource_class
  @resource_class ||= 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.

Returns:

  • (Boolean)


361
362
363
364
365
366
# File 'lib/graphiti/sideload.rb', line 361

def resource_class_loaded?
  resource_class
  true
rescue Graphiti::Errors::ResourceNotFound
  false
end

#scope(parents) ⇒ Object



183
184
185
# File 'lib/graphiti/sideload.rb', line 183

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

Returns:

  • (Boolean)


154
155
156
157
# File 'lib/graphiti/sideload.rb', line 154

def shared_remote?
  resource.remote? &&
    resource.remote_base_url = parent_resource_class.remote_base_url
end

#single?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/graphiti/sideload.rb', line 109

def single?
  !!@single
end

#typeObject



191
192
193
# File 'lib/graphiti/sideload.rb', line 191

def type
  @type || raise("Override #type in subclass. Should be one of #{TYPES.inspect}")
end

#writable?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/graphiti/sideload.rb', line 105

def writable?
  !!@writable
end