From 1c2af6022a85768913674694a5139718e6f622d2 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Sat, 1 Oct 2011 18:04:36 -0300 Subject: [PATCH] Refactor verbose_subject option. --- lib/exception_notifier/notifier.rb | 15 ++++++++------- test/background_exception_notification_test.rb | 2 +- .../test/functional/posts_controller_test.rb | 2 +- test/exception_notification_test.rb | 4 ++++ 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/exception_notifier/notifier.rb b/lib/exception_notifier/notifier.rb index f1c633e9..651d9aff 100644 --- a/lib/exception_notifier/notifier.rb +++ b/lib/exception_notifier/notifier.rb @@ -59,7 +59,7 @@ def exception_notification(env, exception) instance_variable_set("@#{name}", value) end - subject = compose_subject("#{@kontroller.controller_name}##{@kontroller.action_name}") + subject = compose_subject(exception, @kontroller) mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format| format.text { render "#{mailer_name}/exception_notification" } @@ -72,7 +72,7 @@ def background_exception_notification(exception) @exception = exception @backtrace = exception.backtrace || [] @sections = %w{backtrace} - subject = compose_subject"#{@options[:email_prefix]} (#{exception.class}) #{exception.message.inspect}" + subject = compose_subject(exception) mail(:to => @options[:exception_recipients], :from => @options[:sender_address], :subject => subject) do |format| format.text { render "#{mailer_name}/background_exception_notification" } @@ -82,13 +82,14 @@ def background_exception_notification(exception) private - def compose_subject(name = '') - subject = "#{@options[:email_prefix]}#{name}" - subject << " (#{@exception.class})" - subject << " #{@exception.message.inspect}" if @options[:verbose_subject] + def compose_subject(exception, kontroller=nil) + subject = "#{@options[:email_prefix]}" + subject << "#{kontroller.controller_name}##{kontroller.action_name}" if kontroller + subject << " (#{exception.class})" + subject << " #{exception.message.inspect}" if @options[:verbose_subject] subject.length > 120 ? subject[0...120] + "..." : subject end - + def clean_backtrace(exception) if Rails.respond_to?(:backtrace_cleaner) Rails.backtrace_cleaner.send(:filter, exception.backtrace) diff --git a/test/background_exception_notification_test.rb b/test/background_exception_notification_test.rb index 248d3967..f701f22f 100644 --- a/test/background_exception_notification_test.rb +++ b/test/background_exception_notification_test.rb @@ -27,7 +27,7 @@ class BackgroundExceptionNotificationTest < ActiveSupport::TestCase end test "mail should have a descriptive subject" do - assert @mail.subject.include? "[Dummy ERROR] (ZeroDivisionError) \"divided by 0\"" + assert @mail.subject == "[Dummy ERROR] (ZeroDivisionError) \"divided by 0\"" end test "mail should say exception was raised in background" do diff --git a/test/dummy/test/functional/posts_controller_test.rb b/test/dummy/test/functional/posts_controller_test.rb index b39842a5..d65f6dff 100644 --- a/test/dummy/test/functional/posts_controller_test.rb +++ b/test/dummy/test/functional/posts_controller_test.rb @@ -28,7 +28,7 @@ class PostsControllerTest < ActionController::TestCase end test "mail should have a descriptive subject" do - assert @mail.subject.include? "[Dummy ERROR] # (NoMethodError)" + assert @mail.subject.include? "[Dummy ERROR] # (NoMethodError) \"undefined method `nw'" end test "mail should contain backtrace in body" do diff --git a/test/exception_notification_test.rb b/test/exception_notification_test.rb index f8015c1d..5c5b1eb9 100644 --- a/test/exception_notification_test.rb +++ b/test/exception_notification_test.rb @@ -16,4 +16,8 @@ class ExceptionNotificationTest < ActiveSupport::TestCase test "should have default sections" do assert ExceptionNotifier::Notifier.default_sections == %w(request session environment backtrace) end + + test "should have verbose subject by default" do + assert ExceptionNotifier::Notifier.default_options[:verbose_subject] == true + end end