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

pass params to mailer preview #20608

Closed
wants to merge 1 commit into from
Closed

pass params to mailer preview #20608

wants to merge 1 commit into from

Conversation

equivalent
Copy link
Contributor

ability to pass Rails::MailersControlle params to ActionMailer::Preview instances (e.g. MyMailerPreview)

...sounds stupid but hear me out

Imagine you need create a feature where you need to preview different email types for different clients, which has some different states. you don't want to replicate my MyMailerPreview in several classes (MyMailerPreviewClientTypeA, MyMailerPreviewClientTypeB, ...) you want it to be dynamic:

class MyMailerPreview < ActionMailer::Preview 

   def good_news
     MyMailer.good_news(client)
   end

   def bad news
     MyMailer.bad_news(client)
   end

   private

     def client
        Client.find(params['client_id']) || Client.find_by email: 'default@client.test'
     end
end

@equivalent
Copy link
Contributor Author

@mszyndel
Copy link

👍 for getting this merged into Rails!

@equivalent
Copy link
Contributor Author

I've created alternative PR that may be better solution

@equivalent
Copy link
Contributor Author

meanwhile I've created patch-gem that is similar functionality if anyone is interested https://github.com/equivalent/mailer_preview_request_model (I'm using it in one application)

@thinkclay
Copy link

I found a more elegant way (I think) to handle this. TL;DR just use an initializer to extend ActionMailer::Preview from ActionController::Base

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants