Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom variables in Devise reset password instructions?

I need to be able to customise the rails devise mailer view for reset password instructions.

for this I need to do two things.

  1. Specify a custom URL for the link, so that its a host/domain based on a certain business logic. This host and domain comes from the URL in the browser, i.e. the request object, when the user clicks forgot password. So I do not have the request object in delayed_job to process it as I need, hence I need to be able to do this at some point in the delayed_job that is sending the email.

  2. Pass custom variables to the mailer view, so that I can add in various other logic for the view, hiding and showing bits as I need.

Can anyone help? I can see that you can generate the mailer views for devise, but I need to be able pass over various items to it also. Do I need to somehow override the functions myself in my User model and password controller for example?

like image 625
James Sheil Avatar asked Jul 18 '12 13:07

James Sheil


3 Answers

Overriding the whole controller method and adding param in send_reset_password_instructions opts parameters will fix it.

@resource.send_reset_password_instructions(
    email: @email,
    provider: 'email',
    redirect_url: @redirect_url,
    client_config: params[:config_name],
    parameter_passed: params[:parameter_passed],
  )

You can access the param in the view as message['parameter_passed']

like image 72
Renad Avatar answered Sep 22 '22 00:09

Renad


so, after much ado and searching and hacking around with stuff... this is just not possible. so I ended up writing my own mailer and bypassing the devise reset password methods in the controllers, to generate my own reset token, set my variables I needed, called my usermailer.... and embedded the devise url in my mail to get it back calling devise once the password reset link was clicked, and all was fine then....

I hated having to rewrite the logic, but in the end its the quickest and cleanest solution.

One approach that nearly worked, was using a non activerecord attribute on my user model to store the bits I needed and "hacking" that into the @resource in the devise view, but it was causing some grief in devise doing so, as a result, I went with the option above...

like image 34
James Sheil Avatar answered Sep 25 '22 00:09

James Sheil


I needed to add a source to be included into the reset password view, here's what I implemented:

class User < ActiveRecord::Base
  prepend ResetPasswordWithSource

  devise :recoverable

  ....
end

module User::ResetPasswordWithSource
  def send_reset_password_instructions(source=nil)
    @source = source
    super()
  end

  def send_devise_notification(notification, *args)
    args.last.merge!({ source: @source })
    super
  end
end

From here you can just call user.send_reset_password_instructions('special_source')

And can access in views via @options[:source] = 'special_source'

like image 21
raycchan Avatar answered Sep 24 '22 00:09

raycchan