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

Unable to pass arguments to email methods #299

Closed
tomrossi7 opened this issue Jul 27, 2023 · 6 comments · Fixed by #313
Closed

Unable to pass arguments to email methods #299

tomrossi7 opened this issue Jul 27, 2023 · 6 comments · Fixed by #313
Assignees
Labels
enhancement New feature or request

Comments

@tomrossi7
Copy link

tomrossi7 commented Jul 27, 2023

I'm not sure if this follows the opinion of Noticed, but some mailer methods accept both params and arguments. I believe the current email_format method only really sets the params. So for example, Noticed can't send an email notification to a mailer method like this:

UserMailer.with({ recipient: User.first }).new_comment(comment)

Is this something Noticed should support? If so, I can take a crack at a PR.

@excid3
Copy link
Owner

excid3 commented Jul 27, 2023

I'm not sure. How do you envision the user defining those it in their Notification object?

@tomrossi7
Copy link
Author

tomrossi7 commented Jul 27, 2023

I set up my own custom delivery method and this is how I specified the params and args. I kinda like email_params better than email_format since its more explicit.

  def email_args
    params[:comment]
  end

  def email_params
    { recipient: params[:recipient] }
  end

The recipient is merged so it isn't necessary, but I'm just showing how it would work if you did have other params to pass.

@excid3
Copy link
Owner

excid3 commented Jul 27, 2023

So a complete example might look like this:

class CommentNotification < Noticed::Base
  delivers_by :email, mailer: "UserMailer", params: :params_for_email, arguments: :args_for_email

  # Should return an array of arguments 
  def arguments_for_email
    [comment, post]
  end

  def params_for_email
    {}
  end
end

Seems fine to me. 👍

For backwards compatibility, we'd need to support format: but add a deprecation warning if used.

@excid3 excid3 added the enhancement New feature or request label Jul 27, 2023
@tomrossi7
Copy link
Author

Yep! I need to make sure it works with named arguments as well, but will put together a PR for you to check out when I get a chance!

@excid3
Copy link
Owner

excid3 commented Jul 27, 2023

While you're in there, would you mind changing send for the mailer method to public_send? We shouldn't be calling any private methods (or allowing that). 🙃

@tomrossi7
Copy link
Author

You bet! Thanks @excid3 for putting this code together!

@excid3 excid3 linked a pull request Jan 15, 2024 that will close this issue
Merged
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants