Module: Postablr::ArPublish::ClassMethods

Defined in:
app/models/postablr/ar_publish.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



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/postablr/ar_publish.rb', line 51

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

  scope :published, lambda{{:conditions => published_conditions}}
  scope :unpublished, lambda{{:conditions => unpublished_conditions}}
  scope :upcoming, lambda{{:conditions => upcoming_conditions}}
  scope :expired, lambda {{:conditions => expired_conditions}}
  scope :draft, :conditions => {:is_published => false}

  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