Class: GraphQLIncludable::Edge

Inherits:
GraphQL::Relay::Edge
  • Object
show all
Defined in:
lib/graphql_includable/edge.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/graphql_includable/edge.rb', line 34

def method_missing(method_name, *args, &block)
  if edge.respond_to?(method_name)
    edge.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#edgeObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/graphql_includable/edge.rb', line 3

def edge
  return @edge if @edge
  join_chain = joins_along_edge
  edge_class_name = join_chain.shift
  edge_class = str_to_class(edge_class_name)

  root_association_key = class_to_str(parent.class)
  unless edge_class.reflections.keys.include?(root_association_key)
    is_polymorphic = true
    root_association_key = edge_class.reflections.select { |k, r| r.polymorphic? }.keys.first
  end

  if parent.class.delegate_cache&.key?(edge_class_name)
    root_association_search_value = parent.send(parent.class.delegate_cache[edge_class_name])
  else
    root_association_search_value = parent
  end

  root_node = { root_association_key.to_sym => [root_association_search_value] }
  terminal_node = { class_to_str(node.class) => node }
  join_chain.reverse.each do |rel_name|
    terminal_node = { rel_name.to_s.pluralize => terminal_node }
  end

  search_hash = root_node.merge(terminal_node)
  edge_includes = join_chain.map { |s| s.to_s.singularize }
  edge_class = edge_class.includes(*edge_includes) unless edge_includes.empty?
  edge_class = edge_class.joins(root_association_key.to_sym) unless is_polymorphic
  @edge ||= edge_class.find_by(search_hash)
end

#respond_to_missing(method_name, include_private = false) ⇒ Object



42
43
44
# File 'lib/graphql_includable/edge.rb', line 42

def respond_to_missing(method_name, include_private = false)
  edge.respond_to?(method_name) || super
end