public
Description: A fully featured forum system compatible with Rails 2.3
Homepage: http://forum.frozenplague.net
Clone URL: git://github.com/radar/rboard.git
Click here to lend your support to: rboard and make a donation at www.pledgie.com !
Topic moving now correctly works, for real!
radar (author)
Wed Aug 13 20:44:53 -0700 2008
commit  8872174c29284cfdab1b4eea1a88aa2d0cbdaf6b
tree    13f875ac1e1142831438eeac75a8d46ef8d6b457
parent  3a0e632e9f224edb8198557c25f820539eacafd8
...
10
11
12
13
 
14
...
10
11
12
 
13
14
0
@@ -10,5 +10,5 @@ nbproject/project.*
0
 test
0
 coverage
0
 coverage/*
0
-
0
+config/*.sphinx.conf
0
 
...
17
18
19
 
20
21
22
...
17
18
19
20
21
22
23
0
@@ -17,6 +17,7 @@ class PostsController < ApplicationController
0
     @posts = @topic.posts.find(:all, :order => "id DESC", :limit => 10)
0
     @post = @topic.posts.build(params[:post].merge!(:user => current_user))
0
     if @post.save
0
+      @topic.update_attribute("last_post_id", @post.id)
0
       page = (@topic.posts.size.to_f / 30).ceil
0
       flash[:notice] = "Post has been created."
0
       redirect_to forum_topic_path(@post.forum,@topic, :page => page)
...
24
25
26
 
27
28
29
...
24
25
26
27
28
29
30
0
@@ -24,6 +24,7 @@ class TopicsController < ApplicationController
0
     @post = @topic.posts.build(params[:post].merge(:user_id => current_user.id))
0
     @topic.sticky = true if params[:topic][:sticky] == 1 && current_user.admin?
0
     if @topic.save
0
+      @topic.update_attribute("last_post_id", @post.id)
0
       flash[:notice] = "Topic has been created."
0
       redirect_to forum_topic_path(@topic.forum, @topic)
0
     else
...
17
18
19
20
21
22
23
24
25
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
32
33
...
17
18
19
 
 
 
 
 
 
 
 
 
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
37
38
39
0
@@ -17,17 +17,23 @@ class Forum < ActiveRecord::Base
0
   #There's no easy way to do it
0
   alias_method :old_topics, :topics
0
   
0
-  #NOT TESTED
0
-  #POTENTIALLY UNSTABLE
0
-  def update_last_post
0
-    if posts.first.nil?
0
-      self.last_post = nil
0
-      self.last_post_forum = nil
0
-    else
0
-      self.last_post = posts.first
0
-      self.last_post_forum = posts.first.forum if posts.first.forum != self
0
+  def update_last_post(new_forum, post=nil)
0
+    post ||= posts.last
0
+    self.last_post = post
0
+    self.last_post_forum = nil
0
+    self.save!
0
+    for ancestor in (ancestors - [new_forum])
0
+      if !post.nil?
0
+        if ancestor.last_post.nil? || (ancestor.last_post.created_at < post.created_at)
0
+          ancestor.last_post = post
0
+          ancestor.last_post_forum = self
0
+        end
0
+      else
0
+        ancestor.last_post = nil
0
+        ancestor.last_post_forum = nil
0
+      end
0
+      ancestor.save
0
     end
0
-    save
0
   end
0
   
0
   def topics
...
3
4
5
 
6
7
8
...
19
20
21
 
22
23
24
 
 
25
26
 
27
28
29
...
3
4
5
6
7
8
9
...
20
21
22
23
24
25
 
26
27
28
 
29
30
31
32
0
@@ -3,6 +3,7 @@ class Topic < ActiveRecord::Base
0
   belongs_to :forum
0
   has_many :posts, :dependent => :destroy, :order => "created_at asc"
0
   has_many :users, :through => :posts
0
+  belongs_to :last_post, :class_name => "Post"
0
   
0
   #makes error_messages_for return the wrong number of errors.
0
   validates_associated :posts, :message => nil
0
@@ -19,11 +20,13 @@ class Topic < ActiveRecord::Base
0
   
0
   def move!(new_forum_id)
0
     old_forum = Forum.find(forum_id)
0
+    was_old_last_post = old_forum.last_post == self.last_post
0
     new_forum = Forum.find(new_forum_id)
0
     update_attribute("forum_id", new_forum_id)
0
-    posts.last.update_forum if posts.last != new_forum.posts.last
0
+    is_new_last_post = new_forum.last_post.nil? || (new_forum.last_post.created_at <= posts.last.created_at)
0
+    new_forum.update_last_post(new_forum, posts.last) if is_new_last_post
0
     old_forum.reload
0
-    old_forum.update_last_post
0
+    old_forum.update_last_post(new_forum) if was_old_last_post
0
   end
0
   
0
   def lock!
...
45
46
47
48
 
49
50
51
...
45
46
47
 
48
49
50
51
0
@@ -45,7 +45,7 @@ describe ForumsController do
0
   
0
   it "should show the admin forum to the administrator" do
0
     login_as(:administrator)
0
-    Forum.should_receive(:find).twice.and_return(@forum)
0
+    Forum.should_receive(:find).twice.and_return(@forum, @forums)
0
     @forum.should_receive(:viewable?).and_return(true)
0
     @forum.should_receive(:topics).and_return(@topics)
0
     @topics.should_receive(:paginate).and_return(@topics)
...
68
69
70
 
71
72
73
...
68
69
70
71
72
73
74
0
@@ -68,6 +68,7 @@ describe PostsController, "as plebian" do
0
     @post.should_receive(:save).and_return(true)
0
     @post.should_receive(:forum).twice.and_return(@forum)
0
     @post.should_receive(:topic).and_return(@topic)
0
+    @topic.should_receive(:update_attribute).with("last_post_id", @post.id).and_return(true)
0
     post 'create', {:post => { :text => "This is a new post" }, :topic_id => topics(:user).id }
0
     flash[:notice].should eql("Post has been created.")
0
     response.should redirect_to(forum_topic_path(@post.forum, @post.topic, :page => 1))
...
105
106
107
 
108
109
110
...
105
106
107
108
109
110
111
0
@@ -105,6 +105,7 @@ describe TopicsController do
0
       @posts.should_receive(:build).and_return(@post)
0
       @topic.should_receive(:forum).and_return(@forum)
0
       @topic.should_receive(:save).and_return(true)
0
+      @topic.should_receive(:update_attribute).with("last_post_id", @post.id).and_return(true)
0
       post 'create', { :topic => { :subject => "Subject"}, :post => { :text => "New text!"}, :forum_id => forums(:admins_only).id }
0
       flash[:notice].should eql("Topic has been created.")
0
       #TODO: Test for redirect

Comments