Class: BatchesController
  
  
  
Overview
  
  Constant Summary
  
  
  FlashTruncation::STRING_OVERHEAD
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  #block_api_access, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  #max_flash_size, #truncate_flash, #truncate_flash_array
  
  
    Instance Method Details
    
      
  
  
    #batch_parameters  ⇒ Object 
  
  
  
  
    
rubocop:enable Metrics/MethodLength
   
 
  
  
    
      
133
134
135 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 133
def batch_parameters
  @batch_parameters ||= params.require(:batch).permit(:assignee_id)
end 
     | 
  
 
    
      
  
  
    #completed  ⇒ Object 
  
  
  
  
    
      
173
174
175
176
177
178 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 173
def completed
      @pipeline = Pipeline.find(params[:pipeline_id] || params[:id])
  @batches = @pipeline.batches.completed.order(id: :desc).includes(%i[user pipeline]).page(params[:page])
end
     | 
  
 
    
      
  
  
    #create  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/MethodLength
   
 
  
  
    
      
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 83
def create   @pipeline = Pipeline.find(params[:id])
  requests = @pipeline.(request_parameters)
    case params[:action_on_requests]
  when 'cancel_requests'
    transition_requests(requests, :cancel_before_started!, 'Requests cancelled')
  when 'hide_from_inbox'
    transition_requests(requests, :hold!, 'Requests hidden from inbox')
  else
        standard_create(requests)
  end
rescue ActiveRecord::RecordInvalid => e
  respond_to do |format|
    format.html do
      flash[:error] = e.record.errors.full_messages
      redirect_to(pipeline_path(@pipeline))
    end
    format.xml { render xml: @batch.errors.to_xml }
  end
end
     | 
  
 
    
      
  
  
    #download_spreadsheet  ⇒ Object 
  
  
  
  
    
Used in Cherrypicking pipeline to generate the template for CSV driven picks
   
 
  
  
    
      
329
330
331
332 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 329
def download_spreadsheet
  csv_string = Tasks::PlateTemplateHandler.generate_spreadsheet(@batch)
  send_data csv_string, type: 'text/plain', filename: "#{@batch.id}_cherrypick_layout.csv", disposition: 'attachment'
end
     | 
  
 
    
      
  
  
    #edit  ⇒ Object 
  
  
  
  
    
rubocop:enable Metrics/MethodLength
   
 
  
  
    
      
75
76
77
78
79
80 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 75
def edit
  @rits = @batch.pipeline.request_information_types
  @requests = @batch.ordered_requests.includes(:batch_request, :asset, :target_asset, :comments)
  @users = User.all
  @controls = @batch.pipeline.controls
end 
     | 
  
 
    
      
  
  
    #fail  ⇒ Object 
  
  
  
  
    
      
187
188
189 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 187
def fail
  @fail_reasons = @batch.workflow.source_is_internal? ? FAILURE_REASONS['internal'] : FAILURE_REASONS['external']
end 
     | 
  
 
    
      
  
  
    #fail_items  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/AbcSize, Metrics/MethodLength
   
 
  
  
    
      
191
192
193
194
195
196
197
198
199
200
201
202
203 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 191
def fail_items   ActiveRecord::Base.transaction do
    fail_params =
      params.permit(:id, requested_fail: {}, requested_remove: {}, failure: %i[reason comment fail_but_charge])
    fail_and_remover = Batch::RequestFailAndRemover.new(fail_params)
    if fail_and_remover.save
      flash[:notice] = truncate_flash(fail_and_remover.notice)
    else
      flash[:error] = truncate_flash(fail_and_remover.errors.full_messages.join(';'))
    end
    redirect_to action: :fail, id: params[:id]
  end
end
     | 
  
 
    
      
  
  
    #failed  ⇒ Object 
  
  
  
  
    
      
180
181
182
183
184
185 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 180
def failed
      @pipeline = Pipeline.find(params[:pipeline_id] || params[:id])
  @batches = @pipeline.batches.failed.order(id: :desc).includes(%i[user pipeline]).page(params[:page])
end
     | 
  
 
    
      
  
  
    #filtered  ⇒ Object 
  
  
  
  
    
      
305
306 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 305
def filtered
end 
     | 
  
 
    
      
  
  
    #find_batch_by_barcode  ⇒ Object 
  
  
  
  
    
      
342
343
344
345
346
347
348
349
350
351
352 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 342
def find_batch_by_barcode
  batch_id = LabEvent.find_batch_id_by_barcode(params[:id])
  if batch_id.nil?
    @batch_error = 'Batch id not found.'
    render action: 'batch_error', format: :xml
    nil
  else
    @batch = Batch.find(batch_id)
    render action: 'show', format: :xml
  end
end
     | 
  
 
    
      
  
  
    #find_batch_by_batch_id  ⇒ Object 
  
  
  
  
    
      
