:include => [:author, {:last_reply => :author}],
:limit => @topic_pages.items_per_page,
:offset => @topic_pages.current.offset
+ @message = Message.new
render :action => 'show', :layout => !request.xhr?
end
if request.post? && @message.update_attributes(params[:message])
attach_files(@message, params[:attachments])
flash[:notice] = l(:notice_successful_update)
- redirect_to :action => 'show', :id => @topic
+ @message.reload
+ redirect_to :action => 'show', :board_id => @message.board, :id => @message.root
end
end
validates_presence_of :name, :description
validates_length_of :name, :maximum => 30
validates_length_of :description, :maximum => 255
+
+ def reset_counters!
+ self.class.reset_counters!(id)
+ end
+
+ # Updates topics_count, messages_count and last_message_id attributes for +board_id+
+ def self.reset_counters!(board_id)
+ board_id = board_id.to_i
+ update_all("topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id} AND parent_id IS NULL)," +
+ " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id})," +
+ " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=#{board_id})",
+ ["id = ?", board_id])
+ end
end
acts_as_watchable
attr_protected :locked, :sticky
- validates_presence_of :subject, :content
+ validates_presence_of :board, :subject, :content
validates_length_of :subject, :maximum => 255
after_create :add_author_as_watcher
end
def after_create
- board.update_attribute(:last_message_id, self.id)
- board.increment! :messages_count
if parent
parent.reload.update_attribute(:last_reply_id, self.id)
- else
- board.increment! :topics_count
+ end
+ board.reset_counters!
+ end
+
+ def after_update
+ if board_id_changed?
+ Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
+ Board.reset_counters!(board_id_was)
+ Board.reset_counters!(board_id)
end
end
def after_destroy
- # The following line is required so that the previous counter
- # updates (due to children removal) are not overwritten
- board.reload
- board.decrement! :messages_count
- board.decrement! :topics_count unless parent
+ board.reset_counters!
end
def sticky?
<% end %>
</p>
+<% if !replying && !@message.new_record? && User.current.allowed_to?(:edit_messages, @project) %>
+ <p><label><%= l(:label_board) %></label><br />
+ <%= f.select :board_id, @project.boards.collect {|b| [b.name, b.id]} %></p>
+<% end %>
+
<p><%= f.text_area :content, :cols => 80, :rows => 15, :class => 'wiki-edit', :id => 'message_content' %></p>
<%= wikitoolbar_for 'message_content' %>
<!--[eoform:message]-->
id: 1\r
description: Help board\r
position: 1\r
- last_message_id: 5\r
- messages_count: 5\r
+ last_message_id: 6\r
+ messages_count: 6\r
boards_002: \r
name: Discussion\r
project_id: 1\r
+# Redmine - project management software
+# Copyright (C) 2006-2009 Jean-Philippe Lang
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
require File.dirname(__FILE__) + '/../test_helper'
class MessageTest < Test::Unit::TestCase
assert @message.watched_by?(reply_author)
end
+ def test_moving_message_should_update_counters
+ @message = Message.find(1)
+ assert_no_difference 'Message.count' do
+ # Previous board
+ assert_difference 'Board.find(1).topics_count', -1 do
+ assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do
+ # New board
+ assert_difference 'Board.find(2).topics_count' do
+ assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do
+ @message.update_attributes(:board_id => 2)
+ end
+ end
+ end
+ end
+ end
+ end
+
def test_destroy_topic
message = Message.find(1)
board = message.board