Last active
March 11, 2023 14:39
-
-
Save typeoneerror/a5e632f0ee97a8b5987a to your computer and use it in GitHub Desktop.
Setting up Devise with a custom mailer to send different confirmation emails based on devise scope.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# doki_core/mailer/confirmation_instructions_tenants.html.erb | |
I get sent to Tenants when requesting confirmation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# doki_core/mailer/confirmation_instructions_users.html.erb | |
I get sent to Users when requesting confirmation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/devise.rb | |
Devise.setup do |config| | |
config.mailer = 'DokiCore::DeviseMailer' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# doki_core/app/mailers/devise_mailer.rb | |
module DokiCore | |
class DeviseMailer < Devise::Mailer | |
def confirmation_instructions(record, token, opts={}) | |
@token = token | |
scope_name = Devise::Mapping.find_scope!(record) | |
devise_mapping = Devise.mappings[scope_name] | |
template = "confirmation_instructions_#{devise_mapping.scoped_path}".to_sym | |
# set the template path to whereever you want templates to live. | |
# defaults to [#{devise_mapping}/mailer, devise/mailer] but in this case | |
# all the confirmation templates are in my main engine's mailer directory | |
opts = { | |
template_path: 'doki_core/mailer' | |
}.merge(opts) | |
devise_mail(record, template, opts) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Super simplfied routing. Actual devise mounts are quite a bit | |
# more complex and :users are scoped to a subdomain. | |
DokiCore::Engine.routes.draw do | |
# User devise routes | |
devise_for :users, class_name: 'DokiCore::User' | |
# Tenant devise routes | |
devise_for :tenants, class_name: 'DokiCore::Tenant', | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment