Skip to content

Instantly share code, notes, and snippets.

@chrisdpeters
Last active May 21, 2017 12:22
Show Gist options
  • Select an option

  • Save chrisdpeters/8324486e91eaf9e1b1ed to your computer and use it in GitHub Desktop.

Select an option

Save chrisdpeters/8324486e91eaf9e1b1ed to your computer and use it in GitHub Desktop.
Draftsman gem: Add a draft state to your Ruby on Rails and Sinatra relational models http://blog.liveeditorcms.com/draftsman-gem-draft-state-ruby-on-rails-sinatra-relational-models/
# Accessing drafts through the `Draftsman::Draft` class
@drafts = Draftsman::Draft.order(:created_at)
# Accessing a draft through a model
@post = Post.find(params[:id])
@draft = @post.draft if @post.draft?
class Admin::PostsController < Admin::BaseController
def index
@posts = Post.live.includes(:draft).to_a
@posts.map! { |post| post.draft? ? post.draft.reify : post }
end
def show
@post = Post.live.find(params[:id])
@post = @post.draft.reify if @post.draft?
end
end
Widget.drafted # Limits to items that have drafts. Best used in an "admin" area in your application.
Widget.published # Limits to items that have been published at some point in their lifecycles. Best used in a "public" area in your application.
Widget.trashed # Limits to items that have been drafted for deletion (but not fully committed for deletion). Best used in an "admin" area in your application.
Widget.live # Limits to items that have not been drafted for deletion. Best used in an "admin" area in your application.
@dizo1

dizo1 commented May 21, 2017

Copy link
Copy Markdown

Thank you for your work regarding draftsman kindly could you take me through how to get it up and working.I have a recipes project i would like to apply it to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment