9

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?

Alexis
  • 3,817
  • 1
  • 21
  • 33
Nick Ginanto
  • 26,414
  • 39
  • 123
  • 214
  • sorry to disturb, but it looks like you don't think that my answer worth your bounty and I'm a little bit curious about it. Do you think the answer is wrong, or it's not detailed enough, or something? – Alexis Jun 30 '13 at 06:22
  • Actually, I upvoted it, and I am now checking to see that it works as intended. – Nick Ginanto Jun 30 '13 at 06:32

1 Answers1

33

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
kikito
  • 48,656
  • 29
  • 134
  • 183
Alexis
  • 3,817
  • 1
  • 21
  • 33
  • 1
    at some point they switched it from a string to a symbol so you may need to use return super unless key == :invitation_instructions – schpet Jan 09 '14 at 03:09
  • Also, I think this answer deserves to be marked as "correct", @NickGinanto – kikito Mar 21 '14 at 12:16