Class: QcReportsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/qc_reports_controller.rb

Constant Summary

Constants included from FlashTruncation

FlashTruncation::STRING_OVERHEAD

Instance Method Summary collapse

Methods inherited from ApplicationController

#block_api_access, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!

Methods included from FlashTruncation

#max_flash_size, #truncate_flash, #truncate_flash_array

Instance Method Details

#createObject

rubocop:enable Metrics/MethodLength



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/qc_reports_controller.rb', line 45

def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  study = Study.find_by(id: params[:qc_report][:study_id])
  exclude_existing = params[:qc_report][:exclude_existing] == '1'
  qc_report =
    QcReport.new(
      study: study,
      product_criteria: @product.stock_criteria,
      exclude_existing: exclude_existing,
      plate_purposes: params[:qc_report][:plate_purposes].try(:reject, &:blank?)
    )

  if qc_report.save
    flash[:notice] = 'Your report has been requested and will be presented on this page when complete.'
    redirect_to qc_report
  else
    # We failed to save
    error_messages = qc_report.errors.full_messages.join('; ')
    flash[:error] = "Failed to create report: #{error_messages}"
    redirect_back fallback_location: root_path
  end
end

#indexObject

rubocop:todo Metrics/AbcSize



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/qc_reports_controller.rb', line 6

def index # rubocop:todo Metrics/AbcSize
  # Build a conditions hash of acceptable parameters, ignoring those that are blank

  @qc_reports = QcReport.for_report_page(conditions).page(params[:page]).includes(:study, :product)
  @qc_report = QcReport.new(exclude_existing: true, study_id: params[:study_id])
  @studies = Study.alphabetical.pluck(:name, :id)
  @states = QcReport.available_states.map { |s| [s.humanize, s] }

  @all_products = Product.alphabetical.all.map { |product| [product.display_name, product.id] }
  @active_products =
    Product.with_stock_report.active.alphabetical.all.map { |product| [product.display_name, product.id] }
  @plate_purposes = PlatePurpose.pluck(:name).sort
end

#qc_fileObject

On form submit of a qc_file. Strictly speaking this should be an update action on the qc_report itself. However we don’t want to force the user to extract the report identifier from the file.



70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/qc_reports_controller.rb', line 70

def qc_file
  qc_file = params[:qc_report_file]
  overide_qc = params[:overide_qc_decision] == '1'
  file = QcReport::File.new(qc_file, overide_qc, qc_file.original_filename, qc_file.content_type)
  if file.process
    redirect_to file.qc_report
  else
    flash[:error] = "Failed to read report: #{file.errors.join('; ')}"
    redirect_back fallback_location: root_path
  end
end

#showObject

rubocop:todo Metrics/MethodLength



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/qc_reports_controller.rb', line 21

def show # rubocop:todo Metrics/AbcSize
  qc_report = QcReport.find_by!(report_identifier: params[:id])
  queue_count = qc_report.queued? ? Delayed::Job.count : 0
  @report_presenter = Presenters::QcReportPresenter.new(qc_report, queue_count)

  respond_to do |format|
    format.html

    format.csv do
      file = nil
      begin
        file = Tempfile.new(@report_presenter.filename)
        @report_presenter.to_csv(file)
        file.flush
      ensure
        file.close unless file.nil?
      end
      send_file file.path, content_type: 'text/csv', filename: @report_presenter.filename
    end if qc_report.available?
  end
end