@@ -0,0 +1,9 | |||
|
1 | class FixMessagesStickyNull < ActiveRecord::Migration | |
|
2 | def self.up | |
|
3 | Message.update_all('sticky = 0', 'sticky IS NULL') | |
|
4 | end | |
|
5 | ||
|
6 | def self.down | |
|
7 | # nothing to do | |
|
8 | end | |
|
9 | end |
@@ -1,90 +1,94 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class Message < ActiveRecord::Base |
|
19 | 19 | belongs_to :board |
|
20 | 20 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
21 | 21 | acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" |
|
22 | 22 | acts_as_attachable |
|
23 | 23 | belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' |
|
24 | 24 | |
|
25 | 25 | acts_as_searchable :columns => ['subject', 'content'], |
|
26 | 26 | :include => {:board => :project}, |
|
27 | 27 | :project_key => 'project_id', |
|
28 | 28 | :date_column => "#{table_name}.created_on" |
|
29 | 29 | acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, |
|
30 | 30 | :description => :content, |
|
31 | 31 | :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, |
|
32 | 32 | :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : |
|
33 | 33 | {:id => o.parent_id, :anchor => "message-#{o.id}"})} |
|
34 | 34 | |
|
35 | 35 | acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, |
|
36 | 36 | :author_key => :author_id |
|
37 | 37 | acts_as_watchable |
|
38 | 38 | |
|
39 | 39 | attr_protected :locked, :sticky |
|
40 | 40 | validates_presence_of :board, :subject, :content |
|
41 | 41 | validates_length_of :subject, :maximum => 255 |
|
42 | 42 | |
|
43 | 43 | after_create :add_author_as_watcher |
|
44 | 44 | |
|
45 | 45 | def validate_on_create |
|
46 | 46 | # Can not reply to a locked topic |
|
47 | 47 | errors.add_to_base 'Topic is locked' if root.locked? && self != root |
|
48 | 48 | end |
|
49 | 49 | |
|
50 | 50 | def after_create |
|
51 | 51 | if parent |
|
52 | 52 | parent.reload.update_attribute(:last_reply_id, self.id) |
|
53 | 53 | end |
|
54 | 54 | board.reset_counters! |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def after_update |
|
58 | 58 | if board_id_changed? |
|
59 | 59 | Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id]) |
|
60 | 60 | Board.reset_counters!(board_id_was) |
|
61 | 61 | Board.reset_counters!(board_id) |
|
62 | 62 | end |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def after_destroy |
|
66 | 66 | board.reset_counters! |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | def sticky=(arg) | |
|
70 | write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) | |
|
71 | end | |
|
72 | ||
|
69 | 73 | def sticky? |
|
70 | 74 | sticky == 1 |
|
71 | 75 | end |
|
72 | 76 | |
|
73 | 77 | def project |
|
74 | 78 | board.project |
|
75 | 79 | end |
|
76 | 80 | |
|
77 | 81 | def editable_by?(usr) |
|
78 | 82 | usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) |
|
79 | 83 | end |
|
80 | 84 | |
|
81 | 85 | def destroyable_by?(usr) |
|
82 | 86 | usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) |
|
83 | 87 | end |
|
84 | 88 | |
|
85 | 89 | private |
|
86 | 90 | |
|
87 | 91 | def add_author_as_watcher |
|
88 | 92 | Watcher.create(:watchable => self.root, :user => author) |
|
89 | 93 | end |
|
90 | 94 | end |
@@ -1,131 +1,146 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2009 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.dirname(__FILE__) + '/../test_helper' |
|
19 | 19 | |
|
20 | 20 | class MessageTest < Test::Unit::TestCase |
|
21 | 21 | fixtures :projects, :roles, :members, :member_roles, :boards, :messages, :users, :watchers |
|
22 | 22 | |
|
23 | 23 | def setup |
|
24 | 24 | @board = Board.find(1) |
|
25 | 25 | @user = User.find(1) |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | def test_create |
|
29 | 29 | topics_count = @board.topics_count |
|
30 | 30 | messages_count = @board.messages_count |
|
31 | 31 | |
|
32 | 32 | message = Message.new(:board => @board, :subject => 'Test message', :content => 'Test message content', :author => @user) |
|
33 | 33 | assert message.save |
|
34 | 34 | @board.reload |
|
35 | 35 | # topics count incremented |
|
36 | 36 | assert_equal topics_count+1, @board[:topics_count] |
|
37 | 37 | # messages count incremented |
|
38 | 38 | assert_equal messages_count+1, @board[:messages_count] |
|
39 | 39 | assert_equal message, @board.last_message |
|
40 | 40 | # author should be watching the message |
|
41 | 41 | assert message.watched_by?(@user) |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_reply |
|
45 | 45 | topics_count = @board.topics_count |
|
46 | 46 | messages_count = @board.messages_count |
|
47 | 47 | @message = Message.find(1) |
|
48 | 48 | replies_count = @message.replies_count |
|
49 | 49 | |
|
50 | 50 | reply_author = User.find(2) |
|
51 | 51 | reply = Message.new(:board => @board, :subject => 'Test reply', :content => 'Test reply content', :parent => @message, :author => reply_author) |
|
52 | 52 | assert reply.save |
|
53 | 53 | @board.reload |
|
54 | 54 | # same topics count |
|
55 | 55 | assert_equal topics_count, @board[:topics_count] |
|
56 | 56 | # messages count incremented |
|
57 | 57 | assert_equal messages_count+1, @board[:messages_count] |
|
58 | 58 | assert_equal reply, @board.last_message |
|
59 | 59 | @message.reload |
|
60 | 60 | # replies count incremented |
|
61 | 61 | assert_equal replies_count+1, @message[:replies_count] |
|
62 | 62 | assert_equal reply, @message.last_reply |
|
63 | 63 | # author should be watching the message |
|
64 | 64 | assert @message.watched_by?(reply_author) |
|
65 | 65 | end |
|
66 | 66 | |
|
67 | 67 | def test_moving_message_should_update_counters |
|
68 | 68 | @message = Message.find(1) |
|
69 | 69 | assert_no_difference 'Message.count' do |
|
70 | 70 | # Previous board |
|
71 | 71 | assert_difference 'Board.find(1).topics_count', -1 do |
|
72 | 72 | assert_difference 'Board.find(1).messages_count', -(1 + @message.replies_count) do |
|
73 | 73 | # New board |
|
74 | 74 | assert_difference 'Board.find(2).topics_count' do |
|
75 | 75 | assert_difference 'Board.find(2).messages_count', (1 + @message.replies_count) do |
|
76 | 76 | @message.update_attributes(:board_id => 2) |
|
77 | 77 | end |
|
78 | 78 | end |
|
79 | 79 | end |
|
80 | 80 | end |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def test_destroy_topic |
|
85 | 85 | message = Message.find(1) |
|
86 | 86 | board = message.board |
|
87 | 87 | topics_count, messages_count = board.topics_count, board.messages_count |
|
88 | 88 | |
|
89 | 89 | assert_difference('Watcher.count', -1) do |
|
90 | 90 | assert message.destroy |
|
91 | 91 | end |
|
92 | 92 | board.reload |
|
93 | 93 | |
|
94 | 94 | # Replies deleted |
|
95 | 95 | assert Message.find_all_by_parent_id(1).empty? |
|
96 | 96 | # Checks counters |
|
97 | 97 | assert_equal topics_count - 1, board.topics_count |
|
98 | 98 | assert_equal messages_count - 3, board.messages_count |
|
99 | 99 | # Watchers removed |
|
100 | 100 | end |
|
101 | 101 | |
|
102 | 102 | def test_destroy_reply |
|
103 | 103 | message = Message.find(5) |
|
104 | 104 | board = message.board |
|
105 | 105 | topics_count, messages_count = board.topics_count, board.messages_count |
|
106 | 106 | assert message.destroy |
|
107 | 107 | board.reload |
|
108 | 108 | |
|
109 | 109 | # Checks counters |
|
110 | 110 | assert_equal topics_count, board.topics_count |
|
111 | 111 | assert_equal messages_count - 1, board.messages_count |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def test_editable_by |
|
115 | 115 | message = Message.find(6) |
|
116 | 116 | author = message.author |
|
117 | 117 | assert message.editable_by?(author) |
|
118 | 118 | |
|
119 | 119 | author.roles_for_project(message.project).first.remove_permission!(:edit_own_messages) |
|
120 | 120 | assert !message.reload.editable_by?(author.reload) |
|
121 | 121 | end |
|
122 | 122 | |
|
123 | 123 | def test_destroyable_by |
|
124 | 124 | message = Message.find(6) |
|
125 | 125 | author = message.author |
|
126 | 126 | assert message.destroyable_by?(author) |
|
127 | 127 | |
|
128 | 128 | author.roles_for_project(message.project).first.remove_permission!(:delete_own_messages) |
|
129 | 129 | assert !message.reload.destroyable_by?(author.reload) |
|
130 | 130 | end |
|
131 | ||
|
132 | def test_set_sticky | |
|
133 | message = Message.new | |
|
134 | assert_equal 0, message.sticky | |
|
135 | message.sticky = nil | |
|
136 | assert_equal 0, message.sticky | |
|
137 | message.sticky = false | |
|
138 | assert_equal 0, message.sticky | |
|
139 | message.sticky = true | |
|
140 | assert_equal 1, message.sticky | |
|
141 | message.sticky = '0' | |
|
142 | assert_equal 0, message.sticky | |
|
143 | message.sticky = '1' | |
|
144 | assert_equal 1, message.sticky | |
|
145 | end | |
|
131 | 146 | end |
General Comments 0
You need to be logged in to leave comments.
Login now