Skip to content

Commit 3b3ee06

Browse files
authored
Merge pull request #374 from ImMatureTony/allow-for-excluding-controller-and-action-names-in-email-notifier-subject
added :include_controller_and_action_names_in_subject as option for EmailNotifier
2 parents 46ec430 + d3b0d4e commit 3b3ee06

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ If enabled, include the exception message in the subject. Use `:verbose_subject
279279

280280
If enabled, remove numbers from subject so they thread as a single one. Use `:normalize_subject => true` to enable it.
281281

282+
##### include_controller_and_action_names_in_subject
283+
284+
*Boolean, default: true*
285+
286+
If enabled, include the controller and action names in the subject. Use `:include_controller_and_action_names_in_subject => false` to exclude them.
287+
282288
##### email_format
283289

284290
*Symbol, default: :text*

lib/exception_notifier/email_notifier.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class EmailNotifier < BaseNotifier
99
attr_accessor(:sender_address, :exception_recipients,
1010
:pre_callback, :post_callback,
1111
:email_prefix, :email_format, :sections, :background_sections,
12-
:verbose_subject, :normalize_subject, :delivery_method, :mailer_settings,
13-
:email_headers, :mailer_parent, :template_path, :deliver_with)
12+
:verbose_subject, :normalize_subject, :include_controller_and_action_names_in_subject,
13+
:delivery_method, :mailer_settings, :email_headers, :mailer_parent, :template_path, :deliver_with)
1414

1515
module Mailer
1616
class MissingController
@@ -60,7 +60,7 @@ def background_exception_notification(exception, options={}, default_options={})
6060

6161
def compose_subject
6262
subject = "#{@options[:email_prefix]}"
63-
subject << "#{@kontroller.controller_name}##{@kontroller.action_name}" if @kontroller
63+
subject << "#{@kontroller.controller_name}##{@kontroller.action_name}" if @kontroller && @options[:include_controller_and_action_names_in_subject]
6464
subject << " (#{@exception.class})"
6565
subject << " #{@exception.message.inspect}" if @options[:verbose_subject]
6666
subject = EmailNotifier.normalize_digits(subject) if @options[:normalize_subject]
@@ -142,10 +142,10 @@ def initialize(options)
142142
options[:mailer_settings] = options.delete(mailer_settings_key)
143143

144144
options.reverse_merge(EmailNotifier.default_options).select{|k,v|[
145-
:sender_address, :exception_recipients,
146-
:pre_callback, :post_callback,
147-
:email_prefix, :email_format, :sections, :background_sections,
148-
:verbose_subject, :normalize_subject, :delivery_method, :mailer_settings,
145+
:sender_address, :exception_recipients, :pre_callback,
146+
:post_callback, :email_prefix, :email_format,
147+
:sections, :background_sections, :verbose_subject, :normalize_subject,
148+
:include_controller_and_action_names_in_subject, :delivery_method, :mailer_settings,
149149
:email_headers, :mailer_parent, :template_path, :deliver_with].include?(k)}.each{|k,v| send("#{k}=", v)}
150150
end
151151

@@ -201,6 +201,7 @@ def self.default_options
201201
:background_sections => %w(backtrace data),
202202
:verbose_subject => true,
203203
:normalize_subject => false,
204+
:include_controller_and_action_names_in_subject => true,
204205
:delivery_method => nil,
205206
:mailer_settings => nil,
206207
:email_headers => {},

test/dummy/test/functional/posts_controller_test.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PostsControllerTest < ActionController::TestCase
3535
test "mail subject should have the proper prefix" do
3636
assert_includes @mail.subject, "[Dummy ERROR]"
3737
end
38-
38+
3939
test "mail subject should include descriptive error message" do
4040
assert_includes @mail.subject, "(NoMethodError) \"undefined method `nw'"
4141
end
@@ -146,6 +146,25 @@ class PostsControllerTestWithoutVerboseSubject < ActionController::TestCase
146146
end
147147
end
148148

149+
class PostsControllerTestWithoutControllerAndActionNames < ActionController::TestCase
150+
tests PostsController
151+
setup do
152+
@email_notifier = ExceptionNotifier::EmailNotifier.new(:include_controller_and_action_names_in_subject => false)
153+
begin
154+
post :create, method: :post
155+
rescue => e
156+
@exception = e
157+
@mail = @email_notifier.create_email(@exception, {:env => request.env})
158+
end
159+
end
160+
161+
test "should include controller and action names in subject" do
162+
assert_includes @mail.subject, '[ERROR]'
163+
assert_includes @mail.subject, '(NoMethodError)'
164+
refute_includes @mail.subject, 'posts#create'
165+
end
166+
end
167+
149168
class PostsControllerTestWithSmtpSettings < ActionController::TestCase
150169
tests PostsController
151170
setup do

0 commit comments

Comments
 (0)