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, #always_include_resource_ids?, assign, #assign, assign_each, #assign_each, #associate, #associate_all, #association_name, #base_scope, #build_resource_proxy, #clear_resources, #create_remote_resource, #description, #disassociate, #errors, #fire_hooks!, #foreign_key, hooks, link, #link?, #link_extra_fields, #link_filter, #load, params, #parent_resource, #performant_assign?, #polymorphic_child?, #polymorphic_has_many?, #polymorphic_has_one?, #polymorphic_parent?, pre_load, #primary_key, #readable?, #remote?, #resolve, #resource, #resource_class, #resource_class_loaded?, #scope, scope, #shared_remote?, #single?, #writable?

Constructor Details

#initialize(name, opts) ⇒ BelongsTo

Returns a new instance of BelongsTo.



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

def initialize(name, opts)
  opts = {always_include_resource_ids: false}.merge(opts)
  super(name, opts)
end

Instance Method Details

#base_filter(parents) ⇒ Object



18
19
20
21
# File 'lib/graphiti/sideload/belongs_to.rb', line 18

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

#ids_for_parents(parents) ⇒ Object



23
24
25
26
27
28
# File 'lib/graphiti/sideload/belongs_to.rb', line 23

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

#infer_foreign_keyObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/graphiti/sideload/belongs_to.rb', line 30

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



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

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

#typeObject



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

def type
  :belongs_to
end