<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -10,5 +10,5 @@ nbproject/project.*
 test
 coverage
 coverage/*
-
+config/*.sphinx.conf
 </diff>
      <filename>.gitignore</filename>
    </modified>
    <modified>
      <diff>@@ -17,6 +17,7 @@ class PostsController &lt; ApplicationController
     @posts = @topic.posts.find(:all, :order =&gt; &quot;id DESC&quot;, :limit =&gt; 10)
     @post = @topic.posts.build(params[:post].merge!(:user =&gt; current_user))
     if @post.save
+      @topic.update_attribute(&quot;last_post_id&quot;, @post.id)
       page = (@topic.posts.size.to_f / 30).ceil
       flash[:notice] = &quot;Post has been created.&quot;
       redirect_to forum_topic_path(@post.forum,@topic, :page =&gt; page)</diff>
      <filename>app/controllers/posts_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -24,6 +24,7 @@ class TopicsController &lt; ApplicationController
     @post = @topic.posts.build(params[:post].merge(:user_id =&gt; current_user.id))
     @topic.sticky = true if params[:topic][:sticky] == 1 &amp;&amp; current_user.admin?
     if @topic.save
+      @topic.update_attribute(&quot;last_post_id&quot;, @post.id)
       flash[:notice] = &quot;Topic has been created.&quot;
       redirect_to forum_topic_path(@topic.forum, @topic)
     else</diff>
      <filename>app/controllers/topics_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -17,17 +17,23 @@ class Forum &lt; ActiveRecord::Base
   #There's no easy way to do it
   alias_method :old_topics, :topics
   
-  #NOT TESTED
-  #POTENTIALLY UNSTABLE
-  def update_last_post
-    if posts.first.nil?
-      self.last_post = nil
-      self.last_post_forum = nil
-    else
-      self.last_post = posts.first
-      self.last_post_forum = posts.first.forum if posts.first.forum != self
+  def update_last_post(new_forum, post=nil)
+    post ||= posts.last
+    self.last_post = post
+    self.last_post_forum = nil
+    self.save!
+    for ancestor in (ancestors - [new_forum])
+      if !post.nil?
+        if ancestor.last_post.nil? || (ancestor.last_post.created_at &lt; post.created_at)
+          ancestor.last_post = post
+          ancestor.last_post_forum = self
+        end
+      else
+        ancestor.last_post = nil
+        ancestor.last_post_forum = nil
+      end
+      ancestor.save
     end
-    save
   end
   
   def topics</diff>
      <filename>app/models/forum.rb</filename>
    </modified>
    <modified>
      <diff>@@ -3,6 +3,7 @@ class Topic &lt; ActiveRecord::Base
   belongs_to :forum
   has_many :posts, :dependent =&gt; :destroy, :order =&gt; &quot;created_at asc&quot;
   has_many :users, :through =&gt; :posts
+  belongs_to :last_post, :class_name =&gt; &quot;Post&quot;
   
   #makes error_messages_for return the wrong number of errors.
   validates_associated :posts, :message =&gt; nil
@@ -19,11 +20,13 @@ class Topic &lt; ActiveRecord::Base
   
   def move!(new_forum_id)
     old_forum = Forum.find(forum_id)
+    was_old_last_post = old_forum.last_post == self.last_post
     new_forum = Forum.find(new_forum_id)
     update_attribute(&quot;forum_id&quot;, new_forum_id)
-    posts.last.update_forum if posts.last != new_forum.posts.last
+    is_new_last_post = new_forum.last_post.nil? || (new_forum.last_post.created_at &lt;= posts.last.created_at)
+    new_forum.update_last_post(new_forum, posts.last) if is_new_last_post
     old_forum.reload
-    old_forum.update_last_post
+    old_forum.update_last_post(new_forum) if was_old_last_post
   end
   
   def lock!</diff>
      <filename>app/models/topic.rb</filename>
    </modified>
    <modified>
      <diff>@@ -45,7 +45,7 @@ describe ForumsController do
   
   it &quot;should show the admin forum to the administrator&quot; do
     login_as(:administrator)
-    Forum.should_receive(:find).twice.and_return(@forum)
+    Forum.should_receive(:find).twice.and_return(@forum, @forums)
     @forum.should_receive(:viewable?).and_return(true)
     @forum.should_receive(:topics).and_return(@topics)
     @topics.should_receive(:paginate).and_return(@topics)</diff>
      <filename>spec/controllers/forums_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -68,6 +68,7 @@ describe PostsController, &quot;as plebian&quot; do
     @post.should_receive(:save).and_return(true)
     @post.should_receive(:forum).twice.and_return(@forum)
     @post.should_receive(:topic).and_return(@topic)
+    @topic.should_receive(:update_attribute).with(&quot;last_post_id&quot;, @post.id).and_return(true)
     post 'create', {:post =&gt; { :text =&gt; &quot;This is a new post&quot; }, :topic_id =&gt; topics(:user).id }
     flash[:notice].should eql(&quot;Post has been created.&quot;)
     response.should redirect_to(forum_topic_path(@post.forum, @post.topic, :page =&gt; 1))</diff>
      <filename>spec/controllers/posts_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -105,6 +105,7 @@ describe TopicsController do
       @posts.should_receive(:build).and_return(@post)
       @topic.should_receive(:forum).and_return(@forum)
       @topic.should_receive(:save).and_return(true)
+      @topic.should_receive(:update_attribute).with(&quot;last_post_id&quot;, @post.id).and_return(true)
       post 'create', { :topic =&gt; { :subject =&gt; &quot;Subject&quot;}, :post =&gt; { :text =&gt; &quot;New text!&quot;}, :forum_id =&gt; forums(:admins_only).id }
       flash[:notice].should eql(&quot;Topic has been created.&quot;)
       #TODO: Test for redirect</diff>
      <filename>spec/controllers/topics_controller_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>3a0e632e9f224edb8198557c25f820539eacafd8</id>
    </parent>
  </parents>
  <author>
    <name>Ryan Bigg</name>
    <email>radarlistener@gmail.com</email>
  </author>
  <url>http://github.com/radar/rboard/commit/8872174c29284cfdab1b4eea1a88aa2d0cbdaf6b</url>
  <id>8872174c29284cfdab1b4eea1a88aa2d0cbdaf6b</id>
  <committed-date>2008-08-13T20:44:53-07:00</committed-date>
  <authored-date>2008-08-13T20:44:53-07:00</authored-date>
  <message>Topic moving now correctly works, for real!</message>
  <tree>13f875ac1e1142831438eeac75a8d46ef8d6b457</tree>
  <committer>
    <name>Ryan Bigg</name>
    <email>radarlistener@gmail.com</email>
  </committer>
</commit>
