##// END OF EJS Templates
Fixed that messages are not sorted by last reply (#12243)....
Jean-Philippe Lang -
r11194:c17ec1643c00
parent child
Show More
@@ -1,110 +1,111
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class BoardsController < ApplicationController
18 class BoardsController < ApplicationController
19 default_search_scope :messages
19 default_search_scope :messages
20 before_filter :find_project_by_project_id, :find_board_if_available, :authorize
20 before_filter :find_project_by_project_id, :find_board_if_available, :authorize
21 accept_rss_auth :index, :show
21 accept_rss_auth :index, :show
22
22
23 helper :sort
23 helper :sort
24 include SortHelper
24 include SortHelper
25 helper :watchers
25 helper :watchers
26
26
27 def index
27 def index
28 @boards = @project.boards.includes(:last_message => :author).all
28 @boards = @project.boards.includes(:last_message => :author).all
29 # show the board if there is only one
29 # show the board if there is only one
30 if @boards.size == 1
30 if @boards.size == 1
31 @board = @boards.first
31 @board = @boards.first
32 show
32 show
33 end
33 end
34 end
34 end
35
35
36 def show
36 def show
37 respond_to do |format|
37 respond_to do |format|
38 format.html {
38 format.html {
39 sort_init 'updated_on', 'desc'
39 sort_init 'updated_on', 'desc'
40 sort_update 'created_on' => "#{Message.table_name}.created_on",
40 sort_update 'created_on' => "#{Message.table_name}.created_on",
41 'replies' => "#{Message.table_name}.replies_count",
41 'replies' => "#{Message.table_name}.replies_count",
42 'updated_on' => "#{Message.table_name}.updated_on"
42 'updated_on' => "COALESCE(last_replies_messages.created_on, #{Message.table_name}.created_on)"
43
43
44 @topic_count = @board.topics.count
44 @topic_count = @board.topics.count
45 @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
45 @topic_pages = Paginator.new @topic_count, per_page_option, params['page']
46 @topics = @board.topics.
46 @topics = @board.topics.
47 reorder("#{Message.table_name}.sticky DESC").
47 reorder("#{Message.table_name}.sticky DESC").
48 includes(:author, {:last_reply => :author}).
48 includes(:last_reply).
49 limit(@topic_pages.items_per_page).
49 limit(@topic_pages.items_per_page).
50 offset(@topic_pages.offset).
50 offset(@topic_pages.offset).
51 order(sort_clause).
51 order(sort_clause).
52 preload(:author, {:last_reply => :author}).
52 all
53 all
53 @message = Message.new(:board => @board)
54 @message = Message.new(:board => @board)
54 render :action => 'show', :layout => !request.xhr?
55 render :action => 'show', :layout => !request.xhr?
55 }
56 }
56 format.atom {
57 format.atom {
57 @messages = @board.messages.
58 @messages = @board.messages.
58 reorder('created_on DESC').
59 reorder('created_on DESC').
59 includes(:author, :board).
60 includes(:author, :board).
60 limit(Setting.feeds_limit.to_i).
61 limit(Setting.feeds_limit.to_i).
61 all
62 all
62 render_feed(@messages, :title => "#{@project}: #{@board}")
63 render_feed(@messages, :title => "#{@project}: #{@board}")
63 }
64 }
64 end
65 end
65 end
66 end
66
67
67 def new
68 def new
68 @board = @project.boards.build
69 @board = @project.boards.build
69 @board.safe_attributes = params[:board]
70 @board.safe_attributes = params[:board]
70 end
71 end
71
72
72 def create
73 def create
73 @board = @project.boards.build
74 @board = @project.boards.build
74 @board.safe_attributes = params[:board]
75 @board.safe_attributes = params[:board]
75 if @board.save
76 if @board.save
76 flash[:notice] = l(:notice_successful_create)
77 flash[:notice] = l(:notice_successful_create)
77 redirect_to_settings_in_projects
78 redirect_to_settings_in_projects
78 else
79 else
79 render :action => 'new'
80 render :action => 'new'
80 end
81 end
81 end
82 end
82
83
83 def edit
84 def edit
84 end
85 end
85
86
86 def update
87 def update
87 @board.safe_attributes = params[:board]
88 @board.safe_attributes = params[:board]
88 if @board.save
89 if @board.save
89 redirect_to_settings_in_projects
90 redirect_to_settings_in_projects
90 else
91 else
91 render :action => 'edit'
92 render :action => 'edit'
92 end
93 end
93 end
94 end
94
95
95 def destroy
96 def destroy
96 @board.destroy
97 @board.destroy
97 redirect_to_settings_in_projects
98 redirect_to_settings_in_projects
98 end
99 end
99
100
100 private
101 private
101 def redirect_to_settings_in_projects
102 def redirect_to_settings_in_projects
102 redirect_to settings_project_path(@project, :tab => 'boards')
103 redirect_to settings_project_path(@project, :tab => 'boards')
103 end
104 end
104
105
105 def find_board_if_available
106 def find_board_if_available
106 @board = @project.boards.find(params[:id]) if params[:id]
107 @board = @project.boards.find(params[:id]) if params[:id]
107 rescue ActiveRecord::RecordNotFound
108 rescue ActiveRecord::RecordNotFound
108 render_404
109 render_404
109 end
110 end
110 end
111 end
@@ -1,202 +1,217
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class BoardsControllerTest < ActionController::TestCase
20 class BoardsControllerTest < ActionController::TestCase
21 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
21 fixtures :projects, :users, :members, :member_roles, :roles, :boards, :messages, :enabled_modules
22
22
23 def setup
23 def setup
24 User.current = nil
24 User.current = nil
25 end
25 end
26
26
27 def test_index
27 def test_index
28 get :index, :project_id => 1
28 get :index, :project_id => 1
29 assert_response :success
29 assert_response :success
30 assert_template 'index'
30 assert_template 'index'
31 assert_not_nil assigns(:boards)
31 assert_not_nil assigns(:boards)
32 assert_not_nil assigns(:project)
32 assert_not_nil assigns(:project)
33 end
33 end
34
34
35 def test_index_not_found
35 def test_index_not_found
36 get :index, :project_id => 97
36 get :index, :project_id => 97
37 assert_response 404
37 assert_response 404
38 end
38 end
39
39
40 def test_index_should_show_messages_if_only_one_board
40 def test_index_should_show_messages_if_only_one_board
41 Project.find(1).boards.slice(1..-1).each(&:destroy)
41 Project.find(1).boards.slice(1..-1).each(&:destroy)
42
42
43 get :index, :project_id => 1
43 get :index, :project_id => 1
44 assert_response :success
44 assert_response :success
45 assert_template 'show'
45 assert_template 'show'
46 assert_not_nil assigns(:topics)
46 assert_not_nil assigns(:topics)
47 end
47 end
48
48
49 def test_show
49 def test_show
50 get :show, :project_id => 1, :id => 1
50 get :show, :project_id => 1, :id => 1
51 assert_response :success
51 assert_response :success
52 assert_template 'show'
52 assert_template 'show'
53 assert_not_nil assigns(:board)
53 assert_not_nil assigns(:board)
54 assert_not_nil assigns(:project)
54 assert_not_nil assigns(:project)
55 assert_not_nil assigns(:topics)
55 assert_not_nil assigns(:topics)
56 end
56 end
57
57
58 def test_show_should_display_sticky_messages_first
58 def test_show_should_display_sticky_messages_first
59 Message.update_all(:sticky => 0)
59 Message.update_all(:sticky => 0)
60 Message.update_all({:sticky => 1}, {:id => 1})
60 Message.update_all({:sticky => 1}, {:id => 1})
61
61
62 get :show, :project_id => 1, :id => 1
62 get :show, :project_id => 1, :id => 1
63 assert_response :success
63 assert_response :success
64
64
65 topics = assigns(:topics)
65 topics = assigns(:topics)
66 assert_not_nil topics
66 assert_not_nil topics
67 assert topics.size > 1, "topics size was #{topics.size}"
67 assert topics.size > 1, "topics size was #{topics.size}"
68 assert topics.first.sticky?
68 assert topics.first.sticky?
69 assert topics.first.updated_on < topics.second.updated_on
69 assert topics.first.updated_on < topics.second.updated_on
70 end
70 end
71
71
72 def test_show_should_display_message_with_last_reply_first
73 Message.update_all(:sticky => 0)
74
75 # Reply to an old topic
76 old_topic = Message.where(:board_id => 1, :parent_id => nil).order('created_on ASC').first
77 reply = Message.new(:board_id => 1, :subject => 'New reply', :content => 'New reply', :author_id => 2)
78 old_topic.children << reply
79
80 get :show, :project_id => 1, :id => 1
81 assert_response :success
82 topics = assigns(:topics)
83 assert_not_nil topics
84 assert_equal old_topic, topics.first
85 end
86
72 def test_show_with_permission_should_display_the_new_message_form
87 def test_show_with_permission_should_display_the_new_message_form
73 @request.session[:user_id] = 2
88 @request.session[:user_id] = 2
74 get :show, :project_id => 1, :id => 1
89 get :show, :project_id => 1, :id => 1
75 assert_response :success
90 assert_response :success
76 assert_template 'show'
91 assert_template 'show'
77
92
78 assert_select 'form#message-form' do
93 assert_select 'form#message-form' do
79 assert_select 'input[name=?]', 'message[subject]'
94 assert_select 'input[name=?]', 'message[subject]'
80 end
95 end
81 end
96 end
82
97
83 def test_show_atom
98 def test_show_atom
84 get :show, :project_id => 1, :id => 1, :format => 'atom'
99 get :show, :project_id => 1, :id => 1, :format => 'atom'
85 assert_response :success
100 assert_response :success
86 assert_template 'common/feed'
101 assert_template 'common/feed'
87 assert_not_nil assigns(:board)
102 assert_not_nil assigns(:board)
88 assert_not_nil assigns(:project)
103 assert_not_nil assigns(:project)
89 assert_not_nil assigns(:messages)
104 assert_not_nil assigns(:messages)
90 end
105 end
91
106
92 def test_show_not_found
107 def test_show_not_found
93 get :index, :project_id => 1, :id => 97
108 get :index, :project_id => 1, :id => 97
94 assert_response 404
109 assert_response 404
95 end
110 end
96
111
97 def test_new
112 def test_new
98 @request.session[:user_id] = 2
113 @request.session[:user_id] = 2
99 get :new, :project_id => 1
114 get :new, :project_id => 1
100 assert_response :success
115 assert_response :success
101 assert_template 'new'
116 assert_template 'new'
102
117
103 assert_select 'select[name=?]', 'board[parent_id]' do
118 assert_select 'select[name=?]', 'board[parent_id]' do
104 assert_select 'option', (Project.find(1).boards.size + 1)
119 assert_select 'option', (Project.find(1).boards.size + 1)
105 assert_select 'option[value=]', :text => ''
120 assert_select 'option[value=]', :text => ''
106 assert_select 'option[value=1]', :text => 'Help'
121 assert_select 'option[value=1]', :text => 'Help'
107 end
122 end
108 end
123 end
109
124
110 def test_new_without_project_boards
125 def test_new_without_project_boards
111 Project.find(1).boards.delete_all
126 Project.find(1).boards.delete_all
112 @request.session[:user_id] = 2
127 @request.session[:user_id] = 2
113
128
114 get :new, :project_id => 1
129 get :new, :project_id => 1
115 assert_response :success
130 assert_response :success
116 assert_template 'new'
131 assert_template 'new'
117
132
118 assert_select 'select[name=?]', 'board[parent_id]', 0
133 assert_select 'select[name=?]', 'board[parent_id]', 0
119 end
134 end
120
135
121 def test_create
136 def test_create
122 @request.session[:user_id] = 2
137 @request.session[:user_id] = 2
123 assert_difference 'Board.count' do
138 assert_difference 'Board.count' do
124 post :create, :project_id => 1, :board => { :name => 'Testing', :description => 'Testing board creation'}
139 post :create, :project_id => 1, :board => { :name => 'Testing', :description => 'Testing board creation'}
125 end
140 end
126 assert_redirected_to '/projects/ecookbook/settings/boards'
141 assert_redirected_to '/projects/ecookbook/settings/boards'
127 board = Board.first(:order => 'id DESC')
142 board = Board.first(:order => 'id DESC')
128 assert_equal 'Testing', board.name
143 assert_equal 'Testing', board.name
129 assert_equal 'Testing board creation', board.description
144 assert_equal 'Testing board creation', board.description
130 end
145 end
131
146
132 def test_create_with_parent
147 def test_create_with_parent
133 @request.session[:user_id] = 2
148 @request.session[:user_id] = 2
134 assert_difference 'Board.count' do
149 assert_difference 'Board.count' do
135 post :create, :project_id => 1, :board => { :name => 'Testing', :description => 'Testing', :parent_id => 2}
150 post :create, :project_id => 1, :board => { :name => 'Testing', :description => 'Testing', :parent_id => 2}
136 end
151 end
137 assert_redirected_to '/projects/ecookbook/settings/boards'
152 assert_redirected_to '/projects/ecookbook/settings/boards'
138 board = Board.first(:order => 'id DESC')
153 board = Board.first(:order => 'id DESC')
139 assert_equal Board.find(2), board.parent
154 assert_equal Board.find(2), board.parent
140 end
155 end
141
156
142 def test_create_with_failure
157 def test_create_with_failure
143 @request.session[:user_id] = 2
158 @request.session[:user_id] = 2
144 assert_no_difference 'Board.count' do
159 assert_no_difference 'Board.count' do
145 post :create, :project_id => 1, :board => { :name => '', :description => 'Testing board creation'}
160 post :create, :project_id => 1, :board => { :name => '', :description => 'Testing board creation'}
146 end
161 end
147 assert_response :success
162 assert_response :success
148 assert_template 'new'
163 assert_template 'new'
149 end
164 end
150
165
151 def test_edit
166 def test_edit
152 @request.session[:user_id] = 2
167 @request.session[:user_id] = 2
153 get :edit, :project_id => 1, :id => 2
168 get :edit, :project_id => 1, :id => 2
154 assert_response :success
169 assert_response :success
155 assert_template 'edit'
170 assert_template 'edit'
156 end
171 end
157
172
158 def test_edit_with_parent
173 def test_edit_with_parent
159 board = Board.generate!(:project_id => 1, :parent_id => 2)
174 board = Board.generate!(:project_id => 1, :parent_id => 2)
160 @request.session[:user_id] = 2
175 @request.session[:user_id] = 2
161 get :edit, :project_id => 1, :id => board.id
176 get :edit, :project_id => 1, :id => board.id
162 assert_response :success
177 assert_response :success
163 assert_template 'edit'
178 assert_template 'edit'
164
179
165 assert_select 'select[name=?]', 'board[parent_id]' do
180 assert_select 'select[name=?]', 'board[parent_id]' do
166 assert_select 'option[value=2][selected=selected]'
181 assert_select 'option[value=2][selected=selected]'
167 end
182 end
168 end
183 end
169
184
170 def test_update
185 def test_update
171 @request.session[:user_id] = 2
186 @request.session[:user_id] = 2
172 assert_no_difference 'Board.count' do
187 assert_no_difference 'Board.count' do
173 put :update, :project_id => 1, :id => 2, :board => { :name => 'Testing', :description => 'Testing board update'}
188 put :update, :project_id => 1, :id => 2, :board => { :name => 'Testing', :description => 'Testing board update'}
174 end
189 end
175 assert_redirected_to '/projects/ecookbook/settings/boards'
190 assert_redirected_to '/projects/ecookbook/settings/boards'
176 assert_equal 'Testing', Board.find(2).name
191 assert_equal 'Testing', Board.find(2).name
177 end
192 end
178
193
179 def test_update_position
194 def test_update_position
180 @request.session[:user_id] = 2
195 @request.session[:user_id] = 2
181 put :update, :project_id => 1, :id => 2, :board => { :move_to => 'highest'}
196 put :update, :project_id => 1, :id => 2, :board => { :move_to => 'highest'}
182 assert_redirected_to '/projects/ecookbook/settings/boards'
197 assert_redirected_to '/projects/ecookbook/settings/boards'
183 board = Board.find(2)
198 board = Board.find(2)
184 assert_equal 1, board.position
199 assert_equal 1, board.position
185 end
200 end
186
201
187 def test_update_with_failure
202 def test_update_with_failure
188 @request.session[:user_id] = 2
203 @request.session[:user_id] = 2
189 put :update, :project_id => 1, :id => 2, :board => { :name => '', :description => 'Testing board update'}
204 put :update, :project_id => 1, :id => 2, :board => { :name => '', :description => 'Testing board update'}
190 assert_response :success
205 assert_response :success
191 assert_template 'edit'
206 assert_template 'edit'
192 end
207 end
193
208
194 def test_destroy
209 def test_destroy
195 @request.session[:user_id] = 2
210 @request.session[:user_id] = 2
196 assert_difference 'Board.count', -1 do
211 assert_difference 'Board.count', -1 do
197 delete :destroy, :project_id => 1, :id => 2
212 delete :destroy, :project_id => 1, :id => 2
198 end
213 end
199 assert_redirected_to '/projects/ecookbook/settings/boards'
214 assert_redirected_to '/projects/ecookbook/settings/boards'
200 assert_nil Board.find_by_id(2)
215 assert_nil Board.find_by_id(2)
201 end
216 end
202 end
217 end
General Comments 0
You need to be logged in to leave comments. Login now