Skip to content

Commit ff9b258

Browse files
committed
added message tree sorting
1 parent 89bd8e3 commit ff9b258

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

app/controllers/messages_controller.rb

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class MessagesController < ApplicationController
22
def show
33
@id = '/' + params[:year] + '/' + params[:mon] + '/' + params[:day] + '/' + params[:tid]
44
@thread = CfThread.find_by_id(@id)
5+
@thread.sort_tree
56

67
@message = @thread.find_message(params[:mid]) if @thread
78
raise CForum::NotFoundException.new if @thread.nil? or @message.nil?
@@ -10,6 +11,8 @@ def show
1011
def new
1112
@id = '/' + params[:year] + '/' + params[:mon] + '/' + params[:day] + '/' + params[:tid]
1213
@thread = CfThread.find_by_id(@id)
14+
@thread.sort_tree
15+
1316
@parent = @thread.find_message(params[:mid]) if @thread
1417

1518
raise CForum::NotFoundException.new if @thread.nil? or @parent.nil?
@@ -22,6 +25,8 @@ def new
2225
def create
2326
@id = '/' + params[:year] + '/' + params[:mon] + '/' + params[:day] + '/' + params[:tid]
2427
@thread = CfThread.find_by_id(@id)
28+
@thread.sort_tree
29+
2530
@parent = @thread.find_message(params[:mid]) if @thread
2631

2732
raise CForum::NotFoundException.new if @thread.nil? or @parent.nil?

app/controllers/threads_controller.rb

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ def index
2626
@threads = CfThread.order('message.created_at' => -1).limit(ConfigManager.setting('pagination') || 10)
2727
end
2828

29+
@threads.each do |t|
30+
t.sort_tree
31+
end
32+
2933
notification_center.notify(SHOW_THREADLIST, @threads)
3034
end
3135

app/models/cf_thread.rb

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ def find_message(mid, msg = nil)
2121

2222
nil
2323
end
24+
25+
def sort_tree(msg = nil)
26+
msg = message if msg.nil?
27+
28+
unless msg.messages.blank?
29+
msg.messages.sort! {|a,b| b.created_at <=> a.created_at }
30+
31+
msg.messages.each do |m|
32+
sort_tree(m)
33+
end
34+
end
35+
end
36+
2437
end
2538

2639
# eof

doc/TODO

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ TODO
22

33
- avoid duplicates when posting new messages
44
- avoid duplicates when posting new threads
5-
- sort messages
65

76
- use scope for archived/not archived instead of querying in the controller index
87

0 commit comments

Comments
 (0)