This may be the wrong question to ask, but I’ll start here anyway
because this could probably use some optimization.
The code underneath gets a list of all the files in all the folders
specified, then deletes the database entries that aren’t in that list,
updates the entries in both the db and the list, and creates the entries
that aren’t in the db.
Is there a better way to do this?
all_reports is of the form:
{ “a_path” => [“report1”, “report2”, “report3”], “another_path” => […]
… }
list = []
all_reports.each_value { |reps| list.concat reps }
Report.all.each do |rep|
rep.destroy if !list.include? rep.filename
end
all_reports.each do |path, reports|
Dir.chdir path
reports.each do |rep|
report = Report.find_by_filename rep
if report.nil? # If it doesn’t exist, create it
generate(rep, path)
else # If it does exist, update the timestamps.
report.absolute_age = Time.now - File.ctime(rep)
report.age = get_age report.absolute_age # Magic.
aging = modalities.find_by_name(report.modality).aging rescue
DefaultAging
report.old = (report.absolute_age.to_hours > aging) # Yes, I did
create a to_hours
report.save
end
end
end
0