Class: LocationReport
  
  
  
Overview
  
    
rubocop:todo Metrics/ClassLength
   
 
  
Defined Under Namespace
  
    
  
    
      Classes: LocationReportForm
    
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  extended, has_uploaded
  
  
  
  
  
  
  
  
  
  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_labware_found  ⇒ Object 
  
  
  
  
    | 
70
71
72
73
74 | # File 'app/models/location_report.rb', line 70
def check_any_labware_found
  return if labware_list.any?
  errors.add(:base, I18n.t('location_reports.errors.no_rows_found'))
end | 
 
    
      
  
  
    #check_any_select_field_present  ⇒ Object 
  
  
  
  
    | 
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_used  ⇒ Object 
  
  
  
  
    | 
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_date  ⇒ Object 
  
  
  
  
    | 
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_barcode  ⇒ Object 
  
  
  
  
    | 
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_barcodes  ⇒ Object 
  
  
  
  
    | 
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_headers  ⇒ Object 
  
  
  
  
    | 
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
   
 
  
    | 
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   if labware_list.empty?
    yield([I18n.t('location_reports.errors.labware_list_empty')])
    return
  end
  yield column_headers
  labware_list.each do |cur_labware|
    if cur_labware.studies.present?
      cur_labware.studies.each { |cur_study| yield(generate_report_row(cur_labware, cur_study)) }
    else
      yield(generate_report_row(cur_labware, nil))
    end
  end
end | 
 
    
      
  
  
    #schedule_report  ⇒ Object 
  
  
  
  
    | 
111
112
113 | # File 'app/models/location_report.rb', line 111
def schedule_report
  Delayed::Job.enqueue LocationReportJob.new(id)
end | 
 
    
      
  
  
    #type_labwhere?  ⇒ Boolean 
  
  
  
  
    | 
41
42
43 | # File 'app/models/location_report.rb', line 41
def type_labwhere?
  report_type == 'type_labwhere'
end | 
 
    
      
  
  
    #type_selection?  ⇒ Boolean 
  
  
  
  
    | 
37
38
39 | # File 'app/models/location_report.rb', line 37
def type_selection?
  report_type == 'type_selection'
end |