Skip to content

Instantly share code, notes, and snippets.

@justincaldwell
Created March 25, 2010 10:30
Show Gist options
  • Save justincaldwell/343406 to your computer and use it in GitHub Desktop.
Save justincaldwell/343406 to your computer and use it in GitHub Desktop.
class PhotoEmail < ActiveRecord::Base
belongs_to :doctor
belongs_to :kase
belongs_to :photo_email_status
has_many :photos
# Creates a new PhotoEmail from and incoming email
def self.create_from_mail_handler email, mms
pemail = self.new( :subject => mms.subject.gsub(/[Ff][Ww]\W/,'').strip,
:sender => email.from.first.strip,
:body => mms.body.strip )
pemail.save!
end
# Finds similar emails based on the subject
def self.find_similar email
substring = split_subject.inject('') do |t,s|
t << "subject LIKE '%#{s}%' AND "
end
substring.chomp(' AND ')
emails = PhotoEmail.find(:all, :conditions => substring)
emails.delete(email)
emails
end
# Tries to find and save a kase and a doctor
def determine_and_save_kase_and_doc
if (num = subject.match(/\d{6}/))
kase = Kase.find_by_id('CN' + num[0].to_s)
doc = kase.doctor if kase
mm = 'Kase ID'
else
mm = case
when doc = Doctor.find_by_id(subject1) : 'Doctor ID'
when acc = Account.find_by_id(subject1) : doc = acc.doctor; 'Account ID'
when doc = Doctor.find_with_name_like(subject1) : 'Doctor name'
when email = Email.find_by_email(sender) : doc = email.doctor; 'Email'
end
kase = Kase.find_with_patient_like(subject2, doc.id) if doc && subject2
end
self.kase_id = kase.id if kase
self.doctor_id = doc.id if doc
self.matched_by = mm if mm
self.save!
end
def split_subject
terms = subject.split(/[,;:\/-]|[P|p]atient|[P|p]t\.?\s/)
terms.delete_if {|s| s.length < 3 }
end
def subject1
split_subject[0].strip
end
def subject2
split_subject[1].strip
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment