<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -1,56 +1,50 @@
 class Admin::PostsController &lt; Admin::AdminController
   
   def index
-    @posts = Post.all
+    @posts = Post.by_admin_posts
   end
 
   def new
   end
   
   def create
-    # create the article
-    @post = Post.new(params[:post])
+    # create the post
+    @post = Post.new_from_params(params)
     
     respond_to do |format|
-      if params[:preview]
-        format.html {
-          session[&quot;post_preview&quot;] =  @post
-          redirect_to(preview_admin_post_url(@post.generate_unique_permalink_from_title))
-        }
-      else
-        @post.status = 'published' if params[:publish]
         if @post.save
-          format.html { redirect_to(edit_admin_post_url(@post.permalink)) }
+          format.html {
+            params[:preview] ? redirect_to(preview_admin_post_url(@post.permalink)) : redirect_to(edit_admin_post_url(@post.permalink))
+           }
         else
           format.html { render :action =&gt; &quot;new&quot; }
         end
-      end
     end
     
   end
 
   def edit
-    @post = Post.get(params[:id])
+    @post = Post.find_by_permalink(params[:id]) 
   end
   
   def update
-    @post = Post.get(params[:id])
+    @post = Post.find_by_permalink(params[:id]) 
+    
+    #should we see if they diff
+    @post.preview = params[:post][:body] if params[:preview]
+    
+    @post.status = 'published' if params[:publish]
     
     respond_to do |format|
-      if params[:preview]
-        format.html {
-          @new_post = Post.new(params[:post])
-          session[&quot;post_preview&quot;] = @new_post
-          redirect_to(preview_admin_post_url(@new_post.generate_unique_permalink_from_title))
+      
+      @post.status = 'published' if params[:publish]
+      if @post.update_attributes(params[:post])
+        format.html { 
+          params[:preview] ? redirect_to(preview_admin_post_url(@post.permalink)) : redirect_to(edit_admin_post_url(@post.permalink)) 
         }
       else
-        @post.status = 'published' if params[:publish]
-        if @post.update_attributes(params[:post])
-          format.html { redirect_to(edit_admin_post_url(@post.permalink)) }
-        else
-          format.html { render :action =&gt; &quot;edit&quot; }
-        end
-      end
+        format.html { render :action =&gt; &quot;edit&quot; }
+      end   
     end
   end
 
@@ -58,24 +52,11 @@ class Admin::PostsController &lt; Admin::AdminController
     #mark the post as a preview
     @post_preview = true
     
-    if session[&quot;post_preview&quot;]
-      @post = session[&quot;post_preview&quot;]
-      session[&quot;post_preview&quot;] = nil
-    end
-    
     #use id if on query string
-    @post = Post.get(params[:id]) if params[:id]
     
-    respond_to do |format|
-      format.html { render :template =&gt; 'posts/show', :layout =&gt; 'application'}
-    end
-  end
-  
-  def post_preview
-    #mark the post as a preview
-    @post_preview = true
+    @post = Post.find_by_permalink(params[:id])
+    @post.body = @post.preview unless @post.preview.nil?
     
-    @post = Post.new(:title =&gt; params[:post_title], :body =&gt; params[:post_body])
     
     respond_to do |format|
       format.html { render :template =&gt; 'posts/show', :layout =&gt; 'application'}</diff>
      <filename>app/controllers/admin/posts_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -11,7 +11,7 @@ class PostsController &lt; ApplicationController
   end
 
   def show
-    @post = Post.get(params[:id])
+    @post = Post.find_by_permalink(params[:permalink])
     
     respond_to do |format|
       if @post.published? &amp;&amp; @post.verify_date?(:month =&gt; params[:month], :year =&gt; params[:year])</diff>
      <filename>app/controllers/posts_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -5,11 +5,9 @@ class Post &lt; CouchRest::ExtendedDocument
   # -------------------
   #  Properties
   # -------------------
-  
-  unique_id :permalink
-  
   property :title
   property :body
+  property :preview, :default =&gt; nil
   property :status, :default =&gt; 'draft'
   property :permalink
   property(:updated_at, :read_only =&gt; true, :cast_as =&gt; 'Time', :auto_validation =&gt; false)
@@ -20,17 +18,11 @@ class Post &lt; CouchRest::ExtendedDocument
 
   
   validates_present :title, :body
-  
+  #validates_is_unique property.name
 
   
   def set_permalink_from_title
-
     self['permalink'] = generate_unique_permalink_from_title if new_record? || title_changed?
-    
-    # if the title has changed and its an edit
-    # set a new id and clean up the old doc
-    # TODO look at copy method in couch rest
-    cleanup_old_doc if title_changed? and not new_record?  
   end
   
   # generates a unique permalink from the title
@@ -38,17 +30,53 @@ class Post &lt; CouchRest::ExtendedDocument
     
     permalink = title_to_permalink(title)
     permalink = &quot;#{permalink}-#{postfix}&quot; if postfix &gt; 1
-    
 
-    # if post exists call again adding postfix
-    # if currnt post exists then it will find itself anf +1
-    if Post.exists?(permalink)
-      generate_unique_permalink_from_title(postfix+1) 
+    if Post.permalink_unique?(permalink)
+      # permalink is ok and just return
+      return permalink 
     else
-     return permalink
+      # call the same method again with an incremented number
+      generate_unique_permalink_from_title(postfix+1)
     end
   end
   
+  # alternative constructor that return a new post object with some values set 
+  def self.new_from_params(params)
+    post = Post.new(params[:post])
+    #set the status
+    if params[:preview]
+      post.status = 'preview'
+    end 
+    post.status = 'published' if params[:publish]
+
+    post
+  end
+  
+  def set_updated_at(time)
+    self['updated_at'] = Time.parse(time)
+  end
+  
+  
+  # -------------------
+  #  Find by ..
+  # -------------------
+  
+  # will return the first post it finds by the pemalink
+  def self.find_by_permalink(permalink)
+    Post.by_permalink(:key =&gt; permalink).first
+  end
+  
+  def self.find_by_year(year)
+    year = year.to_i
+    self.by_published(:startkey =&gt; [year,12,31], :endkey =&gt; [year,1,1])
+  end
+  
+  def self.find_by_year_and_month(year, month)
+    month = month.to_i
+    year = year.to_i
+    self.by_published(:startkey =&gt; [year,month,Time.days_in_month(month)], :endkey =&gt; [year,month,1])
+  end 
+  
   # -------------------
   #  Views
   # -------------------
@@ -56,6 +84,17 @@ class Post &lt; CouchRest::ExtendedDocument
   view_by :created_at, :descending =&gt; true
   view_by :status, :created_at, :descending =&gt; true
   
+  view_by :permalink
+  
+  # exclude preview posts and order by updated_at
+  view_by :admin_posts, :descending =&gt; true,
+    :map =&gt;
+     &quot;function(doc) {
+       if ((doc['couchrest-type'] == 'Post') &amp;&amp; (doc['status'] != 'preview') &amp;&amp; doc['updated_at']) {
+        emit(doc['updated_at'], 1);
+       }
+     }&quot;
+  
   # it is implied that all published articles are 
   # ordered by date, descending
   view_by :published, :descending =&gt; true,
@@ -74,16 +113,7 @@ class Post &lt; CouchRest::ExtendedDocument
          return sum(values);
        }&quot;
 
-  def self.find_by_year(year)
-    year = year.to_i
-    self.by_published(:startkey =&gt; [year,12,31], :endkey =&gt; [year,1,1])
-  end
-  
-  def self.find_by_year_and_month(year, month)
-    month = month.to_i
-    year = year.to_i
-    self.by_published(:startkey =&gt; [year,month,Time.days_in_month(month)], :endkey =&gt; [year,month,1])
-  end
+
   
   # sum_by_month
   # 
@@ -92,16 +122,27 @@ class Post &lt; CouchRest::ExtendedDocument
     self.by_published :reduce =&gt; true, :group_level =&gt; 2
   end
   
-  def self.exists?(id)
-    !get(id).nil?
-  end
   
+  # -------------------
+  #  Before save
+  # -------------------
+  
+  #TODO remove this and use an explicit set for testing
   def set_timestamps
     self['updated_at'] = Time.now
     self['created_at'] = self['updated_at'] if self.new_document? &amp;&amp; self['created_at'].nil?
+  end  
+  
+  # check if the permalink is unique
+  def self.permalink_unique?(permalink)
+    !Post.find_by_permalink(permalink)
   end
   
+
   
+  # -------------------
+  #  useful methods
+  # -------------------
   def draft?
     self['status'] == 'draft'
   end
@@ -110,6 +151,10 @@ class Post &lt; CouchRest::ExtendedDocument
     self['status'] == 'published'
   end
   
+  def preview?
+    self['status'] == 'preview'
+  end
+  
   def year_and_month
     [self['created_at'].year.to_s, self['created_at'].strftime(&quot;%m&quot;)]
   end
@@ -129,20 +174,8 @@ class Post &lt; CouchRest::ExtendedDocument
   
   private
   
-  def cleanup_old_doc
-    # store the old id 
-    id = self['_id']
-    
-    #keep the same doc but set a new id
-    self['_id'] = self['permalink']
-    
-    # remove old doc
-    post = Post.get(id)
-    post.destroy
-  end
-  
   def title_changed?
-    (self['_id'] != title_to_permalink(self['title']))
+    (self['permalink'] != title_to_permalink(self['title']))
   end
   
 </diff>
      <filename>app/models/post.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,6 +3,7 @@
   &lt;thead&gt;
     &lt;tr&gt;
       &lt;th id=&quot;title&quot; scope=&quot;col&quot;&gt;Title&lt;/th&gt;
+      &lt;th id=&quot;updated&quot; scope=&quot;col&quot; class=&quot;medium&quot;&gt;Last Updated&lt;/th&gt;
       &lt;th id=&quot;created&quot; scope=&quot;col&quot; class=&quot;medium&quot;&gt;Created&lt;/th&gt;
       &lt;th&gt;&amp;nbsp;&lt;/th&gt;
       &lt;th&gt;&amp;nbsp;&lt;/th&gt;
@@ -13,10 +14,11 @@
   &lt;% @posts.each do |post| %&gt;
   &lt;tr class=&quot;&quot;&gt;
     &lt;td&gt;
-      &lt;strong class=&quot;title&quot;&gt;&lt;a href=&quot;&lt;%= edit_admin_post_path(post.id) %&gt;&quot;&gt;&lt;%= post.title %&gt;&lt;/a&gt;&lt;/strong&gt;
+      &lt;strong class=&quot;title&quot;&gt;&lt;a href=&quot;&lt;%= edit_admin_post_path(post.permalink) %&gt;&quot;&gt;&lt;%= post.title %&gt;&lt;/a&gt;&lt;/strong&gt;
     &lt;/td&gt;
+     &lt;td&gt;&lt;%= l post.updated_at, :format =&gt; :verbose %&gt;&lt;/td&gt;
     &lt;td&gt;&lt;%= l post.created_at.to_date, :format =&gt; :long %&gt;&lt;/td&gt;
-    &lt;td&gt;&lt;%= link_to &quot;edit&quot;, edit_admin_post_path(post.id) %&gt;&lt;/td&gt;
+    &lt;td&gt;&lt;%= link_to &quot;edit&quot;, edit_admin_post_path(post.permalink) %&gt;&lt;/td&gt;
     &lt;td&gt;&lt;%= button_to &quot;delete&quot;, admin_post_path(post.id), :method =&gt; :delete
      %&gt;&lt;/td&gt;
   &lt;/tr&gt;</diff>
      <filename>app/views/admin/posts/index.html.erb</filename>
    </modified>
    <modified>
      <diff>@@ -22,6 +22,7 @@ en:
       default: &quot;%a, %d %b %Y %H:%M:%S %z&quot;
       short: &quot;%d %b %H:%M&quot;
       long: &quot;%B %d, %Y %H:%M&quot;
+      verbose: &quot;%d %B %Y at %H:%M&quot;
     am: &quot;am&quot;
     pm: &quot;pm&quot;
       </diff>
      <filename>config/locales/en.yml</filename>
    </modified>
    <modified>
      <diff>@@ -29,7 +29,7 @@ ActionController::Routing::Routes.draw do |map|
   map.resource :user_session
 
   map.namespace :admin do |admin|
-   admin.resources :posts, :member =&gt; { :preview =&gt; :get }, :collection =&gt; {:post_preview =&gt; :post, :preview =&gt; :get}
+   admin.resources :posts, :member =&gt; { :preview =&gt; :get }
   end
   map.admin_root '/admin', :controller =&gt; 'admin/home'
   
