Coder Social home page Coder Social logo

live_draft's Introduction

Live Draft

Easily specify which method to use to display an object. It was created to work in Rails models, though it works with any Ruby class.

This gem is in an early stage of development, so it might not be as configurable as you’d hope. However, it has already been used in production environments with the default settings.

**Important note: THIS GEM ISN’T READY FOR PRODUCTION!**

Motivation

Most CMS let you write articles and have two buttons: “save draft” and “publish”. However, once an article is published, it’s impossible to change it and save the changes as a draft while at the same time keeping the published version.

Keeping both versions might allow you to implement an “autosave draft” feature while editing an article, so the user doesn’t lose any changes if something goes wrong with the browser.

I’ve only seen one gem providing this behaviour, called has_draft. However, it requires two tables with the same structure: one for the published version and one for the draft. This gem tries to implement the same idea, but it avoids duplication but using just one table.

Install

In your Gemfile:

gem "live_draft"

Ruby 1.9, Rails 3 and active record are required.

Usage

First of all, you need to add a column to your table to establish the relation between a draft and its page:

class AddPublishedToArticle < ActiveRecord::Migration
  def self.up
    add_column :articles, :draft, :boolean
    add_column :articles, :published_id, :integer
  end

  def self.down
    remove_column :articles, :draft

remove_column :articles, :published_id

  end
end

Then you have to declare it in your model:

class Article < ActiveRecord::Base
  has_live_draft
end

And you can use the methods provided by the gem:

article = Article.create(:title => "My original title")
article.title = "My new title"
article.has_draft?
article.save_draft
article.has_draft?
article.title
article.draft.title
article.draft.publish
article.has_draft?
article.title

TODO

  • Add generators for migrations.

  • Make it ORM agnostic.

  • Compatibility with more table configurations (not only a boolean column).

Copyright © 2011 Javier Martín. See LICENSE for details.

live_draft's People

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.