class Wco::Lead

def self.find_or_create_by_email email

def self.find_or_create_by_email email
  email = self.normalize_email email
  out = where( email: email ).first
  if !out
    domain    = email.split('@')[1]
    leadset   = Wco::Leadset.where(  company_url: domain ).first
    leadset ||= Wco::Leadset.create( company_url: domain, email: email )
    out = create!( email: email, leadset: leadset )
  end
  return out
end

def self.list

def self.list
  [[nil,nil]] + all.map { |p| [ p.email, p.id ] }
end

def self.normalize_email a

def self.normalize_email a
  a = a.downcase
  if a.index('+')
    a.slice!( a[a.index('+')...a.index('@')] )
  end
  return a
end

def convs; conversations; end

def convs; conversations; end

def ctxs; email_contexts; end

def ctxs; email_contexts; end

def normalize_email

def normalize_email
  self[:email] = email.downcase
  if email.index('+')
    a = email
    a.slice!( a[a.index('+')...a.index('@')] )
    self[:email] = a
  end
end

def schs; email_actions; end

def schs; email_actions; end

def set_leadset

def set_leadset
  domain         = email.split('@')[1]
  self.leadset ||= Wco::Leadset.find_or_create_by({ company_url: domain })
end

def to_s

def to_s
  "#{name} <#{email}>"
end

def unsubscribe_token

def unsubscribe_token
  if !self[:unsubscribe_token]
    update_attributes({ unsubscribe_token: (0...8).map { (65 + rand(26)).chr }.join })
  end
  self[:unsubscribe_token]
end