Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correct syntax to call a mailer with interpolation

I have a method to dynamically call a mailer that look like this :

def mail
  application = mail[:application].capitalize
  class_template = mail[:template].split('/').[0]+ 'Mailer'
  template = mail[:template].split('/').second
  email_params = mail[:params].values.map(&:inspect).join(', ')

  "#{application}::#{class_template}.#{template}(#{email_params}).deliver_now"
   # Here I have something like : "Application::TemplatetMailer.test_name(\"John\", \"Doe\").deliver_now"
end 

How can I have something like :

Application::TemplatetMailer.test(\"John\", \"Doe\").deliver_now

instead of

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"Application::TemplatetMailer.test(\"John\", \"Doe\").deliver_now"

>Solution :

You can look up arbitrary constants with constantize:

mailer = [ application, class_template ].join('::').constantize

mailer.send(template, *email_params).deliver_now

Be extremely careful with what access you allow to end users. Do not expose this in a way that allows them to make arbitrary method calls on arbitrary classes. Having an allow-list of classes and methods is way safer.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading