Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing devise_invitable mailer subject

on default, the subject for invitation mail is

  mailer:
      invitation_instructions:
        subject: 'Invitation instructions'

I'd like to change it to

subject: '%{invited_by} has invited you!'

but this requires to have invited_by variable accessible to the translate method for i18n.

How can I have this variable accessible/declared without changing default behavior too much?

like image 565
Nick Ginanto Avatar asked Jun 20 '13 06:06

Nick Ginanto


1 Answers

Devise default mailer won't work for you, but it's quite easy to set up a custom mailer and define there your own subject_for helper:

# in config/initializers/devise.rb:
...
config.mailer = "CustomDeviseMailer"
...
# in app/mailers/custom_devise_mailer.rb:

class CustomDeviseMailer < Devise::Mailer
  protected

  def subject_for(key)
    return super  unless key.to_s == 'invitation_instructions'

    I18n.t('devise.mailer.invitation_instructions.subject', 
      :invited_by => resource.invited_by.try(:full_name) || 'Someone')
  end
end
like image 182
Alexis Avatar answered Nov 14 '22 00:11

Alexis