Skip to content

Commit 3c7c939

Browse files
committed
Allow email_prefix to be a proc
1 parent dec18c9 commit 3c7c939

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ Who the message is destined for, can be a string of addresses, an array of addre
182182

183183
##### email_prefix
184184

185-
*String, default: [ERROR]*
185+
*String/Proc, default: [ERROR]*
186186

187-
The subject's prefix of the message.
187+
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.
188188

189189
##### sections
190190

lib/exception_notifier/email_notifier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def background_exception_notification(exception, options={}, default_options={})
5959
private
6060

6161
def compose_subject
62-
subject = "#{@options[:email_prefix]}"
62+
subject = "#{maybe_call(@options[:email_prefix])}"
6363
subject << "(#{@options[:accumulated_errors_count]} times)" if @options[:accumulated_errors_count].to_i > 1
6464
subject << "#{@kontroller.controller_name} #{@kontroller.action_name}" if @kontroller && @options[:include_controller_and_action_names_in_subject]
6565
subject << " (#{@exception.class})"

test/exception_notifier/email_notifier_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,21 @@ class EmailNotifierTest < ActiveSupport::TestCase
219219
assert_equal %w{[email protected]}, mail.to
220220
end
221221

222+
test "should lazily evaluate email_prefix" do
223+
email_prefixes = %w(first second)
224+
email_notifier = ExceptionNotifier::EmailNotifier.new(
225+
:email_prefix => -> { email_prefixes.shift },
226+
:sender_address => %{"Dummy Notifier" <[email protected]>},
227+
:exception_recipients => "[email protected]",
228+
:delivery_method => :test,
229+
)
230+
231+
mail = email_notifier.call(@exception)
232+
assert_equal %|first (ZeroDivisionError) "divided by 0"|, mail.subject
233+
mail = email_notifier.call(@exception)
234+
assert_equal %|second (ZeroDivisionError) "divided by 0"|, mail.subject
235+
end
236+
222237
test "should prepend accumulated_errors_count in email subject if accumulated_errors_count larger than 1" do
223238
ActionMailer::Base.deliveries.clear
224239

0 commit comments

Comments
 (0)