Class: Tricle::Mailer

Inherits:
ActionMailer::Base
  • Object
show all
Includes:
ActiveSupport::DescendantsTracker
Defined in:
lib/tricle/mailer.rb

Constant Summary collapse

CSS =
File.read(File.join(File.dirname(__FILE__), 'templates', 'email.css')).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.group(title) ⇒ Object



52
53
54
55
# File 'lib/tricle/mailer.rb', line 52

def group(title)
  self.report.add_group(title)
  yield if block_given?
end

.inherited(klass) ⇒ Object



47
48
49
50
# File 'lib/tricle/mailer.rb', line 47

def inherited(klass)
  klass.report = Tricle::Presenters::Report.new
  super(klass)
end

.list(klass, opts = {}, &block) ⇒ Object



61
62
63
# File 'lib/tricle/mailer.rb', line 61

def list(klass, opts = {}, &block)
  self.report.add_list(klass, opts, &block)
end

.metric(klass, opts = {}) ⇒ Object



57
58
59
# File 'lib/tricle/mailer.rb', line 57

def metric(klass, opts = {})
  self.report.add_metric(klass, opts)
end

.send_allObject



65
66
67
68
69
70
71
72
73
# File 'lib/tricle/mailer.rb', line 65

def send_all
  mailers = Tricle::Mailer.descendants
  puts "Sending #{mailers.size} emails..."
  mailers.each do |klass|
    puts "Sending #{klass.name}..."
    klass.email.deliver
  end
  puts "Done."
end

.send_all_if_beginning_of_weekObject



75
76
77
78
79
80
81
82
# File 'lib/tricle/mailer.rb', line 75

def send_all_if_beginning_of_week
  time = Tricle::Time.new
  if time.beginning_of_week?
    self.send_all
  else
    puts "Skipping send, because it's not the beginning of the week."
  end
end

Instance Method Details

#email(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/tricle/mailer.rb', line 31

def email(options = {})
  options = {
    subject: self.subject
  }.merge(options)

  @report = self.report

  message = mail(options) do |format|
    format.html { render 'templates/email' }
    format.text { render 'templates/email' }
  end

  premailer(message)
end

#premailer(message) ⇒ Object



25
26
27
28
29
# File 'lib/tricle/mailer.rb', line 25

def premailer(message)
  # message.text_part.body = Premailer.new(message.text_part.body.to_s, with_html_string: true).to_plain_text
  message.html_part.body = Premailer.new(message.html_part.body.to_s, css_string: CSS.dup, with_html_string: true).to_inline_css
  message
end

#subjectObject



21
22
23
# File 'lib/tricle/mailer.rb', line 21

def subject
  "Your #{self.class.name.titleize}"
end