Class: Wco::Lead

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, 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



55
56
57
58
59
# File 'app/models/wco/lead.rb', line 55

def self.find_or_create_by_email email
  email = self.normalize_email email
  out   = where( email: email ).first
  out ||= create!( email: email )
end

.listObject



99
100
101
# File 'app/models/wco/lead.rb', line 99

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

.normalize_email(a) ⇒ Object



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

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?



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

field :comment

#convsObject



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

def convs; conversations; end

#ctxsObject



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

def ctxs; email_contexts; end

#nameObject



16
17
18
19
20
21
22
23
24
# File 'app/models/wco/lead.rb', line 16

def name
  if !self[:name].present?
    if email
      _name = (email[/\A[a-zA-Z]+/] || 'associate').capitalize
      update_attributes( name: _name )
    end
  end
  self[:name]
end

#normalize_emailObject



34
35
36
# File 'app/models/wco/lead.rb', line 34

def normalize_email
  self[:email] = Wco::Lead.normalize_email email
end

#schsObject



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

def schs; email_actions; end

#set_leadsetObject



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

def set_leadset
  domain         = email.split('@')[1]
  if existing = Wco::Leadset.where({ company_url: domain }).first
    self.leadset = existing
  else
    root_domain    = PublicSuffix.domain(domain)
    self.leadset ||= Wco::Leadset.find_or_create_by({ company_url: root_domain })
  end
end

#to_sObject



95
96
97
98
# File 'app/models/wco/lead.rb', line 95

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

#unsubscribe_tokenObject



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

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