Rails 中的对象版本控制,类似于 Papertrail,但有单独的表

发布于 2024-10-23 19:36:55 字数 865 浏览 9 评论 0原文

对于我当前正在进行的项目,我需要实现对象版本控制。不幸的是,我需要保留每个对象的完整历史记录,因此像 Papertrail 这样的单表解决方案很快就会变得难以管理。然而,Papertrail 有一些我喜欢的功能,但我无法在每个模型都有单独表的解决方案中找到这些功能(例如acts_as_versioned)。

  • 能够存储来自控制器和模型的元信息
  • 数据被序列化,因此模式更改不会修改版本表
  • 遍历版本的强大方法
  • 自动跟踪更改责任

还有一些 Papertrail 不具备的功能,这将是额外的好处

  • : -in 版本 diff 支持
  • 差异而不是完整版本

我目前正在考虑分叉 Papertrail 为每个模型使用单独的表,但如果有现有的解决方案,我希望节省精力。

更新: Vestal Versions 默认情况下使用单个表,但通过为每个模型提供自定义版本类并使用 ActiveRecord 的“set_table_name”方法,我能够为每个模型创建单独的表。 Vestal Versions 还内置了 diff 支持,尽管其界面不如 Papertrails 强大。它还缺乏协会的支持。

更新2: 由于 papertrail 似乎是一个更活跃的项目,我分叉了 gem 并添加了类似于 Vestal 版本的自定义类支持,现在允许为每个模型定义单独的表。我的分支在这里,但我希望它很快就会被拉到主项目存储库中。 https://github.com/benzittlau/paper_trail

For a project I'm currently working on I need to implement object versioning. Unfortunately I need to keep a full history of each object, so a single table solution like Papertrail would quickly become un-manageable. There are features of Papertrail which I like, however, which I haven't been able to find in a solution with individual tables for each model (such as acts_as_versioned).

  • Ability to store meta-information from both controller and model
  • Data is serialized so schema changes don't modify the version table
  • Powerful methods for traversing versions
  • Automatic tracking of change responsibility

There are also some features which Papertrail does not have which would be bonuses:

  • Built-in version diff support
  • Differential instead of full versions

I am currently considering forking Papertrail to use individual tables for each model, but would like to save that effort if there's an existing solution.

Update:
Vestal Versions by default uses a single table, but by providing a custom version class for each model and using the "set_table_name" method of ActiveRecord, I was able to create separate tables for each model. Vestal Versions also has built in diff support, though its interface isn't as powerful as Papertrails. It also lacks association support.

Update 2:
As papertrail seems to be a more active project I've forked the gem and added in custom class support similar to vestal versions which now allows for the ability to define separate tables per model. My fork is here, but I hope it will be pulled into the main project repository shortly.
https://github.com/benzittlau/paper_trail

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

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

发布评论

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

评论(2

最偏执的依靠 2024-10-30 19:36:55

您可以使用 :class_name 选项指定自定义版本子类:

class Post < ActiveRecord::Base
  has_paper_trail :class_name => 'PostVersion'
end

class PostVersion < Version
  # custom behaviour, e.g:
  self.table_name = :post_versions # in rails 2, use set_table_name
end

这允许您将每个模型的版本存储在单独的表中,如果您要创建大量版本,这将非常有用。

You can specify custom version subclasses with the :class_name option:

class Post < ActiveRecord::Base
  has_paper_trail :class_name => 'PostVersion'
end

class PostVersion < Version
  # custom behaviour, e.g:
  self.table_name = :post_versions # in rails 2, use set_table_name
end

This allows you to store each model's versions in a separate table, which is useful if you have a lot of versions being created.

手长情犹 2024-10-30 19:36:55

我一直在使用 Vestal Versions,这是一个使用备忘录模式创建历史表的 Rails gem/插件,并且它在 after_save 和 after_destroy 回调中保存属性的差异。

它还记录更改的时间并增加版本号,以便您可以回滚到某个版本或某个日期或时间存在的版本。

然后我可以像这样拉出一个物体:

@user = User.last
@user.versions.last.changes #=> {:name => ['Robert','Bob']}

我是一个非常大的粉丝。

I've been using Vestal Versions, a rails gem/plugin that uses the memento pattern to create a history table, and it saves a diff of attributes in after_save and after_destroy callbacks.

It also records when it was changed and increments a version number so you can rollback to a certain version or a version present at a certain date or time.

I can then pull up an object like so:

@user = User.last
@user.versions.last.changes #=> {:name => ['Robert','Bob']}

I'm a pretty big fan.

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