Class: Mail::CacheDelivery

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer_cache_delivery/mail/cache_delivery.rb

Overview

Performs deliveries to temporary cache file, so mails can accessed from other processes.

Default location of files is:

-  "tmp/cache/action_mailer_cache_deliveries.cache" if you use Rails
- "/tmp/cache/action_mailer_cache_deliveries.cache" if you don't use Rails

However, you can overwrite location in configuration:

Examples:

config.action_mailer.cache_settings = { location: "custom/path" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ CacheDelivery

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of CacheDelivery.



21
22
23
24
25
26
# File 'lib/action_mailer_cache_delivery/mail/cache_delivery.rb', line 21

def initialize(settings)
  @settings = settings

  cache_dir = File.dirname(@settings[:location])
  FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir)
end

Instance Attribute Details

#settingsObject



18
19
20
# File 'lib/action_mailer_cache_delivery/mail/cache_delivery.rb', line 18

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/action_mailer_cache_delivery/mail/cache_delivery.rb', line 29

def deliver!(mail)
  # write empty array to cache file if doesn't exist
  unless File.exists?(@settings[:location])
    File.open(@settings[:location], 'w') do |file|
      Marshal.dump([], file)
    end
  end

  # get delivered mails
  mails = ActionMailer::Base.cached_deliveries
  # append new one
  mails << mail
  # write all emails to cache file
  File.open(@settings[:location], 'w') do |file|
    Marshal.dump(mails, file)
  end

  Mail::TestMailer.deliveries << mail
end