Skip to content

Commit

Permalink
Merge pull request #330 from kellyjosephprice/feature/lazy-recipients
Browse files Browse the repository at this point in the history
Allow exception_recipients to be a proc
  • Loading branch information
smartinez87 committed Mar 10, 2016
2 parents 5fdedc1 + ac84013 commit bfd0e02
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -144,9 +144,9 @@ Who the message is from.

##### exception_recipients

*String/Array of strings, default: []*
*String/Array of strings/Proc, default: []*

Who the message is destined for, can be a string of addresses, or an array of addresses.
Who the message is destined for, can be a string of addresses, an array of addresses, or it can be a proc that returns a string of addresses or an array of addresses. The proc will be evaluated when the mail is sent.

##### email_prefix

Expand Down
7 changes: 6 additions & 1 deletion lib/exception_notifier/email_notifier.rb
Expand Up @@ -94,10 +94,11 @@ def compose_email
set_data_variables
subject = compose_subject
name = @env.nil? ? 'background_exception_notification' : 'exception_notification'
exception_recipients = maybe_call(@options[:exception_recipients])

headers = {
:delivery_method => @options[:delivery_method],
:to => @options[:exception_recipients],
:to => exception_recipients,
:from => @options[:sender_address],
:subject => subject,
:template_name => name
Expand All @@ -118,6 +119,10 @@ def load_custom_views
self.prepend_view_path Rails.root.nil? ? "app/views" : "#{Rails.root}/app/views"
end
end

def maybe_call(maybe_proc)
maybe_proc.respond_to?(:call) ? maybe_proc.call : maybe_proc
end
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions test/dummy/test/functional/posts_controller_test.rb
Expand Up @@ -222,3 +222,31 @@ class PostsControllerTestBackgroundNotification < ActionController::TestCase
assert_includes @mail.encoded, "* New background section for testing"
end
end

class PostsControllerTestWithExceptionRecipientsAsProc < ActionController::TestCase
tests PostsController
setup do
exception_recipients = %w{first@example.com second@example.com}

@email_notifier = ExceptionNotifier::EmailNotifier.new(
exception_recipients: -> { [ exception_recipients.shift ] }
)

@action = proc do
begin
@post = posts(:one)
post :create, :post => @post.attributes
rescue => e
@exception = e
@mail = @email_notifier.create_email(@exception, {:env => request.env})
end
end
end

test "should lazily evaluate exception_recipients" do
@action.call
assert_equal [ "first@example.com" ], @mail.to
@action.call
assert_equal [ "second@example.com" ], @mail.to
end
end
15 changes: 15 additions & 0 deletions test/exception_notifier/email_notifier_test.rb
Expand Up @@ -182,4 +182,19 @@ class EmailNotifierTest < ActiveSupport::TestCase

assert_equal 1, ActionMailer::Base.deliveries.count
end

test "should lazily evaluate exception_recipients" do
exception_recipients = %w{first@example.com second@example.com}
email_notifier = ExceptionNotifier::EmailNotifier.new(
:email_prefix => '[Dummy ERROR] ',
:sender_address => %{"Dummy Notifier" <dummynotifier@example.com>},
:exception_recipients => -> { [ exception_recipients.shift ] },
:delivery_method => :test
)

mail = email_notifier.call(@exception)
assert_equal %w{first@example.com}, mail.to
mail = email_notifier.call(@exception)
assert_equal %w{second@example.com}, mail.to
end
end

0 comments on commit bfd0e02

Please sign in to comment.