@@ -39,7 +39,7 @@ ActionController::Routing::Routes.draw do |map|
   map.archives '/archives', :controller =&gt; 'posts', :action =&gt; 'archives'
   map.by_year ':year', :controller =&gt; 'posts', :action =&gt; 'by_year', :year =&gt; /\d{4}/
   map.by_month ':year/:month', :controller =&gt; 'posts', :action =&gt; 'by_month', :year =&gt; /\d{4}/, :month =&gt; /\d{2}/
-  map.post ':year/:month/:id', :controller =&gt; 'posts', :action =&gt; 'show'
+  map.post ':year/:month/:permalink', :controller =&gt; 'posts', :action =&gt; 'show'
   
   # syndication urls
   map.feed '/feed.:format',  :controller =&gt; 'syndication', :action =&gt; 'feed'</diff>
      <filename>config/routes.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,18 +3,20 @@ Feature: Manage posts
   As an article writer
   I want to edit, delete and view existing blog posts
     
-  Scenario: show a list of created posts
+  Scenario: show a list of created posts with recent updated to the top
     Given the following posts:
-      | title        | body             | created_at                |
-      | a title      | the amazing body | 2009/06/24 14:10:27 +0000 |
-      | the best title | the amazing body | 2009/06/26 21:07:32 +0000 |
-      | a great post | the amazing body | 2009/06/26 21:07:32 +0000 |   
+      | title          | body             | created_at                | updated_at                |
+      | a title        | the amazing body | 2009/06/24 14:10:27 +01:00 | 2009/06/24 14:10:27 +01:00 |
+      | the best title | the amazing body | 2009/06/26 21:07:32 +01:00 | 2009/08/26 21:07:32 +01:00 |
+      | a great post   | the amazing body | 2009/06/26 21:07:32 +01:00 | 2009/08/26 22:22:32 +01:00 |
+  
     And I am logged in as &quot;admin&quot;
     And I am on the post admin page
     Then I should see the following posts:
-    | title          | created at       |
-    | a great post        | 26 June 2009 |
-    | a title   | 24 June 2009 |
+    | title          | last updated            | created at   |
+    | a great post   | 26 August 2009 at 22:22 | 26 June 2009 |
+    | the best title | 26 August 2009 at 21:07 | 26 June 2009 |
+    | a title        | 24 June 2009 at 14:10   | 24 June 2009 |
   
   Scenario: edit an existing post
     Given I am logged in as &quot;admin&quot;
@@ -74,15 +76,15 @@ Feature: Manage posts
   Scenario: Delete a post
     Given I am logged in as &quot;admin&quot;
     And the following posts:
-      | title          | body             |
-      | a title        | the amazing body |
-      | a rubbish post | the crap body    |
-      | a great post   | the amazing body |
+      | title          | body             |updated_at                 |
+      | a title        | the amazing body |2009/09/24 14:10:27 +01:00 |
+      | a rubbish post | the crap body    |2009/08/26 21:07:32 +01:00 |
+      | a great post   | the amazing body |2009/07/26 21:07:32 +01:00 |
     When I delete the 2nd post
     Then I should see the following posts:
-    | title          |
+    | title          | 
+    | a title        |
     | a great post   | 
-    | a title        | 
     
 
   </diff>
      <filename>features/manage_posts.feature</filename>
    </modified>
    <modified>
      <diff>@@ -1,6 +1,12 @@
 Given /^the following posts?:$/ do |table|
   table.hashes.each do |hash|
