Class: LocationReport

Inherits:
ApplicationRecord show all
Extended by:
DbFile::Uploader
Defined in:
app/models/location_report.rb

Defined Under Namespace

Classes: LocationReportForm

Instance Method Summary collapse

Methods included from DbFile::Uploader

extended, has_uploaded

Methods inherited from ApplicationRecord

convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Warren::BroadcastMessages

#broadcast, included, #queue_associated_for_broadcast, #queue_for_broadcast, #warren

Instance Method Details

#check_any_plates_foundObject


64
65
66
67
68
# File 'app/models/location_report.rb', line 64

def check_any_plates_found
  return if plates_list.any?

  errors.add(:base, I18n.t('location_reports.errors.no_rows_found'))
end

#check_any_select_field_presentObject


47
48
49
50
# File 'app/models/location_report.rb', line 47

def check_any_select_field_present
  attr_list = %i[faculty_sponsor_ids study_id start_date end_date plate_purpose_ids barcodes]
  errors.add(:base, I18n.t('location_reports.errors.no_selection_fields_filled')) if attr_list.all? { |attr| send(attr).blank? }
end

#check_both_dates_present_if_usedObject


52
53
54
55
56
# File 'app/models/location_report.rb', line 52

def check_both_dates_present_if_used
  return if (start_date.blank? && end_date.blank?) || (start_date.present? && end_date.present?)

  errors.add(:start_date, I18n.t('location_reports.errors.both_dates_required'))
end

#check_end_date_same_or_after_start_dateObject


58
59
60
61
62
# File 'app/models/location_report.rb', line 58

def check_end_date_same_or_after_start_date
  return if (start_date.blank? || end_date.blank?) || end_date >= start_date

  errors.add(:end_date, I18n.t('location_reports.errors.end_date_after_start_date'))
end

#check_location_barcodeObject


41
42
43
44
45
# File 'app/models/location_report.rb', line 41

def check_location_barcode
  return if location_barcode.present?

  errors.add(:location_barcode, I18n.t('location_reports.errors.no_location_barcode_found'))
end

#check_maxlength_of_barcodesObject


70
71
72
73
74
# File 'app/models/location_report.rb', line 70

def check_maxlength_of_barcodes
  return if barcodes.blank? || (barcodes.to_yaml.size <= column_for_attribute(:barcodes).limit)

  errors.add(:barcodes_text, I18n.t('location_reports.errors.barcodes_maxlength_exceeded'))
end

#column_headersObject


76
77
78
# File 'app/models/location_report.rb', line 76

def column_headers
  %w[ScannedBarcode HumanBarcode Type Created Location Service StudyName StudyId FacultySponsor]
end

#generate!Object


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/location_report.rb', line 80

def generate!
  csv_options = { row_sep: "\r\n", force_quotes: true }
  filename    = ['locn_rpt', name].join('_') + '.csv'

  ActiveRecord::Base.transaction do
    Tempfile.open(filename) do |tempfile|
      generate_report_rows do |fields|
        tempfile << CSV.generate_line(fields, csv_options)
      end
      tempfile.rewind
      update!(report: tempfile)
    end
  end
end

#generate_report_rows {|column_headers| ... } ⇒ Object

Yields:


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/models/location_report.rb', line 99

def generate_report_rows
  if plates_list.empty?
    yield([I18n.t('location_reports.errors.plate_list_empty')])
    return
  end

  yield column_headers

  plates_list.each do |cur_plate|
    if cur_plate.studies.present?
      cur_plate.studies.each do |cur_study|
        yield(generate_report_row(cur_plate, cur_study))
      end
    else
      yield(generate_report_row(cur_plate, nil))
    end
  end
end

#schedule_reportObject


95
96
97
# File 'app/models/location_report.rb', line 95

def schedule_report
  Delayed::Job.enqueue LocationReportJob.new(id)
end

#type_labwhere?Boolean

Returns:

  • (Boolean)

37
38
39
# File 'app/models/location_report.rb', line 37

def type_labwhere?
  report_type == 'type_labwhere'
end

#type_selection?Boolean

Returns:

  • (Boolean)

33
34
35
# File 'app/models/location_report.rb', line 33

def type_selection?
  report_type == 'type_selection'
end