Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the email delivery method #300

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/noticed/delivery_methods/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ class Email < Base

def deliver
if options[:enqueue]
mailer.with(format).send(mailer_method.to_sym).deliver_later
composed_mailer.deliver_later
else
mailer.with(format).send(mailer_method.to_sym).deliver_now
composed_mailer.deliver_now
end
end

Expand Down Expand Up @@ -50,6 +50,20 @@ def format
end
params.merge(recipient: recipient, record: record)
end

def args
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should standardize on args or arguments? I usually don't like to abbreviate, but Its kind of likeparams and parameters?

return unless (option = options[:arguments])

notification.send(option)
end

def composed_mailer
if options[:arguments]
mailer.with(params).public_send(mailer_method.to_sym, args)
else
mailer.with(params).public_send(mailer_method.to_sym)
end
end
end
end
end
14 changes: 14 additions & 0 deletions test/delivery_methods/email_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ class EmailDeliveryWithActiveJob < Noticed::Base
deliver_by :email, mailer: "UserMailer", enqueue: true, method: "comment_notification"
end

class EmailDeliveryWithArguments < Noticed::Base
deliver_by :email, mailer: "UserMailer", method: :comment_notification_for, arguments: :email_arguments

def email_arguments
params[:recipient]
end
end

class EmailTest < ActiveSupport::TestCase
setup do
@user = users(:one)
Expand Down Expand Up @@ -43,4 +51,10 @@ class EmailTest < ActiveSupport::TestCase
EmailDeliveryWithActiveJob.new.deliver(user)
assert_enqueued_emails 1
end

test "delivers an email when passing an argument" do
assert_emails 1 do
EmailDeliveryWithArguments.new.deliver(user)
end
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class UserMailer < ApplicationMailer
def comment_notification
mail(body: "")
end

def comment_notification_for(user)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a new mailer action that accepts an argument.

mail(body: "")
end
end