• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

uclibs / treatment_database / 6e23c015-e008-468d-9276-0cb88be104ae

01 Mar 2024 08:15PM UTC coverage: 77.381%. First build
6e23c015-e008-468d-9276-0cb88be104ae

Pull #433

circleci

crowesn
update rails 6.7.1.1, ruby 3.3.0, bundle update
Pull Request #433: [WIP] Upgrade Ruby to 3.3.0 and Rails to 6.7.1.1

5 of 6 new or added lines in 2 files covered. (83.33%)

585 of 756 relevant lines covered (77.38%)

10.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

21.99
/lib/tasks/batch.rake
1
# frozen_string_literal: true
2

3
require 'csv'
1✔
4

5
# create empty hashes in global vars to store input vocab and ids
6

7
@departments = {}
1✔
8
@repair_type = {}
1✔
9
@vendor = {}
1✔
10
@staff_code = {}
1✔
11
@housing = {}
1✔
12
@treatment_time = {}
1✔
13

14
# open csv files to load input vocab with ids
15

16
CSV.foreach('lib/assets/departments.csv', headers: true, return_headers: false) do |i|
1✔
17
  @departments[(i[0])] = i[1]
14✔
18
end
19

20
CSV.foreach('lib/assets/types_of_repairs.csv', headers: true, return_headers: false) do |i|
1✔
21
  @repair_type[(i[0])] = i[1]
51✔
22
end
23

24
CSV.foreach('lib/assets/contract_conservators.csv', headers: true, return_headers: false) do |i|
1✔
25
  @vendor[(i[0])] = i[1]
16✔
26
end
27

28
CSV.foreach('lib/assets/housing.csv', headers: true, return_headers: false) do |i|
1✔
29
  @housing[(i[0])] = i[1]
12✔
30
end
31

32
CSV.foreach('lib/assets/performed_treatment_time.csv', headers: false) do |i|
1✔
33
  @treatment_time[(i[0])] = i[1]
171✔
34
end
35

36
def open_input_csv
1✔
37
  return if ENV['CSV_LOCATION'].blank?
×
38

39
  CSV.read(ENV.fetch('CSV_LOCATION', nil), col_sep: ',', headers: true, row_sep: :auto)
×
40
end
41

42
# method to use input id for target id lookup
43

44
def vocab(input_type, input_key)
1✔
45
  ControlledVocabulary.find_by(vocabulary: input_type, key: input_key).id if input_key.present?
×
46
end
47

48
def user_lookup(name)
1✔
49
  class << self
×
50
    def assign_temp_user(name)
×
51
      puts "User account #{name} not found"
×
52
      User.find_by(display_name: 'Temporary User').id
×
53
    end
54
  end
55

56
  user = User.find_by(display_name: name)
×
57
  user.present? ? user.id : assign_temp_user(name) # assign temp user
×
58
end
59

60
def staff_code_lookup(code)
1✔
61
  if code.present?
×
NEW
62
    StaffCode.find_by(code:).id
×
63
  else
64
    '4'
×
65
  end
66
end
67

68
def create_in_house_repair_reports(row)
1✔
69
  if row['Type In-house Repair'].present?
×
70
    InHouseRepairRecord.create!(repair_type: vocab('repair_type', row['Type In-house Repair']),
×
71
                                performed_by_user_id: user_lookup(row['Preformed In-house Repair']),
72
                                minutes_spent: row['Time In-house Repair'],
73
                                conservation_record_id: row['Database ID'],
74
                                staff_code_id: staff_code_lookup(row['Staff Code']))
75
    puts '   - Attached in-house repair'
×
76
  end
77
  if row['Type In-house Repair 2'].present?
×
78
    InHouseRepairRecord.create!(repair_type: vocab('repair_type', row['Type In-house Repair 2']),
×
79
                                performed_by_user_id: user_lookup(row['Preformed In-house Repair 2']),
80
                                minutes_spent: row['Time In-house Repair 2'],
81
                                conservation_record_id: row['Database ID'],
82
                                staff_code_id: staff_code_lookup(row['Staff Code2']))
83
    puts '   - Attached in-house repair'
×
84
  end
85
  if row['Type In-house Repair 3'].present?
×
86
    InHouseRepairRecord.create!(repair_type: vocab('repair_type', row['Type In-house Repair 3']),
×
87
                                performed_by_user_id: user_lookup(row['Preformed In-house Repair 3']),
88
                                minutes_spent: row['Time In-house Repair 3'],
89
                                conservation_record_id: row['Database ID'],
90
                                staff_code_id: staff_code_lookup(row['Staff Code3']))
91
    puts '   - Attached in-house repair'
×
92
  end
93
  if row['Type In-house Repair other'].present? # rubocop:disable Style/GuardClause
×
94
    InHouseRepairRecord.create!(repair_type: vocab('repair_type', 'Other'),
×
95
                                performed_by_user_id: user_lookup(row['Preformed In-house Repair other']),
96
                                minutes_spent: row['Time In-house Repair other'],
97
                                conservation_record_id: row['Database ID'],
98
                                other_note: row['Type In-house Repair other'],
99
                                staff_code_id: staff_code_lookup(row['Staff Codes other']))
100
    puts '   - Attached in-house repair'
×
101
  end
102
end
103

104
def create_external_repair_reports(row)
1✔
105
  if row['Type Vendor Repair'].present?
×
106
    ExternalRepairRecord.create!(repair_type: vocab('repair_type', row['Type Vendor Repair']),
×
107
                                 performed_by_vendor_id: vocab('contract_conservator', row['Preformed by Vendor']),
108
                                 conservation_record_id: row['Database ID'])
109
    puts '   - Attached external repair'
×
110
  end
111
  if row['Type Vendor Repair 2'].present?
×
112
    ExternalRepairRecord.create!(repair_type: vocab('repair_type', row['Type Vendor Repair 2']),
×
113
                                 performed_by_vendor_id: vocab('contract_conservator', row['Preformed by Vendor 2']),
114
                                 conservation_record_id: row['Database ID'])
115
    puts '   - Attached external repair'
×
116
  end
117
  if row['Type Vendor Repair 3'].present?
×
118
    ExternalRepairRecord.create!(repair_type: vocab('repair_type', row['Type Vendor Repair 3']),
×
119
                                 performed_by_vendor_id: vocab('contract_conservator', row['Preformed by Vendor 3']),
120
                                 conservation_record_id: row['Database ID'])
121
    puts '   - Attached external repair'
×
122
  end
123
  if row['Type Vendor Repair other'].present? # rubocop:disable Style/GuardClause
×
124
    ExternalRepairRecord.create!(repair_type: vocab('repair_type', 'Other'),
×
125
                                 performed_by_vendor_id: vocab('contract_conservator', row['Preformed by Vendor other']),
126
                                 conservation_record_id: row['Database ID'],
127
                                 other_note: row['Type Vendor Repair other'])
128
    puts '   - Attached external repair'
×
129
  end
130
end
131

132
def cost_return_report(row)
1✔
133
  CostReturnReport.create! do |crr|
×
134
    crr.shipping_cost = row['Cost of Shipping to Vendor'].tr('$', '').to_i if row['Cost of Shipping to Vendor'].present?
×
135
    crr.repair_estimate = row['Cost Estimate of Repair'].tr('$', '').to_i if row['Cost Estimate of Repair'].present?
×
136
    crr.repair_cost = row['Actual Cost Billed for Repair'].tr('$', '').to_i if row['Actual Cost Billed for Repair'].present?
×
137
    if row['Date invoice sent to Business office'].present?
×
138
      crr.invoice_sent_to_business_office = Date.strptime(row['Date invoice sent to Business office'], '%m/%d/%Y')
×
139
    end
140
    crr.complete = row['COMPLETE (Returned to origin)']
×
141
    crr.returned_to_origin = Date.strptime(row['Date Returned to Origin'], '%m/%d/%Y') if row['Date Returned to Origin'].present?
×
142
    crr.note = row['Note']
×
143
    crr.conservation_record_id = row['Database ID']
×
144
    puts '   - Attached cost return report'
×
145
  end
146
end
147

148
def con_tech_record(row)
1✔
149
  if row['Reviewed by'].present?
×
150
    ConTechRecord.create!(performed_by_user_id: user_lookup(row['Reviewed by']),
×
151
                          conservation_record_id: ConservationRecord.find_by(item_record_number: row['Item Record #']).id)
152
    puts '   - Attached con_tech_record'
×
153
  end
154
  return if row['Technicians'].blank?
×
155

156
  # split names by seperators and create con-tech records for each
157
  row['Technicians'].split(%r{[&,-/]}) do |name|
×
158
    ConTechRecord.create!(performed_by_user_id: user_lookup(name.strip),
×
159
                          conservation_record_id: ConservationRecord.find_by(item_record_number: row['Item Record #']).id)
160
    puts '   - Attached con_tech_record'
