Class: RailsWorkflow::ProcessImporter

Inherits:
Object
  • Object
show all
Defined in:
app/services/rails_workflow/process_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(json) ⇒ ProcessImporter

Returns a new instance of ProcessImporter.



3
4
5
# File 'app/services/rails_workflow/process_importer.rb', line 3

def initialize json
  @json = json['process_template']
end

Instance Method Details

#processObject



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
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/rails_workflow/process_importer.rb', line 7

def process
  process = ProcessTemplate.
      where(uuid: @json['uuid']).first_or_create!

  process.attributes = @json.except('operations')
  process.save

  operations = []
  ids_to_delete = process.operations.pluck(:id)

  @json['operations'].each do |operation_json|

    operation = process.
        operations.where(uuid: operation_json['uuid']).first_or_create!

    operation.attributes = operation_json
    operation.save

    ids_to_delete.delete(operation.id)
    operations << operation
  end


  OperationTemplate.delete(ids_to_delete) if ids_to_delete.present?


  operations.each do |operation|
    operation.dependencies.each do |d|
      d['id'] = OperationTemplate.
          find_by_uuid(operation['uuid']).try(:id)

      d.delete("uuid")
    end

    operation.save
  end


end