2
votes

I'm using:

gem 'rails', '3.2.2' 
gem 'devise',           '~> 2.0.0'
gem 'devise_invitable', '~> 1.0.0'

The devise_invitable plugin allows my members to invite other members.

My controller looks like it uses the class method invite!:

def create
   #params[resource_name] ==> {"email" => "[email protected]"}
   # current_inviter ==> same as current_artist
   # resource_class Artist

   self.resource = resource_class.invite!(params[resource_name], current_inviter)
   .....

I would like to be notified everytime an invitation is made via an email bcc, but I don't know where to inject this. I tried adding bcc to the hash that is passed to invite! method but that seems to be only to set the model's attributes so I get error:

b = Artist.invite!({:email => '[email protected]', :name=>'test name', :bcc =>    '[email protected]'},t)

 ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: bcc

How can I get a secret copy of every invitation that is sent out?

1

1 Answers

0
votes

I managed to get this functionality by overriding DeviseInvitable::Mailer#invitation_instructions. This is the method responsible for sending out the mail once an invitation is generated. This is probably not the cleanest way to do this but it does work.

In config/initializers/devise_invitable.rb:

module DeviseInvitable
  module Mailer
    def invitation_instructions(record, token, opts = {})
      @token = token
      devise_mail record, :invitation_instructions, opts.merge(bcc: '[email protected]')
    end
  end
end

This is referencing the implementation in my current version of devise_invitable (1.3.0) but you can easily find the equivalent for yours by looking at the source code in Github.

Remember to restart your application for the changes to take effect. It's probably also a good idea to take it one step further and set the recipient address in config and gracefully degrade if it is nil.