Sha256: c8da3bfd0ae21f81a2997d09f8ee964a3a686a4fb432ad6ba6c98a93be846754

Contents?: true

Size: 1.67 KB

Versions: 9

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module RailsWorkflow
  class ProcessImporter
    def initialize(json)
      prepared_json = RailsWorkflow.config.import_preprocessor.prepare(json)
      @json = prepared_json['process_template']
    end

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

      @json['child_processes'] && @json['child_processes'].each do |child_process|
        ProcessImporter.new('process_template' => child_process).process
      end

      process.attributes = @json.except('operations', 'child_processes')
      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.except('child_process')
        if operation_json['child_process'].present?
          child_template = ProcessTemplate.find_by_uuid(operation_json['child_process'])
          raise ActiveRecord::RecordNotFound, "Operation #{operation.title} child process template not found by UUID" if child_template.blank?
          operation.child_process = child_template
        end
        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(d['uuid']).try(:id)

          d.delete('uuid')
        end

        operation.save
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rails_workflow-0.7.3 app/services/rails_workflow/process_importer.rb
rails_workflow-0.7.2 app/services/rails_workflow/process_importer.rb
rails_workflow-0.7.1 app/services/rails_workflow/process_importer.rb
rails_workflow-0.7.0 app/services/rails_workflow/process_importer.rb
rails_workflow-0.4.4 app/services/rails_workflow/process_importer.rb
rails_workflow-0.4.3 app/services/rails_workflow/process_importer.rb
rails_workflow-0.4.2 app/services/rails_workflow/process_importer.rb
rails_workflow-0.4.1 app/services/rails_workflow/process_importer.rb
rails_workflow-0.4.0 app/services/rails_workflow/process_importer.rb