diff --git a/lib/noticed/delivery_methods/email.rb b/lib/noticed/delivery_methods/email.rb index a62d3e3e..95e6f9d7 100644 --- a/lib/noticed/delivery_methods/email.rb +++ b/lib/noticed/delivery_methods/email.rb @@ -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 @@ -42,7 +42,7 @@ def mailer_method end end - def format + def params params = if (method = options[:format]) notification.send(method) else @@ -50,6 +50,28 @@ def format end params.merge(recipient: recipient, record: record) end + + def args + 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 diff --git a/test/delivery_methods/email_test.rb b/test/delivery_methods/email_test.rb index e4b73a34..9d5fe3f2 100644 --- a/test/delivery_methods/email_test.rb +++ b/test/delivery_methods/email_test.rb @@ -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) @@ -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 diff --git a/test/dummy/app/mailers/user_mailer.rb b/test/dummy/app/mailers/user_mailer.rb index cba5fb63..bf1372ac 100644 --- a/test/dummy/app/mailers/user_mailer.rb +++ b/test/dummy/app/mailers/user_mailer.rb @@ -2,4 +2,12 @@ class UserMailer < ApplicationMailer def comment_notification mail(body: "") end + + def comment_notification_for(user) + mail(body: "") + end + + def comment_notification_with(user:) + mail(body: "") + end end