Module: ArPublishControl::Publishable::ClassMethods

Defined in:
lib/ar_publish_control_with_scopes.rb

Instance Method Summary collapse

Instance Method Details

#publish_control(options = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ar_publish_control_with_scopes.rb', line 103

def publish_control(options = {})
   # don't allow multiple calls
   return if self.included_modules.include?(ArPublishControl::Publishable::InstanceMethods)

   write_inheritable_attribute(:publish_scopes, options[:scopes] || [])

   publish_scopes.each do |scope|
     send :class_eval, %Q{
       def #{scope}_published?
         is_#{scope}_published?
       end
   
       def #{scope}_publish
         return if #{scope}_published?
         self.is_#{scope}_published = true
         self.publish_at = Time.now
         self.unpublish_at = nil
       end
   
       def #{scope}_publish!
         #{scope}_publish
         save!
       end
   
       def #{scope}_unpublish
         self.is_#{scope}_published = false
       end
   
       def #{scope}_unpublish!
         #{scope}_unpublish
         save!
       end
     }
   end        
  
   publish_by_default = options[:publish_by_default] || true
   
  
   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

#publish_scopesObject

# == 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 <tt>true</tt>) 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 <tt>datetime</tt>-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
# 
#@@publish_scopes = []


99
100
101
# File 'lib/ar_publish_control_with_scopes.rb', line 99

def publish_scopes
  read_inheritable_attribute :publish_scopes
end