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 all 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
28 changes: 25 additions & 3 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 @@ -42,14 +42,36 @@ def mailer_method
end
end

def format
def params
params = if (method = options[:format])
notification.send(method)
else
notification.params
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[:args])

notification.send(option)
end

def named_args
return unless (option = options[:named_args])

notification.send(option)
end

def composed_mailer
if options[:args]
mailer.with(params).public_send(mailer_method.to_sym, args)
elsif options[:named_args]
mailer.with(params).public_send(mailer_method.to_sym, **named_args)
else
mailer.with(params).public_send(mailer_method.to_sym)
end
end
end
end
end
28 changes: 28 additions & 0 deletions test/delivery_methods/email_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ 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, args: :email_args

def email_args
recipient
end
end

class EmailDeliveryWithNamedArguments < Noticed::Base
deliver_by :email, mailer: "UserMailer", method: :comment_notification_with, named_args: :email_named_args

def email_named_args
{user: recipient}
end
end

class EmailTest < ActiveSupport::TestCase
setup do
@user = users(:one)
Expand Down Expand Up @@ -43,4 +59,16 @@ 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

test "delivers an email when passing a named argument" do
assert_emails 1 do
EmailDeliveryWithNamedArguments.new.deliver(user)
end
end
end
8 changes: 8 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,12 @@ 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

def comment_notification_with(user:)
mail(body: "")
end
end
Loading