The challenge: be able to send out a custom email blast to all app members that they need to change their password by using the provided link.
I started with this link, which ends up being a pretty simple
User.all.each do |user| user.send_reset_password_instructions end
That seemed easy enough, until I tried to customize it. The
send_reset_password_instructions method is baked into the
recoverable.rb file of Devise (see where it lives) and -as is- only sends out the standard Devise Reset Your Password email template, which we are utilizing, so I couldn’t change it.
I started by backtracking what was happening in the
send_reset_password_instructions method and found that there was a token being generated with a
set_reset_password_token method. I also looked into the email template saw that there was a
token being passed along through the path helper.
I dug around a bit more and found this Stack Overflow post, which confirmed for me that I could use that
set_reset_password_token in some fashion. So I went back to that original Devise
.each loop and revised it to look like this:
Member.all.each do |member| token = member.set_reset_password_token MemberMailer.member_import_password_reset(member, token).deliver end
…but that got kicked back because the
set_reset_password_token method is protected.
I knew I was close, so I kept digging and pinged a Slack channel that always seems to come through in a pinch. Turns out there’s a
.send method that will bypass a method’s privacy, so I tweaked my logic to be this:
Member.all.each do |member| token = member.send(:set_reset_password_token) MemberMailer.member_import_password_reset(member, token).deliver end
With that loop I am able to send every member of the app a custom email, with a Devise reset token. When linked up properly with the custom email template it will push the member to the standard Devise Change Your Password page we have setup.