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
-
#group_name ⇒ Object
readonly
Returns the value of attribute group_name.
-
#link ⇒ Object
readonly
Returns the value of attribute link.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_resource_class ⇒ Object
readonly
Returns the value of attribute parent_resource_class.
-
#polymorphic_as ⇒ Object
readonly
Returns the value of attribute polymorphic_as.
Class Method Summary collapse
- .after_save(only: [], except: [], &blk) ⇒ Object
- .assign(&blk) ⇒ Object
- .assign_each(&blk) ⇒ Object
- .hooks ⇒ Object
- .link(&blk) ⇒ Object
- .params(&blk) ⇒ Object
- .pre_load(&blk) ⇒ Object
- .scope(&blk) ⇒ Object
Instance Method Summary collapse
- #always_include_resource_ids? ⇒ Boolean
- #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
- #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
Override in subclass.
-
#initialize(name, opts) ⇒ Sideload
constructor
A new instance of Sideload.
- #link? ⇒ Boolean
- #link_extra_fields ⇒ Object
- #link_filter(parents) ⇒ Object
- #load(parents, query, graph_parent) ⇒ Object
- #load_params(parents, query) ⇒ 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
- #remote? ⇒ Boolean
-
#resolve(parents, query, graph_parent) ⇒ Object
Synchronous counterpart to #future_resolve, used when concurrency is off.
- #resource ⇒ Object
- #resource_class ⇒ Object
- #resource_class_loaded? ⇒ Boolean private
- #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 |
# File 'lib/graphiti/sideload.rb', line 20 def initialize(name, opts) @name = name (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] @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_name ⇒ Object (readonly)
Returns the value of attribute group_name.
6 7 8 |
# File 'lib/graphiti/sideload.rb', line 6 def group_name @group_name end |
#link ⇒ Object (readonly)
Returns the value of attribute link.
6 7 8 |
# File 'lib/graphiti/sideload.rb', line 6 def link @link end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/graphiti/sideload.rb', line 6 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/graphiti/sideload.rb', line 6 def parent @parent end |
#parent_resource_class ⇒ Object (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_as ⇒ Object (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
344 345 346 347 348 349 350 351 |
# File 'lib/graphiti/sideload.rb', line 344 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 |
.hooks ⇒ Object
353 354 355 356 357 358 359 360 |
# File 'lib/graphiti/sideload.rb', line 353 def self.hooks @hooks ||= {}.tap do |h| HOOK_ACTIONS.each do |a| h[:"after_#{a}"] = [] h[:"before_#{a}"] = [] end end end |
.link(&blk) ⇒ Object
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
125 126 127 |
# File 'lib/graphiti/sideload.rb', line 125 def always_include_resource_ids? !!@always_include_resource_ids end |
#assign(parents, children) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/graphiti/sideload.rb', line 270 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
192 193 194 |
# File 'lib/graphiti/sideload.rb', line 192 def assign_each(parent, children) raise "Override #assign_each in subclass" end |
#associate(parent, child) ⇒ Object
375 376 377 |
# File 'lib/graphiti/sideload.rb', line 375 def associate(parent, child) parent_resource.associate(parent, child, association_name, type) end |
#associate_all(parent, children) ⇒ Object
371 372 373 |
# File 'lib/graphiti/sideload.rb', line 371 def associate_all(parent, children) parent_resource.associate_all(parent, children, association_name, type) end |
#association_name ⇒ Object
179 180 181 |
# File 'lib/graphiti/sideload.rb', line 179 def association_name @as || name end |
#base_scope ⇒ Object
209 210 211 212 213 214 215 |
# File 'lib/graphiti/sideload.rb', line 209 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
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/graphiti/sideload.rb', line 217 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 = (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
265 266 267 268 |
# File 'lib/graphiti/sideload.rb', line 265 def clear_resources @resource = nil @parent_resource = nil end |
#create_remote_resource ⇒ Object
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 |
#description ⇒ Object
204 205 206 207 |
# File 'lib/graphiti/sideload.rb', line 204 def description return @description if @description.present? parent_resource_class.resolve_i18n_field_description(name, field_type: :relationships) end |
#disassociate(parent, child) ⇒ Object
379 380 381 |
# File 'lib/graphiti/sideload.rb', line 379 def disassociate(parent, child) parent_resource.disassociate(parent, child, association_name, type) end |
#errors ⇒ Object
93 94 95 |
# File 'lib/graphiti/sideload.rb', line 93 def errors @errors ||= [] end |
#fire_hooks!(parent, objects, method) ⇒ Object
362 363 364 365 366 367 368 369 |
# File 'lib/graphiti/sideload.rb', line 362 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
175 176 177 |
# File 'lib/graphiti/sideload.rb', line 175 def foreign_key @foreign_key ||= infer_foreign_key end |
#future_resolve(parents, query, graph_parent) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 |
# File 'lib/graphiti/sideload.rb', line 322 def future_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.future_resolve do |sideload_results| fire_assign(parents, sideload_results) end else future_load(parents, query, graph_parent) end end |
#guarded? ⇒ Boolean
109 110 111 |
# File 'lib/graphiti/sideload.rb', line 109 def guarded? dynamic_flag?(@readable) || dynamic_flag?(@writable) end |
#ids_for_parents(parents) ⇒ Object
383 384 385 386 387 388 |
# File 'lib/graphiti/sideload.rb', line 383 def ids_for_parents(parents) parent_ids = parents.map(&primary_key) parent_ids.compact! parent_ids.uniq! parent_ids end |
#infer_foreign_key ⇒ Object
Override in subclass
249 250 251 252 253 254 |
# File 'lib/graphiti/sideload.rb', line 249 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
129 130 131 132 133 134 135 136 137 |
# File 'lib/graphiti/sideload.rb', line 129 def link? return true if link_proc if @link.nil? !!@parent_resource_class.autolink else !!@link end end |
#link_extra_fields ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/graphiti/sideload.rb', line 143 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 |
#link_filter(parents) ⇒ Object
139 140 141 |
# File 'lib/graphiti/sideload.rb', line 139 def link_filter(parents) base_filter(parents) end |
#load(parents, query, graph_parent) ⇒ Object
242 243 244 245 246 |
# File 'lib/graphiti/sideload.rb', line 242 def load(parents, query, graph_parent) return build_resource_proxy(parents, query, graph_parent).to_a unless Graphiti.config.concurrency future_load(parents, query, graph_parent).value! end |
#load_params(parents, query) ⇒ Object
200 201 202 |
# File 'lib/graphiti/sideload.rb', line 200 def load_params(parents, query) raise "Override #load_params in subclass" end |
#parent_resource ⇒ Object
260 261 262 |
# File 'lib/graphiti/sideload.rb', line 260 def parent_resource @parent_resource ||= parent_resource_class.new end |
#performant_assign? ⇒ Boolean
390 391 392 |
# File 'lib/graphiti/sideload.rb', line 390 def performant_assign? !self.class.assign_each_proc end |
#polymorphic_child? ⇒ Boolean
167 168 169 |
# File 'lib/graphiti/sideload.rb', line 167 def polymorphic_child? !!@polymorphic_child end |
#polymorphic_has_many? ⇒ Boolean
121 122 123 |
# File 'lib/graphiti/sideload.rb', line 121 def polymorphic_has_many? !!@polymorphic_as end |
#polymorphic_has_one? ⇒ Boolean
117 118 119 |
# File 'lib/graphiti/sideload.rb', line 117 def polymorphic_has_one? !!@polymorphic_as end |
#polymorphic_parent? ⇒ Boolean
163 164 165 |
# File 'lib/graphiti/sideload.rb', line 163 def polymorphic_parent? resource.polymorphic? end |
#primary_key ⇒ Object
171 172 173 |
# File 'lib/graphiti/sideload.rb', line 171 def primary_key @primary_key ||= :id end |
#readable? ⇒ Boolean
101 102 103 |
# File 'lib/graphiti/sideload.rb', line 101 def readable? evaluate_flag @readable end |
#remote? ⇒ Boolean
97 98 99 |
# File 'lib/graphiti/sideload.rb', line 97 def remote? !!@remote end |
#resolve(parents, query, graph_parent) ⇒ Object
Synchronous counterpart to #future_resolve, used when concurrency is off. Mirrors #future_resolve but resolves inline via the synchronous Scope#resolve / #load paths (no promises). See Scope#sync_resolve_sideloads.
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/graphiti/sideload.rb', line 298 def resolve(parents, query, graph_parent) return future_resolve(parents, query, graph_parent).value! if Graphiti.config.concurrency 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 |
#resource ⇒ Object
256 257 258 |
# File 'lib/graphiti/sideload.rb', line 256 def resource @resource ||= resource_class.new end |
#resource_class ⇒ Object
183 184 185 186 |
# File 'lib/graphiti/sideload.rb', line 183 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.
395 396 397 398 399 400 |
# File 'lib/graphiti/sideload.rb', line 395 def resource_class_loaded? resource_class true rescue Graphiti::Errors::ResourceNotFound false end |
#scope(parents) ⇒ Object
188 189 190 |
# File 'lib/graphiti/sideload.rb', line 188 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
158 159 160 161 |
# File 'lib/graphiti/sideload.rb', line 158 def shared_remote? resource.remote? && resource.remote_base_url = parent_resource_class.remote_base_url end |
#single? ⇒ Boolean
113 114 115 |
# File 'lib/graphiti/sideload.rb', line 113 def single? !!@single end |
#type ⇒ Object
196 197 198 |
# File 'lib/graphiti/sideload.rb', line 196 def type @type || raise("Override #type in subclass. Should be one of #{TYPES.inspect}") end |
#writable? ⇒ Boolean
105 106 107 |
# File 'lib/graphiti/sideload.rb', line 105 def writable? evaluate_flag @writable end |