This gem will give you only two scopes and a method for handling published/unpublished stuff. Nothing remarkable bur saved me a lot of typing when I wrote it for a project with an huge number of publishable models. Suggestions are more than welcome.

How It Works

In order to use in your model, write something like the following:


    class Articile < ActiveRecord::Base
        acts_as_publicable
    end

With that you get three very simple scopes:


ruby-1.9.2-p180 :001 > Article.unpublished
  Article Load (0.2ms)  SELECT `articles`.* FROM `articles` WHERE (published = 0)
 => [] 
ruby-1.9.2-p180 :002 > Article.published
  Article Load (0.2ms)  SELECT `articles`.* FROM `articles` WHERE (published = 1)
 => []
ruby-1.9.2-p180 :003 > Article.by_published_state(true)
  Article Load (0.2ms)  SELECT `articles`.* FROM `articles` WHERE (published = 1)
 => []

and a method to publish an article:


ruby-1.9.2-p180 :011 > @article=Article.first
Article Load (0.2ms)  SELECT `articles`.* FROM `articles` LIMIT 1
ruby-1.9.2-p180 :012 > @article.publish!
  SQL (0.1ms)  BEGIN
  AREL (0.2ms)  UPDATE `articles` SET `published` = 1, `updated_at` = '2011-04-05 08:50:45' WHERE (`articles`.`id` = 1)
  SQL (48.3ms)  COMMIT
 => true 

Generator

You can use a generator to add the published field to your target model:

rails g acts_as_publicable model_name

It will generate a simple migration that will add the neeeded field.

Roadmap

  • checking input for generator
  • comment code
  • add a date for handling a better publishing concept

Copyright

This program is free software. It comes without any warranty, to the extent permitted by applicable law. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more details.