×
161
  end
162
end
163

164
namespace :batch do
1✔
165
  desc 'Load repair_type controlled vocabulary'
1✔
166
  task repair_type_controlled_vocabulary: :environment do
1✔
167
    puts '## Controlled Vocab repair_type - Batch Load complete'
×
168
    require 'csv'
×
169
    filename = 'lib/assets/types_of_repairs.csv'
×
170

171
    CSV.foreach(filename, col_sep: ',', headers: true) do |row|
×
172
      ControlledVocabulary.create!(vocabulary: 'repair_type', key: row[1], active: true)
×
173
      puts "Created controlled vocab for repair_type: #{row[1]}"
×
174
    end
175
  end
176

177
  desc 'Load department controlled vocabulary'
1✔
178
  task department_controlled_vocabulary: :environment do
1✔
179
    puts '## Controlled vocab deparment - Batch Load complete'
×
180
    require 'csv'
×
181
    filename = 'lib/assets/departments.csv'
×
182
    CSV.foreach(filename, col_sep: ',', headers: true) do |row|
×
183
      ControlledVocabulary.create!(vocabulary: 'department', key: row[1], active: true)
×
184
      puts "Created controlled vocab for department: #{row[1]}"
×
185
    end
186
  end
187

188
  desc 'Load staff code  controlled vocabulary'
1✔
189
  task staff_code_controlled_vocabulary: :environment do
1✔
190
    puts '## Controlled Vocab staff code - Batch Load complete'
×
191
    require 'csv'
×
192
    filename = 'lib/assets/types_of_staff_code.csv'
×
193
    CSV.foreach(filename, col_sep: ',', headers: true) do |row|
×
194
      StaffCode.create!(code: row[1], points: row[2])
×
195
      puts "Created controlled vocab for staff code: #{row[1]}"
×
196
    end
197
  end
198

199
  desc 'Load housing controlled vocabulary'
1✔
200
  task housing_controlled_vocabulary: :environment do
1✔
201
    puts '## Controlled Vocab housing - Batch Load complete'
×
202
    require 'csv'
×
203
    filename = 'lib/assets/housing.csv'
×
204
    CSV.foreach(filename, col_sep: ',', headers: true) do |row|
×
205
      ControlledVocabulary.create!(vocabulary: 'housing', key: row[1], active: true)
×
206
      puts "Created controlled vocab for housing: #{row[1]}"
×
207
    end
208
  end
209

210
  desc 'Load Contract Conservators controlled vocabulary'
1✔
211
  task contract_conservators_controlled_vocabulary: :environment do
1✔
212
    puts '## Controlled Vocab contract conservators - Batch Load complete'
×
213
    require 'csv'
×
214
    filename = 'lib/assets/contract_conservators.csv'
×
215
    CSV.foreach(filename, col_sep: ',', headers: true) do |row|
×
216
      ControlledVocabulary.create!(vocabulary: 'contract_conservator', key: row[1], active: true)
×
217
      puts "Created controlled vocab for contract conservator: #{row[1]}"
×
218
    end
219
  end
220

221
  desc 'Load_Users'
1✔
222
  task load_users: :environment do
1✔
223
    file = ENV['CSV_LOCATION'].presence || 'lib/assets/users.csv'
×
224
    CSV.foreach(file, headers: true, return_headers: false) do |i|
×
225
      user = User.create(display_name: i[0], email: i[1], role: i[2])
×
226
      user.save(validate: false)
×
227
      puts "Account created: #{user.display_name}"
×
228
    end
229
    puts '##  User load complete'
×
230
  end
231

232
  # call rake task with:
233
  #
234
  # rails batch:load:conservation_records CSV_LOCATION="/tmp/sample_batch_metadata.csv"
235

236
  desc 'Load multiple Conservation records from CSV'
1✔
237
  task conservation_records: :environment do
1✔
238
    # Rake::Task["batch:department_controlled_vocabulary"].execute
239

240
    csv = open_input_csv
×
241
    if (File.readlines('lib/assets/conservation_record_headers.txt').map(&:chomp).sort <=> csv.headers.sort) != 0
×
242
      abort('**CSV Header Validation failed, check headers and in input CSV**')
×
243
    end
244

245
    csv.each do |row|
×
246
      conservation_record = ConservationRecord.new
×
247
      puts "Conservation Record created: #{row['Database ID']}"
×
248
      conservation_record.id = row['Database ID'].to_i
×
249
      if row['Date received in Preservation Services'].present?
×
250
        conservation_record.date_received_in_preservation_services = Date.strptime(row['Date received in Preservation Services'], '%m/%d/%Y')
×
251
      end
252
      conservation_record.title = row['Title']
×
253
      conservation_record.author = row['Author']
×
254
      conservation_record.imprint = row['Imprint']
×
255
      conservation_record.call_number = row['Call Number']
×
256
      conservation_record.item_record_number = row['Item Record #']
×
257
      conservation_record.digitization = row['Digitization'].to_i.positive?
×
258
      # Use $departments global array to lookup input_key > vocab_term > target_id
259
      conservation_record.department = vocab('department', row['Department'])
×
260
      conservation_record.save!
×
261
      # Add repair records
262
      create_in_house_repair_reports(row)
×
263
      create_external_repair_reports(row)
×
264
      # Add cost return report data
265
      cost_return_report(row)
×
266
    end
267
    puts '## Conservation Record Batch load completed ##'
×
268
  end
269

270
  desc 'Load treatment report records from CSV'
1✔
271
  task treatment_reports: :environment do
1✔
272
    csv = open_input_csv
×
273

274
    # Validate CSV headers
275
    if (File.readlines('lib/assets/treatment_report_headers.txt').map(&:chomp).sort <=> csv.headers.sort) != 0
×
276
      abort('**CSV Header Validation failed, check headers and in input CSV**')
×
277
    end
278

279
    csv.each do |row|
×
280
      treatment_report = TreatmentReport.new
×
281
      #      treatment_report.id = row['Treatment ID'].to_i
282
      treatment_report.description_general_remarks = row['Description General Remarks']
×
283
      treatment_report.description_binding = row['Description Binding']
×
284
      treatment_report.description_textblock = row['Description Textblock']
×
285
      treatment_report.description_primary_support = row['Description Primary Support']
×
286
      treatment_report.description_medium = row['Description Media']
×
287
      treatment_report.description_attachments_inserts = row['Description Attachments|Inserts']
×
288
      treatment_report.description_housing = row['Description Housing']
×
289
      treatment_report.condition_summary = row['Condition Summary']
×
290
      treatment_report.condition_binding = row['Condition Binding']
×
291
      treatment_report.condition_textblock = row['Condition Textblock']
×
292
      treatment_report.condition_primary_support = row['Condition Primary Support']
×
293
      treatment_report.condition_medium = row['Condition Medium']
×
294
      treatment_report.condition_housing_id = vocab('housing', @housing[row['Condition Housing']]) if row['Condition Housing'].present?
×
295
      treatment_report.condition_housing_narrative = row['Condition Housing Narrative']
×
296
      treatment_report.condition_attachments_inserts = row['Condition Attachments|Inserts']
×
297
      treatment_report.condition_previous_treatment = row['Condition Foreign substances']
×
298
      treatment_report.condition_materials_analysis = row['Condition Testing']
×
299
      treatment_report.treatment_proposal_proposal = row['Treatment Proposal']
×
300
      treatment_report.treatment_proposal_factors_influencing_treatment = row['Notes and Cautions']
×
301
      if row['Treatment Proposed Housing'].present?
×
302
        treatment_report.treatment_proposal_housing_need_id = vocab('housing', @housing[row['Treatment Proposed Housing']])
×
303
      end
304
      treatment_report.treatment_proposal_performed_treatment = row['Performed Treatment']
×
305

306
      treatment_report.treatment_proposal_housing_provided_id = vocab('housing', @housing[row['Performed Housing']]) if row['Performed Housing'].present?
×
307

308
      treatment_report.treatment_proposal_housing_narrative = row['Performed Housing Provided']
×
309
      treatment_report.treatment_proposal_storage_and_handling_notes = row['Performed Storage and Handlling Notes']
×
310
      if row['Performed Treatment Time'].present?
×
311
        treatment_report.treatment_proposal_total_treatment_time = @treatment_time[row['Performed Treatment Time']]
×
312
      end
313
      if (conservation_record = ConservationRecord.find_by(item_record_number: row['Item Record #']))
×
314
        treatment_report.conservation_record_id = conservation_record.id
×
315
        treatment_report.save!
×
316
        puts "Created treatment report #{row['Treatment ID']} -- Conservation Record #{conservation_record.id}"
×
317
        # Add technicians
318
        con_tech_record(row)
×
319
      else
320
        puts ">> Treatment Report #{row['Treatment ID']} not created -- Conservation Record number: #{row['Item Record #']}"
×
321
      end
322
    end
323
  end
324
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc