<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -91,14 +91,16 @@ module ArPublishControl
         return if self.included_modules.include?(ArPublishControl::Publishable::InstanceMethods)
         send :include, ArPublishControl::Publishable::InstanceMethods
         
-        named_scope :published, :conditions =&gt; published_conditions
-        named_scope :unpublished, :conditions =&gt; unpublished_conditions
+        named_scope :published, lambda{{:conditions =&gt; published_conditions}}
+        named_scope :unpublished, lambda{{:conditions =&gt; unpublished_conditions}}
+        named_scope :upcoming, lambda{{:conditions =&gt; upcoming_conditions}}
         
         named_scope :published_only, lambda {|*args|
           bool = (args.first.nil? ? true : (args.first)) # nil = true by default
           {:conditions =&gt; (bool ? published_conditions : unpublished_conditions)}
         }
-        before_create :set_default_publication_date if options[:publish_by_default]
+        before_validation :init_publish_date # don't allow empty publish_at
+        before_create :publish_by_default if options[:publish_by_default]
       end
       
       # Takes a block whose containing SQL queries are limited to
@@ -118,26 +120,39 @@ module ArPublishControl
       # returns a string for use in SQL to filter the query to unpublished results only
       # Meant for internal use only
       def unpublished_conditions
-        &quot;(#{table_name}.publish_at IS NULL OR #{table_name}.publish_at &gt; '#{Time.now.to_s(:db)}') OR (#{table_name}.unpublish_at IS NOT NULL AND #{table_name}.unpublish_at &lt; '#{Time.now.to_s(:db)}')&quot;
+        t = Time.now
+        [&quot;(#{table_name}.is_published = ? OR #{table_name}.publish_at &gt; ?) OR (#{table_name}.unpublish_at IS NOT NULL AND #{table_name}.unpublish_at &lt; ?)&quot;,false,t,t]
       end
       
       # return a string for use in SQL to filter the query to published results only
       # Meant for internal use only
       def published_conditions
-        &quot;(#{table_name}.publish_at IS NOT NULL AND #{table_name}.publish_at &lt;= '#{Time.now.to_s(:db)}') AND (#{table_name}.unpublish_at IS NULL OR #{table_name}.unpublish_at &gt; '#{Time.now.to_s(:db)}')&quot;
+        t = Time.now
+        [&quot;(#{table_name}.is_published = ? AND #{table_name}.publish_at &lt;= ?) AND (#{table_name}.unpublish_at IS NULL OR #{table_name}.unpublish_at &gt; ?)&quot;,true,t,t]
+      end
+      
+      def upcoming_conditions
+        t = Time.now
+        [&quot;(#{table_name}.is_published = ? AND #{table_name}.publish_at &gt; ?)&quot;,true,t]
       end
     end
     
     module InstanceMethods
       
-      # ActiveRecrod callback fired on +after_create+ to make 
+      # Publish at is NOW if not set
+      def init_publish_date
+        write_attribute(:publish_at, Time.now) if publish_at.nil?
+      end
+      
+      # ActiveRecrod callback fired on +before_create+ to make 
       # sure a new object always gets a publication date; if 
       # none is supplied it defaults to the creation date.
-      def set_default_publication_date
-        write_attribute(:publish_at, Time.now) if publish_at.nil?
+      def publish_by_default
+        write_attribute(:is_published, true) if is_published.nil?
       end
       
       # Validate that unpublish_at is greater than publish_at
+      # publish_at must not be nil
       def validate
         if unpublish_at &amp;&amp; publish_at &amp;&amp; unpublish_at &lt;= publish_at
           errors.add(:unpublish_at,&quot;should be greater than publication date or empty&quot;)
@@ -148,12 +163,17 @@ module ArPublishControl
       
       # Return whether the current object is published or not
       def published?
-        (!publish_at.nil? &amp;&amp; (publish_at &lt;=&gt; Time.now) &lt;= 0) &amp;&amp; (unpublish_at.nil? || (unpublish_at &lt;=&gt; Time.now) &gt;= 0)
+        (is_published? &amp;&amp; (publish_at &lt;=&gt; Time.now) &lt;= 0) &amp;&amp; (unpublish_at.nil? || (unpublish_at &lt;=&gt; Time.now) &gt;= 0)
+      end
+      
+      def upcoming?
+        (is_published? &amp;&amp; publish_at &gt; Time.now)
       end
       
       # Indefinitely publish the current object right now
       def publish
         return if published?
+        self.is_published = true
         self.publish_at = Time.now
         self.unpublish_at = nil
       end
@@ -168,7 +188,7 @@ module ArPublishControl
       # Immediatly unpublish the current object
       def unpublish
         return unless published?
-        self.unpublish_at = 1.minute.ago
+        self.is_published = false
       end
       
       # Same as unpublish, but immediatly saves the object.</diff>
      <filename>lib/ar_publish_control.rb</filename>
    </modified>
    <modified>
      <diff>@@ -91,11 +91,6 @@ describe Comment do
     @published_comment_in_published_post.errors.on(:unpublish_at).should == &quot;should be greater than publication date or empty&quot;
   end
   
-  it &quot;should unpublish if publish_at is nil&quot; do
-    @published_post.update_attributes :publish_at =&gt; nil
-    @published_post.published?.should be_false
-    Post.published.size.should == 0
-  end
 end
 
 # describe Post, 'with no dates' do
@@ -121,5 +116,29 @@ describe Post, &quot;unpublished by default&quot; do
   
   it &quot;should be unpublished by default&quot; do
     @a1.published?.should_not be_true
+    Article.published.size.should == 0
+  end
+  
+  it &quot;should publish&quot; do
+    @a1.publish!
+    @a1.published?.should be_true
+  end
+end
+
+describe Post, 'upcoming' do
+  before(:each) do
+    Post.destroy_all
+    @p1 = Post.create(:title =&gt; 'p1',:is_published =&gt; true,:publish_at =&gt; 1.day.from_now) #upcoming
+    @p2 = Post.create(:title =&gt; 'p2',:is_published =&gt; true,:publish_at =&gt; 1.week.from_now)#upcoming
+    @p3 = Post.create(:title =&gt; 'p3',:is_published =&gt; false,:publish_at =&gt; 1.day.from_now)#unpublished
+    @p4 = Post.create(:title =&gt; 'p4',:is_published =&gt; true)#published
+  end
+  
+  it &quot;should have upcoming&quot; do
+    @p1.upcoming?.should be_true
+    @p2.upcoming?.should be_true
+    @p3.upcoming?.should be_false
+    @p4.upcoming?.should be_false
+    Post.upcoming.size.should == 2
   end
 end</diff>
      <filename>spec/ar_publish_control_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -14,7 +14,7 @@ ActiveRecord::Base.establish_connection(
   :dbfile=&gt; File.join(File.dirname(__FILE__),'db','test.db')
 )
 
-ActiveRecord::Base.connection.query_cache_enabled = false
+#ActiveRecord::Base.connection.query_cache_enabled = false
 
 LOGGER = Logger.new(File.dirname(__FILE__)+'/log/test.log')
 ActiveRecord::Base.logger = LOGGER</diff>
      <filename>spec/spec_helper.rb</filename>
    </modified>
    <modified>
      <diff>@@ -9,6 +9,7 @@ class TestSchema &lt; ActiveRecord::Migration
   def self.up
     create_table :posts do |t|
       t.string :title
+      t.boolean  :is_published
       t.datetime :publish_at
       t.datetime :unpublish_at
       t.timestamps
@@ -16,6 +17,7 @@ class TestSchema &lt; ActiveRecord::Migration
     
     create_table :articles do |t|
       t.string :title
+      t.boolean  :is_published
       t.datetime :publish_at
       t.datetime :unpublish_at
       t.timestamps
@@ -23,6 +25,7 @@ class TestSchema &lt; ActiveRecord::Migration
     
     create_table :comments do |t|
       t.text :body
+      t.boolean  :is_published
       t.integer :post_id
       t.datetime :publish_at
       t.datetime :unpublish_at</diff>
      <filename>tasks/db.rake</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>331c3036155e3ca09e3efe322e6c6ff447a184b4</id>
    </parent>
  </parents>
  <author>
    <name>Ismael Celis</name>
    <email>ismael@ismaelct.com</email>
  </author>
  <url>http://github.com/ismasan/ar_publish_control/commit/944ce9bc1f8c97c117e5b6149352eaa4f77d78b4</url>
  <id>944ce9bc1f8c97c117e5b6149352eaa4f77d78b4</id>
  <committed-date>2008-11-18T05:29:30-08:00</committed-date>
  <authored-date>2008-11-18T05:29:30-08:00</authored-date>
  <message>Added upcoming</message>
  <tree>79fa05b0379da389619e8668f28db194ff8a7d1c</tree>
  <committer>
    <name>Ismael Celis</name>
    <email>ismael@ismaelct.com</email>
  </committer>
</commit>
