From 3c7c939c1ec0e0f1d7fb3cb1267a06614104ef22 Mon Sep 17 00:00:00 2001 From: Corin Langosch Date: Tue, 15 May 2018 20:34:59 +0200 Subject: [PATCH 1/3] Allow email_prefix to be a proc --- README.md | 4 ++-- lib/exception_notifier/email_notifier.rb | 2 +- test/exception_notifier/email_notifier_test.rb | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f5a1ee26..4345ba8a 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/exception_notifier/email_notifier.rb b/lib/exception_notifier/email_notifier.rb index d5a68ddc..39bd2080 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -59,7 +59,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})" diff --git a/test/exception_notifier/email_notifier_test.rb b/test/exception_notifier/email_notifier_test.rb index 442f258c..a0e552a3 100644 --- a/test/exception_notifier/email_notifier_test.rb +++ b/test/exception_notifier/email_notifier_test.rb @@ -219,6 +219,21 @@ class EmailNotifierTest < ActiveSupport::TestCase assert_equal %w{second@example.com}, 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" }, + :exception_recipients => "test@exampke.com", + :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 From b80ce40117710338b7e68106488a83be31c188aa Mon Sep 17 00:00:00 2001 From: Corin Langosch Date: Wed, 16 May 2018 22:06:23 +0200 Subject: [PATCH 2/3] Pass reference to exception notifier blocks --- lib/exception_notifier/email_notifier.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/exception_notifier/email_notifier.rb b/lib/exception_notifier/email_notifier.rb index 39bd2080..2a39cbd3 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -130,7 +130,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 From 0c07ea649dfd0afa8c1590316bc4185e9d867a08 Mon Sep 17 00:00:00 2001 From: Corin Langosch Date: Wed, 16 May 2018 22:14:28 +0200 Subject: [PATCH 3/3] Adds access to some notifier variables in blocks --- lib/exception_notifier/email_notifier.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/exception_notifier/email_notifier.rb b/lib/exception_notifier/email_notifier.rb index 2a39cbd3..412dbcc1 100644 --- a/lib/exception_notifier/email_notifier.rb +++ b/lib/exception_notifier/email_notifier.rb @@ -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.