-    Post.create!(hash)
+    post = Post.create!(hash)
+    # we need to override the updated_at with the values supplied
+    if hash.include?('updated_at')
+      post.set_updated_at(hash['updated_at'])
+      # save without running the set_timestamps
+      post.save_without_callbacks
+    end
   end
 end
 </diff>
      <filename>features/step_definitions/post_steps.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,5 +1,5 @@
 $(document).ready(function(){
-  $(&quot;#post_preview&quot;).bind(&quot;click&quot;, function(){
+/*  $(&quot;#post_preview&quot;).bind(&quot;click&quot;, function(){
 		// get the values from the page
 		var title = $('input#post_title').val();
 		var body = $('textarea#post_body').val();
@@ -11,6 +11,7 @@ $(document).ready(function(){
 
 		return false;
 	});
+*/
 });
 
 function openPopUp(html, windowName){</diff>
      <filename>public/javascripts/application.js</filename>
    </modified>
    <modified>
      <diff>@@ -43,19 +43,19 @@ describe Admin::PostsController do
     describe &quot;with valid params&quot; do
       
       it &quot;exposes a newly created post as @post&quot; do
-        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:save =&gt; true))
+        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:save =&gt; true, :preview? =&gt; false))
         post :create, :post =&gt; {:these =&gt; 'params'}
         assigns(:post).should equal(mock_post)
       end
       
       it &quot;redirect to edit after creation&quot; do
-        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:save =&gt; true, :status= =&gt; &quot;published&quot;))
+        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:save =&gt; true, :status= =&gt; &quot;published&quot;, :preview? =&gt; false))
         post :create, :post =&gt; {:these =&gt; 'params'}, :publish =&gt; 'Publish'
         response.should redirect_to(edit_admin_post_url(mock_post.permalink))
       end
       
       it &quot;should set status to published when published&quot; do
-        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(@foo = mock_post(:save =&gt; true))
+        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(@foo = mock_post(:save =&gt; true, :preview? =&gt; false))
         @foo.should_receive(:status=).with('published').and_return(true)
         post :create, :post =&gt; {:these =&gt; 'params'}, :publish =&gt; 'Publish'
       end
@@ -80,14 +80,14 @@ describe Admin::PostsController do
     
     describe &quot;rendering preview&quot; do
       
-      it &quot;should write post to session as 'post_preview'&quot; do
-        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:generate_unique_permalink_from_title =&gt; 'foo'))
+      it &quot;should set the status to preview&quot; do
+        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(@foo = mock_post(:save =&gt; true, :preview? =&gt; true))
+        @foo.should_receive(:status=).with('preview').and_return(true)
         post :create, :post =&gt; {:these =&gt; 'params'}, :preview =&gt; 'Preview'
-        session[&quot;post_preview&quot;].should == mock_post
       end
         
       it &quot;should redirect to preview page&quot; do
-        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:generate_unique_permalink_from_title =&gt; 'foo'))
+        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(@foo = mock_post(:save =&gt; true, :status= =&gt; true, :preview? =&gt; true))
         post :create, :post =&gt; {:these =&gt; 'params'}, :preview =&gt; 'Preview'
         response.should redirect_to(preview_admin_post_url(mock_post.permalink))
       end
@@ -100,19 +100,19 @@ describe Admin::PostsController do
     describe &quot;with valid params&quot; do
 
       it &quot;updates the requested post&quot; do
-        Post.should_receive(:get).with(&quot;37&quot;).and_return(mock_post)
+        Post.should_receive(:find_by_permalink).with(&quot;37&quot;).and_return(mock_post)
         mock_post.should_receive(:update_attributes).with({'these' =&gt; 'params'})
         put :update, :id =&gt; &quot;37&quot;, :post =&gt; {:these =&gt; 'params'}
       end
 
       it &quot;exposes the requested post as @post&quot; do
-        Post.stub!(:get).and_return(mock_post(:update_attributes =&gt; true))
+        Post.stub!(:find_by_permalink).and_return(mock_post(:update_attributes =&gt; true))
         put :update, :id =&gt; &quot;1&quot;
         assigns(:post).should equal(mock_post)
       end
 
       it &quot;redirects to the post&quot; do
-        Post.stub!(:get).and_return(mock_post(:update_attributes =&gt; true))
+        Post.stub!(:find_by_permalink).and_return(mock_post(:update_attributes =&gt; true))
         put :update, :id =&gt; &quot;1&quot;
         response.should redirect_to(edit_admin_post_url(mock_post.permalink))
       end
@@ -122,19 +122,19 @@ describe Admin::PostsController do
     describe &quot;with invalid params&quot; do
 
       it &quot;updates the requested post&quot; do
-        Post.should_receive(:get).with(&quot;37&quot;).and_return(mock_post)
+        Post.should_receive(:find_by_permalink).with(&quot;37&quot;).and_return(mock_post)
         mock_post.should_receive(:update_attributes).with({'these' =&gt; 'params'})
         put :update, :id =&gt; &quot;37&quot;, :post =&gt; {:these =&gt; 'params'}
       end
 
       it &quot;exposes the post as @post&quot; do
