Module: Postpeek::Metadata::ActionMailerContext

Defined in:
lib/postpeek/metadata/action_mailer_context.rb

Constant Summary collapse

THREAD_KEY_CLASS =
:postpeek_mailer_class
THREAD_KEY_ACTION =
:postpeek_mailer_action

Class Method Summary collapse

Class Method Details

.current_actionObject



25
26
27
# File 'lib/postpeek/metadata/action_mailer_context.rb', line 25

def self.current_action
  Thread.current[THREAD_KEY_ACTION]
end

.current_mailer_classObject



21
22
23
# File 'lib/postpeek/metadata/action_mailer_context.rb', line 21

def self.current_mailer_class
  Thread.current[THREAD_KEY_CLASS]
end

.install!Object

Installs an around_action on ActionMailer::Base. Idempotent: guarded by @installed class-level flag.



31
32
33
34
35
36
37
38
# File 'lib/postpeek/metadata/action_mailer_context.rb', line 31

def self.install!
  return if @installed

  @installed = true
  ActionMailer::Base.around_action do |mailer, action|
    ActionMailerContext.with(mailer.class.name, mailer.action_name) { action.call }
  end
end

.with(mailer_class, action_name) ⇒ Object

Stores mailer context in Thread.current, yields, clears in ensure.

Parameters:

  • mailer_class (String)
  • action_name (String)


12
13
14
15
16
17
18
19
# File 'lib/postpeek/metadata/action_mailer_context.rb', line 12

def self.with(mailer_class, action_name)
  Thread.current[THREAD_KEY_CLASS]  = mailer_class
  Thread.current[THREAD_KEY_ACTION] = action_name
  yield
ensure
  Thread.current[THREAD_KEY_CLASS]  = nil
  Thread.current[THREAD_KEY_ACTION] = nil
end