Sha256: 7356b2a91362053849bb9eb61d0b19be68061d23c8877d12657d44ccc1ee176b

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

module RailsWorkflow
  class ProcessImporter
    def initialize json
      @json = 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

1 entries across 1 versions & 1 rubygems

Version Path
rails_workflow-0.3.6 app/services/rails_workflow/process_importer.rb