Class: Forklift::Base::Mailer

Inherits:
Object
  • Object
show all
Defined in:
lib/forklift/base/mailer.rb

Defined Under Namespace

Classes: ERBBinding

Instance Method Summary collapse

Constructor Details

#initialize(forklift) ⇒ Mailer

Returns a new instance of Mailer.



9
10
11
# File 'lib/forklift/base/mailer.rb', line 9

def initialize(forklift)
  @forklift = forklift
end

Instance Method Details

#configObject

Public: Pull out the settings from config/email.yml.

Returns a Hash with all symbolized keys.



16
17
18
19
# File 'lib/forklift/base/mailer.rb', line 16

def config
  config_file = "#{forklift.config[:project_root]}/config/email.yml"
  @config ||= forklift.utils.load_yml(config_file).deep_symbolize_keys
end

#forkliftObject



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

def forklift
  @forklift
end

#message_defaultsObject



25
26
27
28
29
30
31
# File 'lib/forklift/base/mailer.rb', line 25

def message_defaults
  {
    from:     "Forklift",
    subject:  "Forklift has moved your database @ #{Time.new}",
    body:     "Forklift has moved your database @ #{Time.new}",
  }
end

#send(args, attachment_lines = []) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/forklift/base/mailer.rb', line 41

def send(args, attachment_lines=[])
  params = message_defaults
  [:to, :from, :subject, :body].each do |i|
    params[i] = args[i] unless args[i].nil?
  end
  if attachment_lines.length > 0
    params[:attachments] = {"log.txt" => attachment_lines.join("\r\n")}
  end
  deliver(params)
end

#send_template(args, template_file, variables, attachment_lines = []) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/forklift/base/mailer.rb', line 33

def send_template(args, template_file, variables, attachment_lines=[])
  renderer = ERB.new(File.read(template_file))
  binder = ERBBinding.new(variables)
  body = renderer.result(binder.get_binding)
  args[:body] = body
  send(args, attachment_lines)
end