Module: ArPublishControl

Defined in:
lib/ar_publish_control.rb

Defined Under Namespace

Modules: Publishable

Constant Summary collapse

VERSION =
'0.0.9'
STATUSES =

This is a gem version of github.com/avdgaag/acts_as_publishable ( a Rails plugin) Thanks to Avdaag for his awesome, super readable code which I ripped off for this gem.

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.

Usage

You can add this behaviour to your model like so:

class Post < ActiveRecord::Base
  publish_control
end

Then you can use it as follows:

post = Post.create(:title => 'Hello world')
post.published? # => true

post.publish!
post.published? # => true

post.unpublish!
post.published? # => false

You can use two named_scopes to find the published or unpublished objects. You can chain them with other scopes and use them on association collections:

 Post.all.count          # => 15
 Post.published.count    # => 10
 Post.unpublished.count  # => 5 includes upcoming and expired
 Post.draft.count        # => where is_published == false regardless of date
 @post.comments.published

 Post.recent.published

Klass.unpublished includes all objects which is_published flag is set to false and/or are expired or upcoming.
If you specifically want those where is_published == false regardless of dates, use Klass.draft

There’s a third named_scope that you can pass a boolean in order to find only published items or all of them This is useful in controller for permission-based publish control

@post       = Post.published.find(params[:id])
@comments   = @post.comments.only_published( logged_in? )

Available statuses. This constant is useful for autogenerated routes, controller actions and view helpers

[:published, :draft, :upcoming, :expired]