一个 Ruby gem,可让您创建数据库记录的草稿版本。
Drfatsman is looking for a new Steward
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.
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.
belongs_to association).drafts table.publish! and revert! methods for drafts also handle any dependent drafts
so you don't end up with orphaned records.current_user by default if it exists, but you can have it call any
method you like.save_draft and draft_destruction.draft association.before, after, and around callbacks on each draft persistence
method, such as before_save_draft or around_draft_destruction.Compatible with ActiveRecord 4 and 5.
Works well with Rails, Sinatra, or any other application that depends on ActiveRecord.
Add Draftsman to your Gemfile.
gem 'draftsman', '~> 0.7.1'Or if you want to grab the latest from master:
gem 'draftsman', github: 'jmfederico/draftsman'Generate a migration which will add a drafts table to your database.
$ rails g draftsman:installYou 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:migrateAdd 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:migrateAdd 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.
class ApplicationController
before_action :set_draftsman_whodunnit
endIn 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.
gem 'draftsman', github: 'jmfederico/draftsman'Generate a migration to add a drafts table to your database.
$ rake db:create_migration NAME=create_draftsCopy 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:migrateAdd 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:
# 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,或尚未同步最近议题。