I use devise on a small website I run and it's absolutely fantastic.
Thankfully my site has been growing and I'm looking at launching
similar sites that do the same thing just in different niches (think
kind of like how stack exchange works, but for local business
listings.)
I'm in the process of converting the system to support multiple sites
that are based off of the accessed hostname, and user accounts are
shared across all sites and the major problem I'm having is figuring
out how to get devise mailers to have the context of which site the
user is on when sending emails.
With all of my custom mailers I can simply pass a site_id to the
mailer method so it can look it up and brand the email based on that
site the user is access, but I am not quite sure how to make devise do
this. Basically, if someone tries to reset their password on
site1.com, that sites context needs to make it down to the email
layout so it can use the right logo/name.
Has anyone tried to do anything like this and can offer suggestions?
Thanks in advance!
Here is the devise mailer source code:
https://github.com/plataformatec/devise/blob/master/app/mailers/devise/mailer.rb
And here are the helpers used in it:
https://github.com/plataformatec/devise/blob/master/lib/devise/mailers/helpers.rb
I also think we have more information on the wiki regarding
configuring mailers.
Ultimately this may be something where I need to change my
architecture so that if accounts are shared, users get a generic email
from a parent site, but if I can get away with not doing that I'd be
very happy.
Thanks for such a quick reply and a great piece of code.
On Nov 25, 2:48 am, José Valim <jose.va...@gmail.com> wrote:
> There is a config.mailer in your Devise initializer. So what you can
> do is to inherit from the Devise::Mailer and change Devise to use your
> new mailer. Inside the new mailer you will have access to the user
> object and then you can do whatever you want.
>
> Here is the devise mailer source code:
>
> https://github.com/plataformatec/devise/blob/master/app/mailers/devis...
>
> And here are the helpers used in it:
>
> https://github.com/plataformatec/devise/blob/master/lib/devise/mailer...
1) Add a non-persisted attribute (in my case contextual_site_id) to my
User model.
2) Override create actions in confirmation and password controllers,
add the contextual data to the params[resource_name] hash, and call
super
3) Override send_X_instructions class methods on my User model and set
the contextual_site_id attribute from the parameter added to the
attributes hash
4) Create a custom UserMailer that sets the necessary template
variables from the user object's contextual_site_id attribute.
This was the cleanest way I could find to do what I needed to do and I
don't like it at all which makes me think there may be a need for some
sort of hooking system built into devise to do these sorts of things.
I really have no idea how far my use case is from the norm.
Hopefully this will help anyone else having this issue.