Module: ArPublishControl::Publishable::ClassMethods

Defined in:
lib/ar_publish_control.rb

Instance Method Summary collapse

Instance Method Details

#publish_control(options = { :publish_by_default => true }) ⇒ Object

Configuration options

Right 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
  publish_control :publish_by_default => false
end

Database Schema

The 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


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ar_publish_control.rb', line 97

def publish_control(options = { :publish_by_default => true })
  # don't allow multiple calls
  return if self.included_modules.include?(ArPublishControl::Publishable::InstanceMethods)
  send :include, ArPublishControl::Publishable::InstanceMethods
  
  named_scope :published, lambda{{:conditions => published_conditions}}
  named_scope :unpublished, lambda{{:conditions => unpublished_conditions}}
  named_scope :upcoming, lambda{{:conditions => upcoming_conditions}}
  named_scope :expired, lambda {{:conditions => expired_conditions}}
  named_scope :draft, :conditions => {:is_published => false}
  
  named_scope :published_only, lambda {|*args|
    bool = (args.first.nil? ? true : (args.first)) # nil = true by default
    bool ? {:conditions => published_conditions} : {}
  }
  
  validate :validate_publish_date_consistency
  before_create :publish_by_default if options[:publish_by_default]
end