如何与QUOT;软删除"与用户设计 [英] How to "soft delete" user with Devise

查看:100
本文介绍了如何与QUOT;软删除"与用户设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在Rails项目中使用设计用于用户注册/验证。当用户想取消他们的帐户,用户对象被销毁,这让在不希望的状态,我的申请。

I currently use Devise for user registration/authentication in a Rails project. When a user wants to cancel their account, the user object is destroyed, which leaves my application in an undesired state.

什么是实现软删除最简单的方法,即仅删除个人数据和标记用户为删除?我仍想保留所有记录的关联。

What is the easiest way to implement a "soft delete", i.e. only removing personal data and marking the user as deleted? I still want to keep all record associations.

我想我将不得不先介绍一个新的删除栏供用户使用。但后来我坚持在用户的个人资料检视此默认code:

I assume I will have to first introduce a new "deleted" column for users. But then I am stuck with this default code in the user's profile view:

<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>

我在哪里可以找到:删除的方法?我应该如何覆盖默认的设计方法是什么?

Where can I find the :delete method? How should I overwrite the default Devise methods?

推荐答案

我可以劝重写摧毁方法对您的用户模型简单地做 update_attribute (:deleted_at,Time.current)。(而不是实际破坏),但是从标准API这种偏离可能成为未来的负担,所以这里的如何修改控制器

I could advise overriding destroy method on your User model to simply do update_attribute(:deleted_at, Time.current) (instead of actually destroying), but this deviation from standard API could become burdensome in the future, so here's how to modify the controller.

设计有一堆的默认控制器开箱。定制它们的最好方法是创建您自己的控制器继承相应的色器件控制器。在这种情况下,我们正在谈论设计:: RegistrationsController - 这是很容易通过查看源认可。因此,创建一个新的控制器。

Devise has a bunch of default controllers out of the box. The best way to customize them is to create your own controller inheriting the corresponding devise controller. In this case we are talking about Devise::RegistrationsController — which is easily recognized by looking at source. So create a new controller.

class RegistrationsController < Devise::RegistrationsController
end

现在,我们有我们自己的控制器,完全继承了所有的色器件提供的逻辑。下一步就是告诉想出用它代替默认之一。在你的路线,你有 devise_for 行。它应改为包括注册控制器。

Now we have our own controller fully inheriting all the devise-provided logic. Next step is to tell devise to use it instead of the default one. In your routes you have devise_for line. It should be changed to include registrations controller.

devise_for :users, :controllers => { :registrations => 'registrations' } 

这似乎很奇怪,但因为默认情况下它的色器件/注册,不是简单的'注册'。

This seems strange, but it makes sense because by default it's 'devise/registrations', not simply 'registrations'.

下一步是覆盖注册控制器中的摧毁的行动。当您使用 registration_path(:用户):方法=&GT; :删除 - 这就是它的链接。为了摧毁注册控制器的作用。

Next step is to override the destroy action in registrations controller. When you use registration_path(:user), :method => :delete — that's where it links. To destroy action of registrations controller.

目前制定执行以下步骤。

Currently devise does the following.

def destroy
  resource.destroy
  set_flash_message :notice, :destroyed
  sign_out_and_redirect(self.resource)
end

我们可以使用它代替code。首先让我们来添加新方法用户模式。

We can instead use this code. First let's add new method to User model.

class User < ActiveRecord::Base
  def soft_delete
    # assuming you have deleted_at column added already
    update_attribute(:deleted_at, Time.current)
  end
end

# Use this for Devise 2.1.0 and newer versions
class RegistrationsController < Devise::RegistrationsController

  def destroy
    resource.soft_delete
    Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
    set_flash_message :notice, :destroyed if is_navigational_format?
    respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
  end
end

# Use this for older Devise versions
class RegistrationsController < Devise::RegistrationsController
  def destroy
    resource.soft_delete
    set_flash_message :notice, :destroyed
    sign_out_and_redirect(resource)
  end
end

现在,你应该所有设置。使用范围来过滤掉删除的用户。

Now you should be all set. Use scopes to filter out deleted users.

这篇关于如何与QUOT;软删除&QUOT;与用户设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