Class: WcoEmail::Context

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



99
100
101
# File 'app/models/wco_email/context.rb', line 99

def tid
  @tid
end

Class Method Details

.notsentObject



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

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

.scheduledObject



81
82
83
# File 'app/models/wco_email/context.rb', line 81

def self.scheduled
  where( :send_at.lte => Time.now  )
end

.summaryObject



108
109
110
111
112
113
114
115
116
117
# File 'app/models/wco_email/context.rb', line 108

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

Instance Method Details

#bodyObject

Looks good… 2024-01-17



28
29
30
31
32
33
34
# File 'app/models/wco_email/context.rb', line 28

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

#clear_bodyObject



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

def clear_body
  tmp = ActionView::Base.full_sanitizer.sanitize body
  if tmp.blank?
    self[:body] = nil
  end
end

#configObject



119
120
121
122
# File 'app/models/wco_email/context.rb', line 119

def config
  @config ||= OpenStruct.new JSON.parse( tmpl[:config_json] )
  @config
end

#from_emailObject



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

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

#get_bindingObject



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

def get_binding
  @lead             = lead
  @utm_tracking_str = utm_tracking_str
  # eval( tmpl.config_exe ) ## @TODO: remove? 2024-01-17
  binding()
end

#preview_strObject



18
19
20
21
22
23
24
# File 'app/models/wco_email/context.rb', line 18

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

#schObject



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

def sch; email_action; end

#subjectObject



56
57
58
# File 'app/models/wco_email/context.rb', line 56

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

#tmplObject



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

def tmpl; email_template; end

#to_emailObject



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

def to_email
  lead.email
end

#unsubscribe_urlObject



133
134
135
136
137
138
139
140
# File 'app/models/wco_email/context.rb', line 133

def unsubscribe_url
  Wco::Engine.routes.url_helpers.unsubscribes_url({
    host:        Rails.application.routes.default_url_options[:host],
    lead_id:     lead_id,
    template_id: tmpl.id,
    token:       lead.unsubscribe_token,
  })
end

#utm_tracking_strObject



124
125
126
127
128
129
130
131
# File 'app/models/wco_email/context.rb', line 124

def utm_tracking_str
  {
    'cid'          => lead_id,
    'utm_campaign' => tmpl.slug,
    'utm_medium'   => 'email',
    'utm_source'   => tmpl.slug,
  }.map { |k, v| "#{k}=#{v}" }.join("&")
end