Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow email_prefix to be a proc #416

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,9 @@ Who the message is destined for, can be a string of addresses, an array of addre

##### email_prefix

*String, default: [ERROR]*
*String/Proc, default: [ERROR]*

The subject's prefix of the message.
The subject's prefix of the message. It can be a proc that returns a string. The proc will be evaluated when the mail is sent.

##### sections

Expand Down
7 changes: 5 additions & 2 deletions lib/exception_notifier/email_notifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def method_missing(*args, &block)

def self.extended(base)
base.class_eval do
attr_reader :env, :exception, :request, :data

self.send(:include, ExceptionNotifier::BacktraceCleaner)

# Append application view path to the ExceptionNotifier lookup context.
Expand Down Expand Up @@ -59,7 +61,7 @@ def background_exception_notification(exception, options={}, default_options={})
private

def compose_subject
subject = "#{@options[:email_prefix]}"
subject = "#{maybe_call(@options[:email_prefix])}"
subject << "(#{@options[:accumulated_errors_count]} times)" if @options[:accumulated_errors_count].to_i > 1
subject << "#{@kontroller.controller_name} #{@kontroller.action_name}" if @kontroller && @options[:include_controller_and_action_names_in_subject]
subject << " (#{@exception.class})"
Expand Down Expand Up @@ -130,7 +132,8 @@ def load_custom_views
end

def maybe_call(maybe_proc)
maybe_proc.respond_to?(:call) ? maybe_proc.call : maybe_proc
return maybe_proc unless maybe_proc.respond_to?(:call)
(maybe_proc.arity == 0) ? maybe_proc.call : maybe_proc.call(self)
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions test/exception_notifier/email_notifier_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,21 @@ class EmailNotifierTest < ActiveSupport::TestCase
assert_equal %w{[email protected]}, mail.to
end

test "should lazily evaluate email_prefix" do
email_prefixes = %w(first second)
email_notifier = ExceptionNotifier::EmailNotifier.new(
:email_prefix => -> { email_prefixes.shift },
:sender_address => %{"Dummy Notifier" <[email protected]>},
:exception_recipients => "[email protected]",
:delivery_method => :test,
)

mail = email_notifier.call(@exception)
assert_equal %|first (ZeroDivisionError) "divided by 0"|, mail.subject
mail = email_notifier.call(@exception)
assert_equal %|second (ZeroDivisionError) "divided by 0"|, mail.subject
end

test "should prepend accumulated_errors_count in email subject if accumulated_errors_count larger than 1" do
ActionMailer::Base.deliveries.clear

Expand Down