Skip to content

Instantly share code, notes, and snippets.

@salkar
Last active August 29, 2015 14:03
Show Gist options
  • Save salkar/34f793488d79a0c3db1c to your computer and use it in GitHub Desktop.
Save salkar/34f793488d79a0c3db1c to your computer and use it in GitHub Desktop.
Rails: Devise user soft deletion with custom error on sign in.
1. Add deleted_at column to User model.
2. Add to User model:
def soft_delete
update_attribute(:deleted_at, Time.current)
end
def active_for_authentication?
super && !self.deleted_at
end
def inactive_message
self.deleted_at ? :deleted : super
end
3. Create registration controller and overwrite destroy:
class RegistrationsController < Devise::RegistrationsController
def destroy
resource.soft_delete #owerwrited from resource.destroy
Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
set_flash_message :notice, :destroyed if is_flashing_format?
yield resource if block_given?
respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
end
end
4. Add route:
devise_for :users, :controllers => { :registrations => 'registrations' }
5. Add error message to devise.en.yml:
en:
devise:
failure:
deleted: "Your account is marked as deleted. You can recover it."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment