Skip to content

Instantly share code, notes, and snippets.

@typeoneerror
Last active March 11, 2023 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typeoneerror/a5e632f0ee97a8b5987a to your computer and use it in GitHub Desktop.
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.
# doki_core/mailer/confirmation_instructions_tenants.html.erb
I get sent to Tenants when requesting confirmation.
# doki_core/mailer/confirmation_instructions_users.html.erb
I get sent to Users when requesting confirmation.
# config/initializers/devise.rb
Devise.setup do |config|
config.mailer = 'DokiCore::DeviseMailer'
end
# 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
# 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