-        Post.stub!(:get).and_return(mock_post(:update_attributes =&gt; false))
+        Post.stub!(:find_by_permalink).and_return(mock_post(:update_attributes =&gt; false))
         put :update, :id =&gt; &quot;1&quot;
         assigns(:post).should equal(mock_post)
       end
 
       it &quot;re-renders the 'edit' template&quot; do
-        Post.stub!(:get).and_return(mock_post(:update_attributes =&gt; false))
+        Post.stub!(:find_by_permalink).and_return(mock_post(:update_attributes =&gt; false))
         put :update, :id =&gt; &quot;1&quot;
         response.should render_template('edit')
       end
@@ -143,18 +143,7 @@ describe Admin::PostsController do
     
     describe &quot;rendering preview&quot; do
       before do
-        Post.stub!(:get).and_return(mock_post(:generate_unique_permalink_from_title =&gt; 'foo'))
-        Post.should_receive(:new).with({'these' =&gt; 'params'}).and_return(mock_post(:generate_unique_permalink_from_title =&gt; 'foo'))
-      end
-      
-      it &quot;should create fresh post and expose as @new_post&quot; do
-        post :update, :id =&gt; &quot;foo&quot;, :post =&gt; {:these =&gt; 'params'}, :preview =&gt; 'Preview'
-        assigns(:new_post).should equal(mock_post)
-      end
-      
-      it &quot;should write post to session as 'post_preview'&quot; do
-        post :update, :id =&gt; &quot;foo&quot;, :post =&gt; {:these =&gt; 'params'}, :preview =&gt; 'Preview'
-        session[&quot;post_preview&quot;].should == mock_post
+        Post.stub!(:find_by_permalink).and_return(mock_post(:preview= =&gt; true, :update_attributes =&gt; true))
       end
         
       it &quot;should redirect to preview page&quot; do
@@ -163,55 +152,33 @@ describe Admin::PostsController do
       end
     end
 
-  end
-  
-  describe &quot;GET /preview&quot; do    
-    it &quot;exposes a post initialised from the cache as @post&quot; do
-      session[:post_preview] = mock_post
-      get :preview
-      assigns(:post).should equal(mock_post)
-    end
-  
-    it &quot;should clear the session&quot; do
-      session[:post_preview] = mock_post
-      get :preview
-      session[&quot;post_preview&quot;].should == nil
-    end
-  
-    it &quot;should render the 'posts/show' template&quot; do
-      session[:post_preview] = mock_post
-      get :preview
-      response.should render_template('posts/show')
-    end   
   end     
   
-  describe &quot;GET /id/preview&quot; do
+  describe &quot;GET /:id/preview&quot; do
     it &quot;use the id to init the post&quot; do
-      Post.stub!(:get).and_return(mock_post)
+      Post.stub!(:find_by_permalink).and_return(mock_post(:preview =&gt; nil))
       get :preview, :id =&gt; 'foo'
       assigns(:post).should equal(mock_post)
     end
   
     it &quot;should render the 'posts/show' template&quot; do
-      Post.stub!(:get).and_return(mock_post)
+      Post.stub!(:find_by_permalink).and_return(mock_post(:preview =&gt; nil))
       get :preview, :id =&gt; 'foo'
       response.should render_template('posts/show')
-    end    
-  end
-  
-  describe &quot;POST post_preview&quot; do
-    
-    it &quot;exposes a newly created post as @post&quot; do
-      Post.should_receive(:new).with({:title =&gt; 'foo', :body =&gt; 'bar'}).and_return(mock_post)
-      post :post_preview, :post_title =&gt; 'foo', :post_body =&gt; 'bar'
-      assigns(:post).should equal(mock_post)
     end
     
-    it &quot;should render the 'posts/show' template&quot; do
-      Post.stub!(:new).and_return(mock_post)
-      post :post_preview, :post =&gt; {}
-      response.should render_template('posts/show')
+    it &quot;should mark the post as a preview&quot; do
+      Post.stub!(:find_by_permalink).and_return(mock_post(:preview =&gt; nil))
+      get :preview, :id =&gt; 'foo'
+      assigns(:post_preview).should equal(true)
     end
