百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
D

draftsman

> 后端框架
开源

一个 Ruby gem,可让您创建数据库记录的草稿版本。

171 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

一个 Ruby gem,可让您创建数据库记录的草稿版本。

Project status

Drfatsman is looking for a new Steward

Draftsman v0.8.0.dev

Draftsman is a Ruby gem that lets you create draft versions of your database records. If you're developing a system in need of simple drafts or a publishing approval queue, then Draftsman just might be what you need.

  • The largest risk at this time is functionality that assists with publishing or reverting dependencies through associations (for example, "publishing" a child also publishes its parent if it's a new item). We'll be putting this functionality through its paces in the coming months.
  • The RSpec tests are lacking in some areas, so I will be adding to those over time as well. (Unfortunately, this gem was not developed with TDD best practices because it was lifted from PaperTrail and modified from there.)

This gem is inspired by the [Kentouzu][1] gem, which is based heavily on [PaperTrail][2]. In fact, much of the structure for this gem emulates PaperTrail (because it works beautifully). You should definitely check out PaperTrail and its source: it's a nice clean example of a gem that hooks into Rails and Sinatra.

Features

  • Provides API for storing drafts of creations, updates, and destroys.
  • A max of one draft per record (via belongs_to association).
  • Does not store drafts for updates that don't change anything.
  • Allows you to specify attributes (by inclusion or exclusion) that must change for a draft to be stored.
  • Ability to query drafts based on the current drafted item, or query all drafts polymorphically on the drafts table.
  • publish! and revert! methods for drafts also handle any dependent drafts so you don't end up with orphaned records.
  • Allows you to get at every draft, even if the schema has since changed.
  • Automatically records who was responsible via your controller. Draftsman calls current_user by default if it exists, but you can have it call any method you like.
  • Allows you to store arbitrary model-level metadata with each draft (useful for filtering).
  • Allows you to store arbitrary controller-level information with each draft (e.g., remote IP, current account ID).
  • Only saves drafts when you explicitly tell it to via instance methods like save_draft and draft_destruction.
  • Stores everything in a single database table by default (generates migration for you), or you can use separate tables for separate models.
  • Supports custom draft classes so different models' drafts can have different behavior.
  • Supports custom name for draft association.
  • Supports before, after, and around callbacks on each draft persistence method, such as before_save_draft or around_draft_destruction.
  • Threadsafe.

Compatibility

Compatible with ActiveRecord 4 and 5.

Works well with Rails, Sinatra, or any other application that depends on ActiveRecord.

Installation

Rails 4 and 5

Add Draftsman to your Gemfile.

ruby
gem 'draftsman', '~> 0.7.1'

Or if you want to grab the latest from master:

ruby
gem 'draftsman', github: 'jmfederico/draftsman'

Generate a migration which will add a drafts table to your database.

$ rails g draftsman:install

You can pass zero or any combination of these options to the generator:

$ rails g draftsman:install --skip-initializer  # Skip generation of the boilerplate initializer at
                                                # `config/initializers/draftsman.rb`.

$ rails g draftsman:install --with-changes      # Store changeset (diff) with each draft.

$ rails g draftsman:install --with-pg-json      # Use PostgreSQL JSON data type for serialized data.

Run the migration(s).

$ rake db:migrate

Add draft_id, published_at, and trashed_at attributes to the models you want to have drafts on. trashed_at is optional if you don't want to store drafts for destroys.

$ rails g migration add_drafts_to_widgets draft_id:integer published_at:timestamp trashed_at:timestamp
$ rake db:migrate

Add has_drafts to the models you want to have drafts on.

Lastly, if your controllers have a current_user method, you can easily track who is responsible for changes by adding a controller filter.

ruby
class ApplicationController
  before_action :set_draftsman_whodunnit
end

Sinatra

In order to configure Draftsman for usage with [Sinatra][5], your Sinatra app must be using ActiveRecord 4 or greater. It is also recommended to use the [Sinatra ActiveRecord Extension][6] or something similar for managing your application's ActiveRecord connection in a manner similar to the way Rails does. If using the aforementioned Sinatra ActiveRecord Extension, steps for setting up your app with Draftsman will look something like this:

Add Draftsman to your Gemfile.

ruby
gem 'draftsman', github: 'jmfederico/draftsman'

Generate a migration to add a drafts table to your database.

$ rake db:create_migration NAME=create_drafts

Copy contents of [create_drafts.rb][7] into the create_drafts migration that was generated into your db/migrate directory.

Run the migration(s).

$ rake db:migrate

Add draft_id, published_at, and trashed_at attributes to the models you want to have drafts on. (trashed_at is optional if you don't want to store drafts for destroys.)

Add has_drafts to the models you want to have drafts on.

Draftsman provides a helper extension that acts similarly to the controller mixin it provides for Rails applications.

It will set Draftsman::Draft#whodunnit to whatever is returned by a method named user_for_draftsman, which you can define inside your Sinatra application. (By default, it attempts to invoke a method named current_user.)

If you're using the modular [Sinatra::Base][8] style of application, you will need to register the extension:

ruby
# my_app.rb
require 'sinatra/base'

class MyApp  Given a version number MAJOR.MINOR.PATCH, increment the:
>
> 1.  MAJOR version when you make incompatible API changes,
> 2.  MINOR version when you add functionality in a backwards-compatible manner, and
> 3.  PATCH version when you make backwards-compatible bug fixes.

## Contributing

If you feel like you can add something useful to Draftsman, then don't hesitate
to contribute! To make sure your fix/feature has a high chance of being
included, please do the following:

1.  Fork the repo.

2.  Run `bundle install`.

3.  Run `RAILS_ENV=test bundle exec rake -f spec/dummy/Rakefile db:schema:load`
    to load test database schema.

4.  Add at least one test for your change. Only refactoring and documentation
    changes require no new tests. If you are adding functionality or fixing a
    bug, you need a test!

5.  Make all tests pass by running `rake`.

6.  Push to your fork and submit a pull request.

I can't guarantee that I will accept the change, but if I don't, I will be sure
to let you know why.

Here are some things that will increase the chance that your pull request is
accepted, taken straight from the Ruby on Rails guide:

-  Use Rails idioms
-  Include tests that fail without your code, and pass with it
-  Update the documentation, guides, or whatever is affected by your
   contribution

This gem is a work in progress. I am adding specs as I need features in my
application. Please add missing ones as you work on features or find bugs!

## License

Copyright 2013-2016 Minimal Orange, LLC.

Draftsman is released under the [MIT License][9].

[1]: https://github.com/seaneshbaugh/kentouzu
[2]: https://github.com/airblade/paper_trail
[4]: http://railscasts.com/episodes/416-form-objects
[5]: http://www.sinatrarb.com/
[6]: https://github.com/janko-m/sinatra-activerecord
[7]: https://raw.github.com/jmfederico/draftsman/master/lib/generators/draftsman/templates/create_drafts.rb
[8]: http://www.sinatrarb.com/intro.html#Modular%20vs.%20Classic%20Style
[9]: http://www.opensource.org/licenses/MIT
[10]: http://semver.org/

Issues· 25 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubydraft-versionsrailsrubyruby-gem

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架