@@ -46,6 +46,7 class BoardsController < ApplicationController | |||||
46 | :include => [:author, {:last_reply => :author}], |
|
46 | :include => [:author, {:last_reply => :author}], | |
47 | :limit => @topic_pages.items_per_page, |
|
47 | :limit => @topic_pages.items_per_page, | |
48 | :offset => @topic_pages.current.offset |
|
48 | :offset => @topic_pages.current.offset | |
|
49 | @message = Message.new | |||
49 | render :action => 'show', :layout => !request.xhr? |
|
50 | render :action => 'show', :layout => !request.xhr? | |
50 | end |
|
51 | end | |
51 |
|
52 |
@@ -73,7 +73,8 class MessagesController < ApplicationController | |||||
73 | if request.post? && @message.update_attributes(params[:message]) |
|
73 | if request.post? && @message.update_attributes(params[:message]) | |
74 | attach_files(@message, params[:attachments]) |
|
74 | attach_files(@message, params[:attachments]) | |
75 | flash[:notice] = l(:notice_successful_update) |
|
75 | flash[:notice] = l(:notice_successful_update) | |
76 | redirect_to :action => 'show', :id => @topic |
|
76 | @message.reload | |
|
77 | redirect_to :action => 'show', :board_id => @message.board, :id => @message.root | |||
77 | end |
|
78 | end | |
78 | end |
|
79 | end | |
79 |
|
80 |
@@ -26,4 +26,17 class Board < ActiveRecord::Base | |||||
26 | validates_presence_of :name, :description |
|
26 | validates_presence_of :name, :description | |
27 | validates_length_of :name, :maximum => 30 |
|
27 | validates_length_of :name, :maximum => 30 | |
28 | validates_length_of :description, :maximum => 255 |
|
28 | validates_length_of :description, :maximum => 255 | |
|
29 | ||||
|
30 | def reset_counters! | |||
|
31 | self.class.reset_counters!(id) | |||
|
32 | end | |||
|
33 | ||||
|
34 | # Updates topics_count, messages_count and last_message_id attributes for +board_id+ | |||
|
35 | def self.reset_counters!(board_id) | |||
|
36 | board_id = board_id.to_i | |||
|
37 | update_all("topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id} AND parent_id IS NULL)," + | |||
|
38 | " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id})," + | |||
|
39 | " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=#{board_id})", | |||
|
40 | ["id = ?", board_id]) | |||
|
41 | end | |||
29 | end |
|
42 | end |
@@ -37,7 +37,7 class Message < ActiveRecord::Base | |||||
37 | acts_as_watchable |
|
37 | acts_as_watchable | |
38 |
|
38 | |||
39 | attr_protected :locked, :sticky |
|
39 | attr_protected :locked, :sticky | |
40 | validates_presence_of :subject, :content |
|
40 | validates_presence_of :board, :subject, :content | |
41 | validates_length_of :subject, :maximum => 255 |
|
41 | validates_length_of :subject, :maximum => 255 | |
42 |
|
42 | |||
43 | after_create :add_author_as_watcher |
|
43 | after_create :add_author_as_watcher | |
@@ -48,21 +48,22 class Message < ActiveRecord::Base | |||||
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def after_create |
|
50 | def after_create | |
51 | board.update_attribute(:last_message_id, self.id) |
|
|||
52 | board.increment! :messages_count |
|
|||
53 | if parent |
|
51 | if parent | |
54 | parent.reload.update_attribute(:last_reply_id, self.id) |
|
52 | parent.reload.update_attribute(:last_reply_id, self.id) | |
55 |
e |
|
53 | end | |
56 |
|
|
54 | board.reset_counters! | |
|
55 | end | |||
|
56 | ||||
|
57 | def after_update | |||
|
58 | if board_id_changed? | |||
|
59 | Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id]) | |||
|
60 | Board.reset_counters!(board_id_was) | |||
|
61 | Board.reset_counters!(board_id) | |||
57 | end |
|
62 | end | |
58 | end |
|
63 | end | |
59 |
|
64 | |||
60 | def after_destroy |
|
65 | def after_destroy | |
61 | # The following line is required so that the previous counter |
|
66 | board.reset_counters! | |
62 | # updates (due to children removal) are not overwritten |
|
|||
63 | board.reload |
|
|||
64 | board.decrement! :messages_count |
|
|||
65 | board.decrement! :topics_count unless parent |
|
|||
66 | end |
|
67 | end | |
67 |
|
68 | |||
68 | def sticky? |
|
69 | def sticky? |
@@ -12,6 +12,11 | |||||
12 | <% end %> |
|
12 | <% end %> | |
13 | </p> |
|
13 | </p> | |
14 |
|
14 | |||
|
15 | <% if !replying && !@message.new_record? && User.current.allowed_to?(:edit_messages, @project) %> | |||
|
16 | <p><label><%= l(:label_board) %></label><br /> | |||
|
17 | <%= f.select :board_id, @project.boards.collect {|b| [b.name, b.id]} %></p> | |||
|
18 | <% end %> | |||
|
19 | ||||
15 | <p><%= f.text_area :content, :cols => 80, :rows => 15, :class => 'wiki-edit', :id => 'message_content' %></p> |
|
20 | <p><%= f.text_area :content, :cols => 80, :rows => 15, :class => 'wiki-edit', :id => 'message_content' %></p> | |
16 | <%= wikitoolbar_for 'message_content' %> |
|
21 | <%= wikitoolbar_for 'message_content' %> | |
17 | <!--[eoform:message]--> |
|
22 | <!--[eoform:message]--> |
@@ -6,8 +6,8 boards_001: | |||||
6 | id: 1 |
|
6 | id: 1 | |
7 | description: Help board |
|
7 | description: Help board | |
8 | position: 1 |
|
8 | position: 1 | |
9 |
last_message_id: |
|
9 | last_message_id: 6 | |
10 |
messages_count: |
|
10 | messages_count: 6 | |
11 | boards_002: |
|
11 | boards_002: | |
12 | name: Discussion |
|
12 | name: Discussion | |
13 | project_id: 1 |
|
13 | project_id: 1 |
@@ -1,3 +1,20 | |||||
|
1 | # Redmine - project management software | |||
|
2 | # Copyright (C) 2006-2009 Jean-Philippe Lang | |||
|
3 | # | |||
|
4 | # This program is free software; you can redistribute it and/or | |||
|
5 | # modify it under the terms of the GNU General Public License | |||
|
6 | # as published by the Free Software Foundation; either version 2 | |||
|
7 | # of the License, or (at your option) any later version. | |||
|
8 | # | |||
|
9 | # This program is distributed in the hope that it will be useful, | |||
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
|
12 | # GNU General Public License for more details. | |||
|
13 | # | |||
|
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 | |||
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
|
17 | ||||
1 | require File.dirname(__FILE__) + '/../test_helper' |
|
18 | require File.dirname(__FILE__) + '/../test_helper' | |
2 |
|
19 | |||
3 | class MessageTest < Test::Unit::TestCase |
|
20 | class MessageTest < Test::Unit::TestCase | |
@@ -47,6 +64,23 class MessageTest < Test::Unit::TestCase | |||||
47 | assert @message.watched_by?(reply_author) |
|
64 | assert @message.watched_by?(reply_author) | |
48 | end |
|
65 | end | |
49 |
|
66 | |||
|
67 | def test_moving_message_should_update_counters | |||
|
68 | @message = Message.find(1) | |||
|
69 | assert_no_difference 'Message.count' do | |||
|
70 | # Previous board | |||
|
71 | assert_difference 'Board.find(1).topics_count', -1 do | |||
|
72 | assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do | |||
|
73 | # New board | |||
|
74 | assert_difference 'Board.find(2).topics_count' do | |||
|
75 | assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do | |||
|
76 | @message.update_attributes(:board_id => 2) | |||
|
77 | end | |||
|
78 | end | |||
|
79 | end | |||
|
80 | end | |||
|
81 | end | |||
|
82 | end | |||
|
83 | ||||
50 | def test_destroy_topic |
|
84 | def test_destroy_topic | |
51 | message = Message.find(1) |
|
85 | message = Message.find(1) | |
52 | board = message.board |
|
86 | board = message.board |
General Comments 0
You need to be logged in to leave comments.
Login now