+    
+    it &quot;should set the body as the preview if its there&quot; do
+      Post.stub!(:find_by_permalink).and_return(@foo = mock_post(:preview =&gt; 'foo preview'))
+      @foo.should_receive(:body=).with(&quot;foo preview&quot;)
+      get :preview, :id =&gt; 'foo'
+      
+    end  
   end
 
   describe &quot;DELETE destroy&quot; do</diff>
      <filename>spec/controllers/admin/posts_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -34,27 +34,27 @@ describe PostsController do
   
   describe &quot;GET 'show'&quot; do
     it &quot;should be successful&quot; do
-      Post.stub!(:get).and_return(mock_post(:published? =&gt; true, :verify_date? =&gt; true))
-      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :id =&gt; &quot;a-title&quot;
+      Post.stub!(:find_by_permalink).and_return(mock_post(:published? =&gt; true, :verify_date? =&gt; true))
+      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :permalink =&gt; &quot;a-title&quot;
       response.should be_success
     end
     
     it &quot;should assign post as @post&quot; do
-      Post.stub!(:get).and_return(mock_post(:published? =&gt; true, :verify_date? =&gt; true))
-      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :id =&gt; &quot;a-title&quot;
+      Post.stub!(:find_by_permalink).and_return(mock_post(:published? =&gt; true, :verify_date? =&gt; true))
+      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :permalink =&gt; &quot;a-title&quot;
       assigns[:post].should == mock_post
     end
     
     it &quot;should redirect unpublished posts to the 404 page&quot; do
-      Post.stub!(:get).and_return(mock_post(:published? =&gt; false, :verify_date? =&gt; true))
-      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :id =&gt; &quot;a-title&quot;
+      Post.stub!(:find_by_permalink).and_return(mock_post(:published? =&gt; false, :verify_date? =&gt; true))
+      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :permalink =&gt; &quot;a-title&quot;
       response.should redirect_to('/404')
     end
     
     # we really want to index and cache the correct url
     it &quot;should redirect posts with incorrect dates to 404&quot; do
-      Post.stub!(:get).and_return(mock_post(:published? =&gt; true, :verify_date? =&gt; false))
-      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :id =&gt; &quot;a-title&quot;
+      Post.stub!(:find_by_permalink).and_return(mock_post(:published? =&gt; true, :verify_date? =&gt; false))
+      get :show, :year =&gt; &quot;2009&quot;, :month =&gt; &quot;06&quot;, :permalink =&gt; &quot;a-title&quot;
       response.should redirect_to('/404')
     end
   </diff>
      <filename>spec/controllers/posts_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -52,20 +52,12 @@ describe Post do
     @new.permalink.should == &quot;only-unique-permalinks-times-3&quot;
   end
   
-  it &quot;should update the id when title is changed&quot; do
-    @post = Post.create!(:title =&gt; &quot;title one&quot;, :body =&gt; &quot;The best blog post in the world&quot;)
-    @post.title = &quot;title two&quot;
-    @post.save!
-    
-    Post.get(&quot;title-two&quot;).should_not == nil
-  end
-  
   it &quot;should update the permalink when title is changed&quot; do
     @post = Post.create!(:title =&gt; &quot;title one&quot;, :body =&gt; &quot;The best blog post in the world&quot;)
     @post.title = &quot;title two&quot;
     @post.save!
     
-    @foo = Post.get(&quot;title-two&quot;)
+    @foo = Post.get(@post.id)
     @foo.permalink.should == &quot;title-two&quot;
   end
   
@@ -76,14 +68,14 @@ describe Post do
     Post.get(&quot;title-one&quot;).should == nil
   end
   
-  describe &quot;exists? method&quot; do
-    it &quot;should return true if document exists&quot; do
+  describe &quot;permalink_unique? method&quot; do
+    it &quot;should return false if permalink is being used&quot; do
       @post.save!
-      Post.exists?(@post.id).should == true
+      Post.permalink_unique?(@post.permalink).should == false
     end
     
-    it &quot;should return false when document no in db&quot; do
-      Post.exists?(&quot;random-id-that-does-not-exist&quot;).should == false
+    it &quot;should return true if permalink is not in use&quot; do
+      Post.permalink_unique?(&quot;random-id-that-does-not-exist&quot;).should == true
     end
   end
   
