Class: Graphiti::Sideload::BelongsTo

Inherits:
Graphiti::Sideload show all
Defined in:
lib/graphiti/sideload/belongs_to.rb

Constant Summary

Constants inherited from Graphiti::Sideload

HOOK_ACTIONS, TYPES

Instance Attribute Summary

Attributes inherited from Graphiti::Sideload

#group_name, #link, #name, #parent, #parent_resource_class, #polymorphic_as

Instance Method Summary collapse

Methods inherited from Graphiti::Sideload

after_save, assign, #assign, assign_each, #assign_each, #associate, #associate_all, #association_name, #base_scope, #build_resource_proxy, #clear_resources, #create_remote_resource, #customized_base_scope?, #description, #disassociate, #errors, #fire_hooks!, #foreign_key, #future_resolve, #guarded?, hooks, #initialize, link, #link?, #link_extra_fields, #link_filter, #load, #non_default_options, params, #parent_resource, #performant_assign?, #polymorphic_child?, #polymorphic_has_many?, #polymorphic_has_one?, #polymorphic_parent?, pre_load, #primary_key, #readable?, #readable_guard_name, #readable_guarded?, #remote?, #render_resource_ids?, #resolve, #resource, #resource_class, #resource_class_loaded?, #resource_ids_from_foreign_key?, scope, #scope, #shared_remote?, #single?, #writable?

Constructor Details

This class inherits a constructor from Graphiti::Sideload

Instance Method Details

#base_filter(parents) ⇒ Object



44
45
46
47
# File 'lib/graphiti/sideload/belongs_to.rb', line 44

def base_filter(parents)
  parent_ids = ids_for_parents(parents)
  {primary_key => parent_ids.join(",")}
end

#default_render_resource_ids?Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
# File 'lib/graphiti/sideload/belongs_to.rb', line 6

def default_render_resource_ids?
  case parent_resource_class&.belongs_to_resource_ids_by_default
  when :always then renderable_at_all?
  when :never then false
  else resource_ids_from_foreign_key?
  end
end

base_filter matches the foreign key against primary_key, so a custom primary_key means the key holds that column's value, not the related id.

Returns:

  • (Boolean)


33
34
35
# File 'lib/graphiti/sideload/belongs_to.rb', line 33

def foreign_key_is_related_id?
  primary_key == :id
end

#ids_for_parents(parents) ⇒ Object



49
50
51
52
53
54
# File 'lib/graphiti/sideload/belongs_to.rb', line 49

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

#infer_foreign_keyObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/graphiti/sideload/belongs_to.rb', line 56

def infer_foreign_key
  return parent.foreign_key if polymorphic_child?

  if resource.remote?
    namespace = namespace_for(resource.class)
    resource_name = resource.class.name
      .gsub("#{namespace}::", "")
      .gsub("Resource", "")
    if resource_name.include?(".remote")
      resource_name = resource_name.split(".remote")[0].split(".")[1]
    end
    :"#{resource_name.singularize.underscore}_id"
  else
    model = resource.model
    namespace = namespace_for(model)
    model_name = model.name.gsub("#{namespace}::", "")
    :"#{model_name.underscore}_id"
  end
end

#load_params(parents, query) ⇒ Object



37
38
39
40
41
42
# File 'lib/graphiti/sideload/belongs_to.rb', line 37

def load_params(parents, query)
  query.hash.tap do |hash|
    hash[:filter] ||= {}
    hash[:filter].merge!(base_filter(parents))
  end
end

#renderable_at_all?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/graphiti/sideload/belongs_to.rb', line 14

def renderable_at_all?
  readable_guarded? || readable?
end

#resource_ids_blockerObject



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/graphiti/sideload/belongs_to.rb', line 18

def resource_ids_blocker
  return :unreadable unless renderable_at_all?
  return :custom_primary_key unless foreign_key_is_related_id?
  return :polymorphic_child if polymorphic_child?
  return :scope_block if self.class.scope_proc
  return :params_block if self.class.params_proc
  return :base_scope if @base_scope
  return :remote if remote?
  return :polymorphic_resource if resource.class.polymorphic.present?

  nil
end

#typeObject



2
3
4
# File 'lib/graphiti/sideload/belongs_to.rb', line 2

def type
  :belongs_to
end