338
339
340 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 338
def find_batch_by_batch_id
  @batch = Batch.find(params[:batch_id])
end 
     | 
  
 
    
      
  
  
    #find_batch_by_id  ⇒ Object 
  
  
  
  
    
      
334
335
336 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 334
def find_batch_by_id
  @batch = Batch.find(params[:id])
end 
     | 
  
 
    
      
  
  
    #generate_sample_sheet  ⇒ Object 
  
  
  
  
    
      
354
355
356
357
358
359
360 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 354
def generate_sample_sheet
  csv_string = AvitiSampleSheet::SampleSheetGenerator.generate(@batch)
  send_data csv_string.encode('UTF-8'),
            type: 'text/csv',
            filename: "batch_#{@batch.id}_run_manifest.csv",
            disposition: 'attachment'
end
     | 
  
 
    
      
  
  
    #index  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/AbcSize, Metrics/MethodLength
   
 
  
  
    
      
33
34
35
36
37
38
39
40
41
42
43
44
45
46 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 33
def index   if logged_in?
    @user = params.fetch(:user, current_user)
    @batches = Batch.for_user(@user).order(id: :desc).includes(:user, :assignee, :pipeline).page(params[:page])
  else
        @batches = Batch.order(id: :asc).page(params[:page]).limit(10)
  end
  respond_to do |format|
    format.html
    format.xml { render xml: @batches.to_xml }
    format.json { render json: @batches.to_json.gsub('null', '""') }
  end
end
     | 
  
 
    
      
  
  
    #pending  ⇒ Object 
  
  
  
  
    
      
147
148
149
150
151
152 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 147
def pending
      @pipeline = Pipeline.find(params[:pipeline_id] || params[:id])
  @batches = @pipeline.batches.pending.order(id: :desc).includes(%i[user pipeline]).page(params[:page])
end
     | 
  
 
    
      
  
  
    #pipeline  ⇒ Object 
  
  
  
  
    
      
137
138
139
140
141
142
143
144
145 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 137
def pipeline
    @batches =
    Batch
      .where(pipeline_id: params[:pipeline_id] || params[:id])
      .order(id: :desc)
      .includes(:user, :pipeline)
      .page(params[:page])
end
     | 
  
 
    
      
  
  
    #previous_qc_state  ⇒ Object 
  
  
  
  
    
      
298
299
300
301
302
303 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 298
def previous_qc_state
  @batch.qc_previous_state!(current_user)
  @batch.save
  flash[:notice] = "Batch #{@batch.id} reset to state #{@batch.qc_state}"
  redirect_to batch_url(@batch)
end
     | 
  
 
    
      
  
  
    #print  ⇒ Object 
  
  
  
  
    
Handles printing of the worksheet
   
 
  
  
    
      
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 254
def print   @task = Task.find_by(id: params[:task_id])
  @pipeline = @batch.pipeline
  @comments = @batch.
  template = @pipeline.batch_worksheet
  if template == 'cherrypick_worksheet'
    robot_id = params.fetch(:robot_id, @batch.robot_id)
    @robot = robot_id ? Robot.find(robot_id) : Robot.default_for_verification
    @plates = params[:barcode] ? Plate.with_barcode(params[:barcode]) : @batch.output_plates
  end
  if template
    render action: template, layout: false
  else
    redirect_back fallback_location: batch_path(@batch), alert: "No worksheet for #{@pipeline.name}"
  end
end
     | 
  
 
    
      
  
  
    #print_barcodes  ⇒ Object 
  
  
  
  
    
      
244
245
246
247
248
249
250
251 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 244
def print_barcodes
  if @batch.requests.empty?
    flash[:notice] = 'Your batch contains no requests.'
    redirect_to controller: 'batches', action: 'show', id: @batch.id
  else
    print_handler(LabelPrinter::Label::BatchTube)
  end
end
     | 
  
 
    
      
  
  
    #print_labels  ⇒ Object 
  
  
  
  
    
      
217
218 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 217
def print_labels
end 
     | 
  
 
    
      
  
  
    #print_plate_barcodes  ⇒ Object 
  
  
  
 
    
      
  
  
    #print_plate_labels  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/MethodLength
   
 
  
  
    
      
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 220
def print_plate_labels   @pipeline = @batch.pipeline
  @output_barcodes = []
  @output_labware = @batch.plate_group_barcodes || []
  @output_labware.each_key do |parent|
    next if parent.nil?
    plate_barcode = parent.human_barcode
    @output_barcodes << plate_barcode if plate_barcode.present?
  end
  return if @output_barcodes.present?
    flash[:error] = 'Output plates do not have barcodes to print'
  redirect_to controller: 'batches', action: 'show', id: @batch.id
