实现 Gmail 风格“撤消”的最佳方式是什么? 在 Rails 中?

发布于 2024-07-06 11:34:31 字数 647 浏览 10 评论 0原文

我认为在销毁记录时使用“撤消”方法(如gmail)非常重要,而不是显示一个恼人的弹出窗口,“你确定吗?”。

我实现此方法的方法是在模型中添加一个“deleted_at”时间戳列,当调用 destroy 方法时,该列将被加上时间戳。 要

def destroy
  @foo = Foo.find(params[:id])
  @foo.update_attribute(:deleted_at, Time.now)
  ...
end

恢复/撤消,我只需将同一列设置为 nil

def revert
  @foo = Foo.find(params[:id])
  @foo.update_attribute(:deleted_at, nil)
  ...
end

当我调用 find 方法时,我只需要添加一个条件来过滤掉“已删除”的 foos 。 也许设置一个 cron 或后台任务在一段时间后真正销毁“已删除”的 foos。

对我有用并且易于实现,但我很好奇是否有更好的方法来实现此功能? 也许有一个插件或 gem 提供了我不知道的功能?

I think it important to have an "undo" method ala gmail when destroying records instead of displaying an annoying popup that says, "Are you sure?".

The way that I've implemented this is to have a "deleted_at" timestamp column in the model which gets timestamped when destroy method is called

def destroy
  @foo = Foo.find(params[:id])
  @foo.update_attribute(:deleted_at, Time.now)
  ...
end

To revert/undo I'll just set the same column to nil

def revert
  @foo = Foo.find(params[:id])
  @foo.update_attribute(:deleted_at, nil)
  ...
end

I'll just have to add a condition to filter off "deleted" foos when I call the find method. Perhaps set a cron or background task to really destroy "deleted" foos after some time.

Works for me and easy to implement but I'm curious as to if there's a better way to implement this feature? Maybe there's a plugin or gem that provides this that I don't know about?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱格式化 2024-07-13 11:34:31

确实有一些插件可以在 敏捷 Web 开发 中找到。

以下是似乎与您的描述相符的插件的链接和摘要:

  1. 充当偏执狂:让您的Active Records“偏执”。 删除它们并不会删除该行,而是设置一个deleted_at字段。 查找已重载以跳过已删除的记录。
  2. 充当软删除:提供软删除 ActiveRecord 模型的功能。

There are indeed some plugins that can be found at Agile Web Development.

Here are the links and summaries for the plugins which seem to match your description:

  1. Acts as Paranoid: Make your Active Records "paranoid." Deleting them does not delete the row, but set a deleted_at field. Find is overloaded to skip deleted records.
  2. Acts as soft deletable: Provides the ability to soft delete ActiveRecord models.
一个人的旅程 2024-07-13 11:34:31

有一个文件这里似乎可以做你的事情需要,但我个人认为必须有一些东西可以自动过滤掉已删除的记录,除非您特别包含它们。 这样,除非您包含重新包含它们的参数或命名范围,否则它们实际上会显示为已删除。

不幸的是,我还没有写一篇,而且业余时间有限,但应该不会那么难,不是吗?

There is a file here that seems to do what you're requiring, but personally I think there must be something out there that automatically filters out deleted records unless you specifically include them. That way they really would appear deleted unless you include a parameter or a named scope that re-includes them.

Unfortunately, I haven't written one and spare time is limited, but it shouldn't be that hard, should it?

牵强ㄟ 2024-07-13 11:34:31

您可以将已删除的项目移动到单独的集合(或表,或其他任何内容)中 - 然后在原始列表中查找的任何内容都会发现它已被删除,并且您可以在方便时处理新列表。

You can move the deleted items into a separate collection (or table, or whatever) - then anything that looks in the original list will see that it's been deleted, and you can deal with the new list when it's convenient.

ゃ懵逼小萝莉 2024-07-13 11:34:31

PaperTrail 很好地做到了这一点。 上面有一个很好的Railscast,虽然现在有点旧了。 我知道这个问题很久以前就被问过,但我出于好奇而偶然发现了它,并认为我应该参考一种很好的最新方法来做到这一点。

PaperTrail does this nicely. There is a good Railscast on it, although it is a little old now. I know this question was asked quite a while ago but I stumbled upon it out of curiosity and thought I should reference a nice up to date way of doing this.

可是我不能没有你 2024-07-13 11:34:31

责任链模式

class Action
{
  Perform(context);
  Undo(context);
}

responsibility chain pattern

class Action
{
  Perform(context);
  Undo(context);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文