Skip to content

Commit 19259ed

Browse files
author
Christian Kruse
committed
FEAT: moved sorting from the model to a helper
1 parent 158295c commit 19259ed

10 files changed

+41
-24
lines changed

app/controllers/application_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ApplicationController < ActionController::Base
1616
include FayeHelper
1717
include MessageHelper
1818
include SortablesHelper
19+
include SortingHelper
1920

2021
before_filter :do_init, :locked?, :set_forums, :notifications,
2122
:run_before_handler, :check_authorizations

app/controllers/cf_forums_controller.rb

+2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ def index
5353

5454
def redirect_archive
5555
thread = CfThread.find_by_tid(params[:tid][1..-1].to_i)
56+
5657
if thread
58+
sort_thread(thread)
5759
redirect_to cf_message_url(thread, thread.message), status: 301
5860
else
5961
raise CForum::NotFoundException.new # TODO: add message

app/controllers/cf_threads_controller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ def create
158158
def moving
159159
@id = CfThread.make_id(params)
160160
@thread = CfThread.includes(:forum).where(slug: @id).first!
161+
@thread.gen_tree
161162

162163
if current_user.admin
163164
@forums = CfForum.order('name ASC')

app/helpers/rights_helper.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def get_thread
6060
raise CForum::NotFoundException.new if thread.blank?
6161

6262
# sort messages
63-
thread.message
63+
sort_thread(thread)
6464

6565
return thread, id
6666
end

app/helpers/sorting_helper.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding: utf-8 -*-
2+
3+
module SortingHelper
4+
def sort_thread(thread, message = nil, direction = nil)
5+
direction = uconf('sort_messages', 'ascending') if direction.blank?
6+
7+
if message.blank?
8+
thread.gen_tree
9+
message = thread.sorted_messages[0]
10+
end
11+
12+
unless message.messages.blank?
13+
if direction == 'ascending'
14+
message.messages.sort! { |a,b| a.created_at <=> b.created_at }
15+
else
16+
message.messages.sort! { |a,b| b.created_at <=> a.created_at }
17+
end
18+
19+
message.messages.each do |m|
20+
sort_thread(thread, m, direction)
21+
end
22+
end
23+
end
24+
end
25+
26+
# eof

app/helpers/threads_helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ def index_threads
7878
@threads = b.call(@threads)
7979
end
8080

81+
@threads.each do |t|
82+
sort_thread(t)
83+
end
84+
8185
@threads
8286
end
8387
end

app/models/cf_thread.rb

-22
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,6 @@ def gen_tree
8282
end
8383
end
8484

85-
def sort_tree(msg = nil)
86-
msg = message if msg.nil?
87-
88-
unless msg.messages.blank?
89-
msg.messages.sort! {|a,b| b.created_at <=> a.created_at }
90-
91-
msg.messages.each do |m|
92-
sort_tree(m)
93-
end
94-
end
95-
end
96-
9785
def self.gen_id(thread, num = nil)
9886
now = thread.message.created_at
9987
now = Time.now if now.nil?
@@ -124,16 +112,6 @@ def self.make_id(year, mon = nil, day = nil, tid = nil)
124112
self.flags ||= {} if attributes.has_key? 'flags'
125113
end
126114

127-
def sorted_messages
128-
if not @generated
129-
@generated = true
130-
gen_tree
131-
sort_tree
132-
end
133-
134-
@sorted_messages
135-
end
136-
137115
def acceptance_forbidden?(usr, uuid)
138116
forbidden = false
139117

lib/async/notify_new_task.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def perform_message(args)
5656
@sent_mails = {}
5757
@notified = {}
5858

59-
@thread.sorted_messages.each do |m|
59+
@thread.messages.each do |m|
6060
Rails.logger.debug "notify new task: perform_message: owner: " + m.owner.inspect
6161

6262
if check_notify(m.owner, @thread, @message, @parent)

test/unit/cf_message_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CfMessageTest < ActiveSupport::TestCase
7474
m1 = FactoryGirl.create(:cf_message, forum: m.forum, thread: m.thread, parent_id: m.message_id)
7575

7676
t = CfThread.preload(:messages).find(m.thread.thread_id)
77+
t.gen_tree
7778

7879
t.sorted_messages[0].delete_with_subtree
7980
assert m.reload.deleted

test/unit/cf_thread_test.rb

+4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ class CfThreadTest < ActiveSupport::TestCase
3535
m = FactoryGirl.create(:cf_message, owner: nil, forum: t.forum, thread: t)
3636

3737
t = CfThread.find t.thread_id
38+
t.gen_tree
3839
assert_equal 1, t.sorted_messages.count()
3940

4041
m1 = FactoryGirl.create(:cf_message, owner: nil, forum: t.forum, thread: t, parent_id: m.message_id)
4142

4243
t = CfThread.includes(:messages).find t.thread_id
44+
t.gen_tree
4345
assert_equal 2, t.sorted_messages.count()
4446
assert_equal m.message_id, t.message.message_id
4547
assert_equal m1.message_id, t.sorted_messages[0].messages[0].message_id
@@ -89,6 +91,7 @@ class CfThreadTest < ActiveSupport::TestCase
8991

9092
test "acceptance_forbidden?" do
9193
msg = FactoryGirl.create(:cf_message)
94+
msg.thread.gen_tree
9295

9396
assert msg.thread.acceptance_forbidden?(nil, nil)
9497
assert msg.thread.acceptance_forbidden?('', '')
@@ -103,6 +106,7 @@ class CfThreadTest < ActiveSupport::TestCase
103106
msg.uuid = '1234'
104107
msg.save
105108
msg.reload
109+
msg.thread.gen_tree
106110

107111
assert !msg.thread.acceptance_forbidden?(nil, '1234')
108112
assert msg.thread.acceptance_forbidden?(nil, '12345')

0 commit comments

Comments
 (0)