@@ -157,5 +149,31 @@ describe Post do
       @posts = Post.find_by_year_and_month('2009', '06')
       @posts.size.should == 1
     end
+    
+    it &quot;should find a post by its permalink&quot; do
+      @post = Post.find_by_permalink('title-one')
+      @post.should_not == nil
+    end
+  end
+  
+  
+  describe &quot;.new_from_params&quot; do
+  
+    it &quot;should set the post status from the params&quot; do
+      @post = Post.new_from_params({:post=&gt;{:body=&gt;&quot;foo bar&quot;, :title=&gt;&quot;testing&quot;}, :preview=&gt;&quot;Preview&quot;})
+      @post.status.should == &quot;preview&quot;
+    end
+    
+    it &quot;should store the preview&quot; do
+      @post = Post.new_from_params({:post=&gt;{:body=&gt;&quot;foo bar&quot;, :title=&gt;&quot;testing&quot;}, :preview=&gt;&quot;Preview&quot;})
+      @post.preview.should == nil 
+    end
+  
+  end
+  
+  it &quot;should be able to override the updated_at value&quot; do
+    @post = Post.create!(:title =&gt; &quot;title one&quot;, :body =&gt; &quot;The best blog post in the world&quot;)
+    @post.set_updated_at(&quot;2009/06/24 14:10:27&quot;)
+    @post.updated_at.should == Time.parse(&quot;2009/06/24 14:10:27&quot;)
   end
 end
\ No newline at end of file</diff>
      <filename>spec/models/post_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -8,7 +8,7 @@ describe PostsController do
     end
   
     it &quot;should map show&quot; do
-      route_for(:controller =&gt; &quot;posts&quot;, :action =&gt; &quot;show&quot;, :id =&gt; &quot;a-title&quot;, :month =&gt; &quot;01&quot;, :year =&gt; &quot;2009&quot;).should == &quot;/2009/01/a-title&quot;
+      route_for(:controller =&gt; &quot;posts&quot;, :action =&gt; &quot;show&quot;, :permalink =&gt; &quot;a-title&quot;, :month =&gt; &quot;01&quot;, :year =&gt; &quot;2009&quot;).should == &quot;/2009/01/a-title&quot;
     end
     
     it &quot;should map by_year&quot; do
@@ -26,7 +26,7 @@ describe PostsController do
   
   describe &quot;route recognition&quot; do
     it &quot;generates params for #show&quot; do
-      params_from(:get, &quot;/2009/01/a-title&quot;).should == {:controller =&gt; &quot;posts&quot;, :action =&gt; &quot;show&quot;, :id =&gt; &quot;a-title&quot;, :month =&gt; &quot;01&quot;, :year =&gt; &quot;2009&quot;}
+      params_from(:get, &quot;/2009/01/a-title&quot;).should == {:controller =&gt; &quot;posts&quot;, :action =&gt; &quot;show&quot;, :permalink =&gt; &quot;a-title&quot;, :month =&gt; &quot;01&quot;, :year =&gt; &quot;2009&quot;}
     end
     
     it &quot;generates params for #by_year&quot; do</diff>
      <filename>spec/routing/posts_routing_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array">
    <removed>
      <filename>app/views/shared/_nav.erb.html</filename>
    </removed>
  </removed>
  <parents type="array">
    <parent>
      <id>e90d3c82eb04f9c16467c2887e69661546c00061</id>
    </parent>
  </parents>
  <author>
    <name>Ben Aldred</name>
    <email>benaldred@gmail.com</email>
  </author>
  <url>http://github.com/benaldred/soapbox/commit/f81a1b09e04be0d34b0d9172f0ee3404465eca6b</url>
  <id>f81a1b09e04be0d34b0d9172f0ee3404465eca6b</id>
  <committed-date>2009-09-30T06:26:52-07:00</committed-date>
  <authored-date>2009-09-30T06:26:52-07:00</authored-date>
  <message>refactored a lot</message>
  <tree>618c395dab123ea678cf911e1b77f7fbbf065002</tree>
  <committer>
    <name>Ben Aldred</name>
    <email>benaldred@gmail.com</email>
  </committer>
</commit>
