Dylan Richards
Dylan Richards

Reputation: 786

Render email text conditionally based on who sent it

Users can accept a project, or an admin user can do it on their behalf. Regardless of the sender, an email is sent. I would like the contents of the email to be different when an admin triggers its' sending.

There's a current_user method in ApplicationController. When current_user.admin? evaluates to true, I'd like to do something else.

How can I access current_user from either inside the mailer's view template or from the *_mailer.rb itself?

I'm also open to working with controller/action names. For example, if controller_name == "Admin". It'll get us there, but this method is unavailable in the mailer as well.

Upvotes: 0

Views: 69

Answers (1)

Wes Foster
Wes Foster

Reputation: 8910

Just pass the current_user to the mailer when you are creating a new delivery:

YourMailer.conditional_creation_email(current_user).deliver_now

In your_mailer.rb

def conditional_creation_email(sender)
  if sender.admin?
    # do...
    mail(to: '@gmail.com', subject: 'New  Order')
  end
end

Upvotes: 1

Related Questions