Skip to content

Instantly share code, notes, and snippets.

@jleo3
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jleo3/2a925ce6cc3a6d779459 to your computer and use it in GitHub Desktop.
Save jleo3/2a925ce6cc3a6d779459 to your computer and use it in GitHub Desktop.
@potential_errors =[]
params = { filepicker: true, created_at: DateTime.parse("2015-01-25 14:48:09")..DateTime.parse("2015-04-01 19:50") }
grouped_count = Attachment.where(params).group("original_filename").having("count(1) > 1").count ; nil
grouped_attachments = grouped_count.map { |count| Attachment.where(params.merge(original_filename: count.first)).to_a } ; nil
grouped_attachments.each do |group|
arr = group.map &:document_file_size
if arr.uniq != arr
@potential_errors << group
end
end ; nil
puts "#{@potential_errors.count} groups"
puts "#{@potential_errors.flatten.count} attachments"
@potential_errors.flatten! ; nil
@potential_errors.reject! { |att| att.subject.bill_status.read_attribute(:status) == "done" } ; nil
puts "#{@potential_errors.count} attachments remaining"
@att_errors = []
@potential_errors.each_with_index do |att, i|
puts "counter: #{i + 1}"
begin
# perceived issue with update_attributes! (could be my imagination)
att.document = att.original_document
att.save!
rescue => e
@att_errors << [e.message, att.id, att.subject_id]
end
end ; nil
#
# The code from here down hasn't been used for a couple of days but may nevertheless prove relevant
#
@csv_errors = []
@potential_csv_errors = []
@potential_errors.each_with_index do |group, i|
puts "Working on group #{i +1}"
begin
group.each do |att|
bill = att.subject
unless bill.bill_status.status == "done"
bill.submissions.each do |bs|
@potential_csv_errors << [att.id, bill.id, att.report_type.description, att.original_filename, att.document_file_size, att.document_file_name, bill.bill_status.status, bill.date_of_service, bs.id, bs.created_at, bs.updated_at]
end
end
end
rescue => e
@csv_errors << [e, group]
end
end ; nil
potential_errors_file = CSV.generate do |csv|
csv << ["Attachment ID", "Bill ID", "Report Type", "Original Filename", "Document File Size", "Document Filename", "Bill Status", "Date of Service", "Submission ID", "Submission Created", "Submission Updated"]
@potential_csv_errors.each { |x| csv << x }
end
@errors = []
@with_errors = []
grouped_attachments.each_with_index do |group, i|
puts i
group.each_with_index do |attachment, index|
begin
bill = attachment.subject
group[(index)..-1].each do |remaining_attachment|
bill.submissions.map do |bs|
if bs.created_at > remaining_attachment.created_at && bill.bill_status.status != "done"
@with_errors << [attachment.id, attachment.subject_id, bs.id, bs.created_at]
end
end
end
rescue => e
@errors << [e, group]
end
end
end ; nil
column_names = ["Attachment ID", "Bill ID", "Bill Submission ID", "Bill Submission Created At"]
CSV.generate do |csv|
csv << column_names
@with_errors.each do |err|
csv << err
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment