Class: LocationReport

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

Overview

rubocop:todo Metrics/ClassLength

Defined Under Namespace

Classes: LocationReportForm

Instance Method Summary collapse

Methods included from DbFile::Uploader

extended, has_uploaded

Methods inherited from ApplicationRecord

alias_association, convert_labware_to_receptacle_for, find_by_id_or_name, find_by_id_or_name!

Methods included from Squishify

extended

Instance Method Details

#check_any_plates_foundObject



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

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



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

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

#check_both_dates_present_if_usedObject



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

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



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

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



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

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



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

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



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

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

#generate!Object



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

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 { |fields| tempfile << CSV.generate_line(fields, **csv_options) }
      tempfile.rewind
      update!(report: tempfile)
    end
  end
end

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

rubocop:todo Metrics/MethodLength

Yields:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/models/location_report.rb', line 115

def generate_report_rows # rubocop:todo Metrics/MethodLength
  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 { |cur_study| yield(generate_report_row(cur_plate, cur_study)) }
    else
      yield(generate_report_row(cur_plate, nil))
    end
  end
end

#schedule_reportObject



111
112
113
# File 'app/models/location_report.rb', line 111

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

#type_labwhere?Boolean

Returns:

  • (Boolean)


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

def type_labwhere?
  report_type == 'type_labwhere'
end

#type_selection?Boolean

Returns:

  • (Boolean)


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

def type_selection?
  report_type == 'type_selection'
end