Class: WcoEmail::Context

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/wco_email/context.rb

Overview

Send a single email

Constant Summary collapse

PAGE_PARAM_NAME =
'email_contexts_page'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#tidObject (readonly)

For tracking / utm



87
88
89
# File 'app/models/wco_email/context.rb', line 87

def tid
  @tid
end

Class Method Details

.notsentObject



65
# File 'app/models/wco_email/context.rb', line 65

def self.notsent; new.notsent; end

.scheduledObject



72
# File 'app/models/wco_email/context.rb', line 72

def self.scheduled; new.scheduled; end

.summaryObject



101
102
103
104
105
106
107
108
109
110
# File 'app/models/wco_email/context.rb', line 101

def self.summary
  pipeline = [
    { '$group' => {
      '_id' => { '$dateToString' => { 'format' => "%Y-%m-%d", 'date' => "$sent_at", 'timezone' => 'America/Chicago' } }, 'total' => { '$sum' => 1 }
    } },
    { '$sort' => { '_id': -1 } },
  ]
  outs = Ish::EmailContext.collection.aggregate( pipeline )
  outs.to_a
end

Instance Method Details

#bodyObject



23
24
25
26
27
28
29
# File 'app/models/wco_email/context.rb', line 23

def body
  if self[:body].presence
    return self[:body]
  else
    return tmpl.body
  end
end

#from_emailObject



34
35
36
37
38
39
40
# File 'app/models/wco_email/context.rb', line 34

def from_email
  if self[:from_email].presence
    return self[:from_email]
  else
    return tmpl.from_email
  end
end

#get_bindingObject



89
90
91
92
93
94
95
96
97
98
99
# File 'app/models/wco_email/context.rb', line 89

def get_binding
  @lead = lead()
  @utm_tracking_str = {
    'cid'          => lead.id,
    'utm_campaign' => tmpl.slug,
    'utm_medium'   => 'email',
    'utm_source'   => tmpl.slug,
  }.map { |k, v| "#{k}=#{v}" }.join("&")
  eval( tmpl.config_exe )
  binding()
end

#leadObject



77
# File 'app/models/wco_email/context.rb', line 77

def lead; Lead.find( lead_id ); end

#lead_idObject

like belongs_to to_lead , but Lead is SQL to just the lead_id



76
# File 'app/models/wco_email/context.rb', line 76

field :lead_id, type: :integer

#notsentObject



62
63
64
# File 'app/models/wco_email/context.rb', line 62

def notsent
  Ish::EmailContext.where( sent_at: nil, unsubscribed_at: nil )
end

#preview_strObject



14
15
16
17
18
19
20
# File 'app/models/wco_email/context.rb', line 14

def preview_str
  if self[:preview_str].presence
    return self[:preview_str]
  else
    return tmpl.preview_str
  end
end

#schObject



51
# File 'app/models/wco_email/context.rb', line 51

def sch; scheduled_email_action; end

#scheduledObject



68
69
70
71
# File 'app/models/wco_email/context.rb', line 68

def scheduled
  # or({ :send_at.lte => Time.now }, { :send_at => nil }) ## This won't work b/c I need draft state!
  Ish::EmailContext.where({ :send_at.lte => Time.now  })
end

#subjectObject



43
44
45
# File 'app/models/wco_email/context.rb', line 43

def subject
  self[:subject].presence || tmpl.subject
end

#tmplObject



48
# File 'app/models/wco_email/context.rb', line 48

def tmpl; email_template; end

#to_emailObject

@TODO: remove, just use the lead. vp 2023-03-27



78
# File 'app/models/wco_email/context.rb', line 78

def to_email; lead[:email]; end