Skip to content

Instantly share code, notes, and snippets.

@thexodus
Forked from willywg/UserMailer.rb
Created October 3, 2012 08:30
Show Gist options
  • Save thexodus/3825804 to your computer and use it in GitHub Desktop.
Save thexodus/3825804 to your computer and use it in GitHub Desktop.
Mass password reset and email notification for Devise 1.4.2 with Rails 3.x
# Assign an new random password for each user and send them a email notification
# path: lib/tasks/devise.rake
namespace :devise do
desc 'Mass password reset and send email instructions'
task :mass_password_reset => :environment do
model = ENV['MODEL'] || 'User'
begin
model_mailer = "#{model}Mailer".constantize
model = model.constantize
model.find_each(:conditions => 'id > 70720') do |record|
# Assign a random password
random_password = User.send(:generate_token, 'encrypted_password').slice(0, 8)
record.password = random_password
record.save
# Send change notification (Ensure you have created #{model}Mailer e.g. UserMailer)
model_mailer.password_reset(record, random_password).deliver
end
rescue Exception => e
puts "Error: #{e.message}"
end
end
end
# Send password reset notification
# path: app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default :from => "no-reply@example.com"
def password_reset(user, password)
@user = user
@password = password
mail(:to => user.email,
:subject => 'Password Reset Notification')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment