Class: Wco::Lead

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Paranoia, Mongoid::Timestamps
Defined in:
app/models/wco/lead.rb

Constant Summary collapse

PAGE_PARAM_NAME =
'leads_page'
OP_DELETE =
'delete'
OP_ADD_TO_CAMPAIGN =
'add_to_campaign'
OPS =
[ OP_DELETE, OP_ADD_TO_CAMPAIGN ]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_by_email(email) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/wco/lead.rb', line 42

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

.listObject



88
89
90
# File 'app/models/wco/lead.rb', line 88

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

.normalize_email(a) ⇒ Object



35
36
37
38
39
40
41
# File 'app/models/wco/lead.rb', line 35

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

Instance Method Details

#commentObject

_TODO: replace with log?



17
# File 'app/models/wco/lead.rb', line 17

field :comment

#convsObject



60
# File 'app/models/wco/lead.rb', line 60

def convs; conversations; end

#ctxsObject



62
# File 'app/models/wco/lead.rb', line 62

def ctxs; email_contexts; end

#normalize_emailObject



27
28
29
30
31
32
33
34
# File 'app/models/wco/lead.rb', line 27

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

#schsObject



64
# File 'app/models/wco/lead.rb', line 64

def schs; email_actions; end

#set_leadsetObject



22
23
24
25
# File 'app/models/wco/lead.rb', line 22

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

#to_sObject



84
85
86
87
# File 'app/models/wco/lead.rb', line 84

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

#unsubscribe_tokenObject



77
78
79
80
81
82
# File 'app/models/wco/lead.rb', line 77

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