Please note that this project has moved to Github: http://github.com/avdgaag/acts_as_publishable*
Specify this act if you want to show or hide your object based on date/time settings. This act lets you specify two dates two form a range in which the model is publicly available; it is unavailable outside it.
InstallationUse subversion to check out this plugin into your Rails project:
svn checkout http://actsaspublishable.googlecode.com/svn/trunk/ actsaspublishableYou can also check out or export stable releases; see http://actsaspublishable.googlecode.com/svn/tags/.
UsageYou can add this behaviour to your model like so:
class Post < ActiveRecord::Base
acts_as_publishable
endThen you can use it as follows:
post = Post.create(:title => 'Hello world')
post.published? # => true
post.publish!
post.published? # => true
post.unpublish!
post.published? # => falseYou can use two special finder methods to find the published or unpublished objects. Use them like you use your standard #find:
Post.find(:all).size # => 15
Post.find_published(:all).size # => 10
Post.find_unpublished(:all).size # => 5Finally, there are scoping methods for limiting your own custom finders to just published or unpublished objects. These are simple wrapper methods for the #with_scope method and hence are used as follows:
class Post < ActiveRecord::Base
def find_recent
published_only do
find :all, :limit => 5, :order => 'created_at DESC'
end
end
endDo note that it is considered poor style to use scoping methods like this in your controller. You can, but always try moving it into you model.
Configuration optionsRight now this plugin has only one configuration option. Models with no publication dates are by default published, not unpublished. If you want to hide your model when it has no explicit publication date set, you can turn off this behaviour with the publish_by_default (defaults to true) option like so:
class Post < ActiveRecord::Base
acts_as_publishable :publish_by_default => false
endDatabase SchemaThe model that you're publishing needs to have two special date attributes:
publish_at unpublish_at These attributes have no further requirements or required validations; they just need to be datetime-columns.
You can use a migration like this to add these columns to your model:
class AddPublicationDatesToPosts < ActiveRecord::Migration
def self.up
add_column :posts, :publish_at, :datetime
add_column :posts, :unpublish_at, :datetime
end
def self.down
remove_column :posts, :publish_at
remove_column :posts, :unpublish_at
end
end