Module: Tasks::SetDescriptorsHandler

Included in:
WorkflowsController
Defined in:
app/models/tasks/set_descriptors_handler.rb

Instance Method Summary collapse

Instance Method Details

#do_set_descriptors_task(_task, params) ⇒ Object


2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/models/tasks/set_descriptors_handler.rb', line 2

def do_set_descriptors_task(_task, params)
  @batch = Batch.includes(:requests, :pipeline, :lab_events).find(params[:batch_id])
  @rits = @batch.pipeline.request_information_types
  @requests = @batch.ordered_requests

  # if qc_state is qc_manual then update it
  if @batch.qc_state == 'qc_manual'
    @batch.lab_events.create(description: 'Manual QC', message: "Manual QC started for batch #{@batch.id}", user_id: current_user.id)
    @batch.lab_events.create(description: 'Manual QC', message: "Manual QC started for batch #{@batch.id}", user_id: current_user.id)
    @batch.qc_state = @batch.qc_next_state
    @batch.save
  end

  @workflow = Workflow.includes(:tasks).find(params[:workflow_id])
  @task = @workflow.tasks[params[:id].to_i]
  @stage = params[:id].to_i
  @count = 0
  if params[:values].nil?
    @values = {}
  else
    @values = params[:values]
  end

  # Perform the necessary updates if we've passed batch creation
  unless params[:next_stage].nil?
    updated = 0

    @batch.requests.each do |request|
      unless params[:request].nil?
        params[:request].keys.each do |checked|
          # This is used to see if any check boxes are set on when the page is rendered with
          # with a single set of descriptor fields is shared between all the requests...
          if request.id == checked.to_i

            event = LabEvent.new(batch_id: @batch.id, description: @task.name)

            # This is called when a single set of fields is used
            # and called over and over based on the select boxs
            unless params[:descriptors].nil?
              event.descriptors = params[:descriptors]
              event.descriptor_fields = ordered_fields(params[:fields])

              # Cache values to populate the next request on the same stage
              event.descriptors.each do |descriptor|
                @values[descriptor.name] = descriptor.value
              end
            end

            if !params[:requests].nil? && !params[:requests][(request.id).to_s].nil? && !params[:requests][(request.id).to_s][:descriptors].nil?
              # Descriptors: create description for event

              event.descriptors = params[:requests][(request.id).to_s][:descriptors]
              event.descriptor_fields = ordered_fields(params[:requests][(request.id).to_s][:fields])

            end

            if params[:upload].present?
              params[:upload].each do |key, uploaded|
                event.filename = uploaded.original_filename.gsub(/[^a-zA-Z0-9.]/, '_')
                event.data = uploaded.read
                event.add_descriptor Descriptor.new(name: key, value: event.filename)
              end
            end

            event.save
            current_user.lab_events << event
            request.lab_events << event

            unless request.asset.try(:resource)
              EventSender.send_request_update(request.id, 'update', "Passed: #{@task.name}")
            end
          end
        end
      end

      if request.has_passed(@batch, @task) || request.failed?
        updated += 1
      end
    end

    if updated == @batch.requests.count
      eventify_batch @batch, @task
      return true
    else
      # Some requests have yet to pass this task
      # Construct a URL that contains a nested hash of values to display as defaults for the next request
      @params = { batch_id: @batch.id, workflow_id: @workflow.id, values: @values }
      redirect_to url_for(flatten_hash(@params))
    end

  end
  false
end

#render_set_descriptors_task(_task, params) ⇒ Object


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/models/tasks/set_descriptors_handler.rb', line 96

def render_set_descriptors_task(_task, params)
  @batch = Batch.includes(:requests, :pipeline, :lab_events).find(params[:batch_id])
  @rits = @batch.pipeline.request_information_types
  @requests = @batch.ordered_requests
  @workflow = Workflow.includes(:tasks).find(params[:workflow_id])
  @task = @workflow.tasks[params[:id].to_i]
  @stage = params[:id].to_i
  @count = 0
  if params[:values].nil?
    @values = {}
  else
    @values = params[:values]
  end
end