BenMorganIO
BenMorganIO

Reputation: 2046

How to List all Mailers in a Rails App

Say you have some mailers, but you want a dynamic list of them, how would you go about doing this?

The list is of all the mailer classes within a Rails application that inherit from ActionMailer::Base.

Upvotes: 2

Views: 1337

Answers (1)

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

> ActionMailer::Base.descendants
=> [Devise::Mailer]
> Dir['app/mailers/*.rb'].each {|f| require File.basename(f, '.rb')}
> ActionMailer::Base.descendants
=> [Devise::Mailer, InternalMailer, MailingListMailer, MessageMailer, UserMailer]

There may well be better ways. Note that if your mailers are somewhere else (or in nested directories) you'd need to modify the Dir glob above.

Upvotes: 6

Related Questions