Rails Admin Draft

Rails Admin Draft allows you to make drafts of the desired models in rails_admin.

Installation

Add this line to your application's Gemfile:

gem 'rails_admin_draft'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install rails_admin_draft

Usage

Add draft columns to your table

Create migration

$ rails generate draft_migration --table [TABLE]

Run the migration

$ rake db:migrate

Add draft to your model

class MyModel < ApplicationRecord
  include DraftConcern
  has_draft
end

Add draft to your rails_admin model config

RailsAdmin.config do |config|
  config.model 'MyModel' do
    has_draft true
  end
end

Add copy_as_draft action in your rails_admin config

You can skip this part if you don't want the copy_as_draft action in the listings.

RailsAdmin.config do |config|
  config.actions do
    copy_as_draft
  end
end

Additional information

Available scopes

When your model includes DraftConcern, you have access to the following scopes:

  • draft: Records where draft_status = 'draft'
  • published: Records where draft_status = 'published'

Available rails_admin scopes

When your model includes DraftConcern, you have access to the following scopes:

  • rails_admin_all: All records
  • rails_admin_draft: Records where draft_status = 'draft'
  • rails_admin_published: Records where draft_status = 'draft'

These can be used to add scopes in rails_admin listing:

RailsAdmin.config do |config|
  config.model 'MyModel' do
    has_draft true
    list do
      scopes [:rails_admin_all, :rails_admin_published, :rails_admin_draft]
    end
  end
end

Available methods

When your model includes DraftConcern, you have access to the following methods:

  • title_with_draft_status: Adds 'draft' to your model's title if it's a draft. Used to display in rails_admin listing. By default, the title attribute is title. If you want to change it, override the title_attribute class method in your model.
  • is_draft?: Returns true if your model is a draft
  • is_published?: Returns true if your model is published

Overridable class methods

When your model includes DraftConcern, you can override these class methods:

  • unique_text_fields: List of string fields that are unique. When you copy a published record as draft, the string 'draft-' will be added at the beginning of each field to avoid unique constraint errors. The string 'draft-' is removed when the record is published. By default, the only field is slug.
  • except_upload_attributes: When you copy a record as draft, its uploads are also copied. To prevent an upload of being copied, add it to this list. You can also add attributes in associated tables. Ex: [ :image, { relation_name: :associated_model, attribute: :image } ]
  • title_attribute: The attribute that represent the title/name of the record. By default, it is set to title. Used by the title_with_draft_status method.

How to handle unique indexes

Since this gem allows a published record to be copied as draft, you have to make sure your unique indexes are composite indexes including the draft_status attribute.