Module: Graphiti::ActiveGraph::Deserializer

Defined in:
lib/graphiti/active_graph/deserializer.rb

Defined Under Namespace

Classes: Conflict

Instance Method Summary collapse

Instance Method Details

#add_path_id_to_relationships!(params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/graphiti/active_graph/deserializer.rb', line 69

def add_path_id_to_relationships!(params)
  return params if path_relationships_updated?
  detect_conflict(:id, @params[:id], attributes[:id])
  path_map.each do |rel_name, path_value|
    body_value = relationships.dig(rel_name, :attributes, :id)
    if body_value
      detect_conflict(rel_name, path_value.to_i, body_value&.to_i)
    else
      update_params(params, rel_name, path_value)
      update_realationships(rel_name, path_value)
    end
  end
  path_relationships_updated!
  params
end

#detect_conflict(key, path_value, body_value) ⇒ Object

Raises:



128
129
130
# File 'lib/graphiti/active_graph/deserializer.rb', line 128

def detect_conflict(key, path_value, body_value)
  raise Conflict.new(key, path_value, body_value) if body_value && body_value != path_value
end

#filter_keys(map) ⇒ Object



120
121
122
# File 'lib/graphiti/active_graph/deserializer.rb', line 120

def filter_keys(map)
  map.map { |key, v| [yield(key), v] }.select(&:first).to_h
end

#filter_keys_presence(map) ⇒ Object



116
117
118
# File 'lib/graphiti/active_graph/deserializer.rb', line 116

def filter_keys_presence(map)
  filter_keys(map) { |key| presence(key) || presence(key.to_s.pluralize.to_sym) }
end

#initialize(payload, env = nil, model = nil, parent_map = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/graphiti/active_graph/deserializer.rb', line 17

def initialize(payload, env=nil, model=nil, parent_map=nil)
  super(payload)

  if data.blank? && env && JsonApiNonParsableMiddleware.parsable_content?(env)
    raise ArgumentError, "JSON API payload must contain the 'data' key"
  end

  @params = payload
  @model = model
  @parent_map = parent_map || {}
  @env = env
end

#meta(action: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/graphiti/active_graph/deserializer.rb', line 55

def meta(action: nil)
  results = super
  return results if action.present? || @env.nil?

  action = case @env['REQUEST_METHOD']
           when 'POST' then :create
           when 'PUT', 'PATCH' then :update
           when 'DELETE' then :destroy
           end

  results[:method] = action
  results
end

#path_mapObject



108
109
110
111
112
113
114
# File 'lib/graphiti/active_graph/deserializer.rb', line 108

def path_map
  map = @params.select { |key, _| key =~ /_id$/ }.permit!.to_h
  map = filter_keys(map) { |key| key.gsub(/_id$/, '').to_sym }
  map = filter_keys(map) { |key| @parent_map[key] || key }
  map = filter_keys_presence(map) if @model < ActiveGraph::Node
  map
end

#path_relationships_updated!Object



85
86
87
# File 'lib/graphiti/active_graph/deserializer.rb', line 85

def path_relationships_updated!
  @path_relationships_updated = true
end

#path_relationships_updated?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/graphiti/active_graph/deserializer.rb', line 89

def path_relationships_updated?
  @path_relationships_updated.present?
end

#presence(key) ⇒ Object



124
125
126
# File 'lib/graphiti/active_graph/deserializer.rb', line 124

def presence(key)
  key if @model.associations.include?(key)
end

#process_nil_relationship(name) ⇒ Object

change empty relationship as disassociate hash so they will be removed



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/graphiti/active_graph/deserializer.rb', line 41

def process_nil_relationship(name)
  attributes = {}
  method_name = :disassociate

  {
    meta: {
      jsonapi_type: name.to_sym,
      method: method_name
    },
    attributes: attributes,
    relationships: {}
  }
end

#process_relationships(relationship_hash) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/graphiti/active_graph/deserializer.rb', line 30

def process_relationships(relationship_hash)
  {}.tap do |hash|
    relationship_hash.each_pair do |name, relationship_payload|
      name = name.to_sym
      data_payload = relationship_payload[:data]
      hash[name] = data_payload.nil? ? process_nil_relationship(name) : process_relationship(relationship_payload[:data])
    end
  end
end

#update_params(params, rel_name, path_value) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/graphiti/active_graph/deserializer.rb', line 93

def update_params(params, rel_name, path_value)
  params[:data] ||= {}
  params[:data][:relationships] ||= {}
  params[:data][:relationships][rel_name] = {
    data: {
      type: rel_name.to_s,
      id: path_value.to_i
    }
  }
end

#update_realationships(rel_name, path_value) ⇒ Object



104
105
106
# File 'lib/graphiti/active_graph/deserializer.rb', line 104

def update_realationships(rel_name, path_value)
  relationships[rel_name] = { meta: {}, attributes: { id: path_value.to_i } }
end