module ActionMailerAutoPreviews

This gem uses sensible defaults, but can be altered as follows: @example

ActionMailerAutoPreviews.setup do |config|
  config.enabled = true
  config.history_limit = 10
  config.intercept_mode = :preview_only
  config.launch_browser = true
  config.logger = Rails.logger
  config.preview_host_url = "http://localhost:3000"
end

Constants

MAIL_CACHE

This is a cache that holds onto the Mail objects for viewing via the ActionMailer::Preview web pages. This cache will grow up the max capacity defined in the :history_limit attribute.

VERSION

@private

Public Class Methods

setup() { |self| ... } click to toggle source

Call with a block to override one or more of the default configuration options. @example

ActionMailerAutoPreviews.setup do |config|
  config.launch_browser = false
  config.history_limit = 10
end
# File lib/action_mailer_auto_previews.rb, line 49
def self.setup
  yield self
end

Private Class Methods

auto_preview(message_delivery) click to toggle source

This method is the magic sauce of what we’re trying to achieve. It is called via the patch on ActionMailer::MessageDelivery

# File lib/action_mailer_auto_previews.rb, line 60
def self.auto_preview(message_delivery)
  # Generate a unique preview key
  preview_key = "preview_#{Time.now.to_i}#{rand(1000)}"

  # Store the message object that contains the data for the preview
  ActionMailerAutoPreviews::MAIL_CACHE[preview_key] = message_delivery

  # Dynamically add a ActionMailer::Preview method, named after `preview_key`
  eval %Q(
    class ActionMailerAutoPreviews::AutomaticPreviewMailer < ActionMailer::Preview
      define_method("#{preview_key}") do
        ActionMailerAutoPreviews::MAIL_CACHE["#{preview_key}"]
      end
    end
  )

  # Launch the default browser to the newly-available ActionMailer::Preview
  preview_url = "#{ActionMailerAutoPreviews.preview_host_url}/rails/mailers/action_mailer_auto_previews/automatic_preview_mailer/#{preview_key}"
  ActionMailerAutoPreviews.launch_browser ? Launchy.open(preview_url) : ActionMailerAutoPreviews.logger.info("New ActionMailer::Preview available at #{preview_url}")

  # Clean up the cache if the number of generated previews now exceeds `history_limit`
  if ActionMailerAutoPreviews::MAIL_CACHE.count > ActionMailerAutoPreviews.history_limit
    preview_key_to_purge = ActionMailerAutoPreviews::MAIL_CACHE.keys.first # Get the oldest preview_key
    ActionMailerAutoPreviews::MAIL_CACHE.delete preview_key_to_purge # Delete the reference to the mail object
    eval %Q(
      class ActionMailerAutoPreviews::AutomaticPreviewMailer < ActionMailer::Preview
        remove_method("#{preview_key_to_purge}")
      end
    ) # Removes the ActionMailer::Preview method, so the Preview is no longer available and the old URL will stop functioning
    ActionMailerAutoPreviews.logger.info("ActionMailer::Preview #{preview_key_to_purge} has been purged, history buffer is full")
  end
end
logger() click to toggle source

Logic to return the appropriate logger to be used by this gem

# File lib/action_mailer_auto_previews.rb, line 55
def self.logger
  @@logger ||= Object.const_defined?('Rails') ? Rails.logger : Logger.new(STDOUT)
end