This is a gem version of github.com/avdgaag/acts_as_publishable ( a Rails plugin) Thanks to Arjan van der Gaag for his original work in that plugin, which this gem is heavily based off.
His plugin is Copyright © 2007 Arjan van der Gaag, released under the MIT license
ar_publish_control¶ ↑
github.com/ismasan/ar_publish_control
DESCRIPTION:¶ ↑
Add start/end publishing dates to your ActiveRecord models.
FEATURES/PROBLEMS:¶ ↑
Adds published, unpublished and published_only(boolean) named_scopes to your models.
SYNOPSIS:¶ ↑
class Post < ActiveRecord::Base publish_control end
# creating @post = Post.create(params[:post])
@post.publish!
# With start and end dates @post.update_attributes :publish_at => Time.now, :unpublish_at => 2.weeks.from_now
# Or, in your views... <% form_for @post do |f| %> ... <p> <%= f.label :is_published, "Publish" %> <%= f.check_box :is_published %> </p> <p> <%= f.label :publish_at, "From" %> <%= f.date_select :publish_at %> </p> <p> <%= f.label :unpublish_at, "To" %> <%= f.date_select :unpublish_at %> </p> ... <% end %>
# in your controllers def index @posts = Post.published end def show @post = Post.published.find(params[:id]) @post.published? # => true end # You can nest scopes: @post = current_user.posts.published(params[:id]) @posts = current_user.posts.published.paginate(:page => params[:page]) # unpublished ones @posts = Post.unpublished # All posts if logged_in?, only published otherwise @posts = Post.published_only?( !logged_in? ).paginate(:page => params[:page]) # Unpublish @post.unpublish! @post.unpublished? # => true # You can access the ones marked as "published" but with a publish date in the future @upcoming_post = Post.upcoming @upcoming_post.first.upcoming? # => true # You can fetch objects that have expired (ie. they have an unpublish_at date set in the past) @expired_posts = Post.expired @expired_posts.first.expired? # => true # Finally, if you want those objects where is_published == false, regardless of dates @drafts = Post.draft
All of these named_scopes can be chained with other finder conditions and paginated with will_paginate
REQUIREMENTS:¶ ↑
ActiveRecord
INSTALL:¶ ↑
If you haven’t yet, add github.com to your gem sources (you only need to do that once):
gem sources -a http://gems.github.com
Now you can install the normal way:
sudo gem install ismasan-ar_publish_control
Add the necessary fields to you schema, in a migration:
class AddPublishableToPosts < ActiveRecord::Migration def self.up add_column :posts, :is_published, :boolean, :default => true add_column :posts, :publish_at, :datetime add_column :posts, :unpublish_at, :datetime # update existing records if you have them Post.update_all "publish_at = created_at" end def self.down remove_column :posts, :is_published remove_column :posts, :publish_at remove_column :posts, :unpublish_at end end
rake db:migrate
Then, in your Rails app’s environment:
config.gem 'ismasan-ar_publish_control',:lib => 'ar_publish_control',:source => "http://gems.github.com"
…Or in Merb or elsewhere
require "ar_publish_control"
If you wan to unpack the gem to you app’s “vendor” directory:
rake gems:unpack
TEST¶ ↑
Test are in the spec directory (rspec). To run them, first initialize the test sqlite database
rake db:create
Now you can run the specs
rake spec
LICENSE:¶ ↑
(The MIT License)
Copyright © 2008 Ismael Celis
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.