Class: QcReportsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

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

Instance Method Details

#createObject


18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/qc_reports_controller.rb', line 18

def create
  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


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

def index
  # 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.


36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/qc_reports_controller.rb', line 36

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


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

def show
  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