publishable Build Status

Publishable allows a given Boolean, Date, or DateTime column to indicate whether an ActiveRecord model object is “published”. This can be used, for example, to schedule a news post to be published at some later date on a website, or to hide work-in-progress content until it is ready to be released to the public.

Methods are provided to publish and unpublish the model object, and scopes are added for filtering your records.

Installation

Add this line to your application’s Gemfile:

gem 'publishable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install publishable

Setup

  1. Create the publishable column via a migration:

    rails generate migration add_published_to_post published:date
    rails generate migration add_published_to_story published:datetime
    rails generate migration add_released_to_album released:boolean
    
  2. Declare that your models are publishable:

    class Post < ActiveRecord::Base
      publishable
    end
    
    class Story < ActiveRecord::Base
      publishable
    end
    
    class Album < ActiveRecord::Base
      publishable :on => :released
    end
    

The column name used by publishable can be specified via the :on option. The default used (if :on is not specified) is :published. If publishing via a Boolean column, be sure to add a default value to the column in the migration— otherwise existing models will be neither published nor unpublished.

Usage

published_posts = Post.recent(5) # Returns the five most recently-published Posts, in descending order by publish-date

album = Album.new
album.published?  # false - by default, records are not published
album.publish     # sets the 'released' column to true, but does not save it back to the database
album.publish!    # publishes AND saves the record to the database
album.published?  # true
album.unpublish!  # sets the 'released' column back to false and saves the record

story = Story.new
story.publish!(2.hours.from_now)    # Sets the story to go live in two hours
story.unpublish!                    # Sets the 'published' column to nil
upcoming_stories = Story.upcoming   # Get all stories that will be published in the future, in ascending order by publish-date

Contributing to publishable

Copyright © 2010-2014 Martin Linkhorst, released under the MIT license. Copyright © 2013-2014 тιηуηυмвєяѕ. See LICENSE.txt for further details. Copyright © 2013-2014 Andrew Janssen. See LICENSE.txt for further details.