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_all(with_period = nil) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/tricle/mailer.rb', line 82

def send_all(with_period = nil)
  klasses = Tricle::Mailer.descendants.select do |klass|
    !with_period || (klass.period == with_period)
  end

  send_mailers(klasses)
end

.send_at_periodObject



78
79
80
# File 'lib/tricle/mailer.rb', line 78

def send_at_period
  send_mailers(Tricle::Mailer.descendants.select(&:send_today?))
end

.send_today?Boolean

Returns:

  • (Boolean)


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

def send_today?
  time = Tricle::Time.new

  case self.period
  when :day
    true
  when :week
    time.beginning_of_week?
  when :month
    time.beginning_of_month?
  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