end
     | 
  
 
    
      
  
  
    #released  ⇒ Object 
  
  
  
  
    
      
161
162
163
164
165
166
167
168
169
170
171 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 161
def released
      @pipeline = Pipeline.find(params[:pipeline_id] || params[:id])
  @batches = @pipeline.batches.released.order(id: :desc).includes(%i[user pipeline]).page(params[:page])
  respond_to do |format|
    format.html
    format.xml { render layout: false }
  end
end
     | 
  
 
    
      
  
  
    #reset_batch  ⇒ Object 
  
  
  
  
    
      
291
292
293
294
295
296 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 291
def reset_batch
  pipeline = @batch.pipeline
  @batch.reset!(current_user)
  flash[:notice] = "Batch #{@batch.id} has been reset"
  redirect_to controller: 'pipelines', action: :show, id: pipeline.id
end
     | 
  
 
    
      
  
  
    #save  ⇒ Object 
  
  
  
  
    
      
213
214
215 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 213
def save
  redirect_to action: :show, id: @batch.id
end 
     | 
  
 
    
      
  
  
    #show  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/MethodLength
   
 
  
  
    
      
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 49
def show   respond_to do |format|
    format.html do
      @submenu_presenter = Presenters::BatchSubmenuPresenter.new(current_user, @batch)
      @pipeline = @batch.pipeline
      @tasks = @batch.tasks.sort_by(&:sorted)
      @rits = @pipeline.request_information_types
      @input_labware = @batch.input_labware_report
      @output_labware = @batch.output_labware_report
      if @pipeline.pick_data
        @robot = @batch.robot_id ? Robot.find(@batch.robot_id) : Robot.with_verification_behaviour.first
                        @robot ||= Robot.first
        @robots = Robot.with_verification_behaviour
      end
    end
    format.xml { render layout: false }
  end
end
     | 
  
 
    
      
  
  
    #sort  ⇒ Object 
  
  
  
  
    
      
205
206
207
208
209
210
211 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 205
def sort
  @batch.assign_positions_to_requests!(params['requests_list'].map(&:to_i))
    @batch.touch   head :ok
end
     | 
  
 
    
      
  
  
    #started  ⇒ Object 
  
  
  
  
    
      
154
155
156
157
158
159 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 154
def started
      @pipeline = Pipeline.find(params[:pipeline_id] || params[:id])
  @batches = @pipeline.batches.started.order(id: :desc).includes(%i[user pipeline]).page(params[:page])
end
     | 
  
 
    
      
  
  
    #swap  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/AbcSize
   
 
  
  
    
      
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 308
def swap   if @batch.swap(
    current_user,
    'batch_1' => {
      'id' => params['batch']['1'],
      'lane' => params['batch']['position']['1']
    },
    'batch_2' => {
      'id' => params['batch']['2'],
      'lane' => params['batch']['position']['2']
    }
  )
    flash[:notice] = 'Successfully swapped lane positions'
    redirect_to batch_path(@batch)
  else
    flash[:error] = @batch.errors.full_messages.join('<br />')
    redirect_to action: :filtered, id: @batch.id
  end
end
     | 
  
 
    
      
  
  
    #update  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/MethodLength
   
 
  
  
    
      
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 111
def update   if batch_parameters[:assignee_id]
    user = User.find(batch_parameters[:assignee_id])
    assigned_message = "Assigned to #{user.name} (#{user.login})."
  else
    assigned_message = ''
  end
  respond_to do |format|
    if @batch.update(batch_parameters)
      flash[:notice] = "Batch was successfully updated. #{assigned_message}"
      format.html { redirect_to batch_url(@batch) }
      format.xml { head :ok }
    else
      format.html { render action: 'edit' }
      format.xml { render xml: @batch.errors.to_xml }
    end
  end
end
     | 
  
 
    
      
  
  
    #verify  ⇒ Object 
  
  
  
  
    
      
273
274
275
276
277 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 273
def verify
  @requests = @batch.ordered_requests
  @pipeline = @batch.pipeline
  @count = @requests.length
end 
     | 
  
 
    
      
  
  
    #verify_tube_layout  ⇒ Object 
  
  
  
  
    
rubocop:todo Metrics/AbcSize
   
 
  
  
    
      
279
280
281
282
283
284
285
286
287
288
289 
     | 
    
      # File 'app/controllers/batches_controller.rb', line 279
def verify_tube_layout   tube_barcodes = Array.new(@batch.requests.count) { |i| params["barcode_#{i}"] }
  if @batch.verify_tube_layout(tube_barcodes, current_user)
    flash[:notice] = 'All of the tubes are in their correct positions.'
    redirect_to batch_path(@batch)
  else
    flash[:error] = @batch.errors.full_messages.sort
    redirect_to action: :verify, id: @batch.id
  end
end
     |