Module: Graphoid::Mutations::Processor

Defined in:
lib/graphoid/mutations/processor.rb

Class Method Summary collapse

Class Method Details

.execute(model, grapho, data, user) ⇒ Object



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
# File 'lib/graphoid/mutations/processor.rb', line 6

def self.execute(model, grapho, data, user)
  root_object = []
  operations = []
  data.each { |key, value| operations << Operation.new(model, key, value) }

  operations.each do |operation|
    item = operation.operand.precreate(operation.value)
    root_object << item if item.present?
  end

  root_object = root_object.reduce({}, :merge)

  fieldnames = Attribute.fieldnames_of(model)
  root_object['created_by_id'] = user.id if fieldnames.include?('created_by_id')
  root_object['updated_by_id'] = user.id if fieldnames.include?('updated_by_id')

  sanitized = Attribute.correct(model, root_object)
  root = model.create!(sanitized)

  operations.each do |operation|
    operation.operand.create(root, operation.value, grapho)
  end

  root.reload
end