Mercurial > hg > soundsoftware-site
comparison app/models/.svn/text-base/message.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | 0579821a129a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # redMine - project management software | |
2 # Copyright (C) 2006-2007 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 | |
18 class Message < ActiveRecord::Base | |
19 belongs_to :board | |
20 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' | |
21 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" | |
22 acts_as_attachable | |
23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' | |
24 | |
25 acts_as_searchable :columns => ['subject', 'content'], | |
26 :include => {:board => :project}, | |
27 :project_key => 'project_id', | |
28 :date_column => "#{table_name}.created_on" | |
29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, | |
30 :description => :content, | |
31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, | |
32 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : | |
33 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})} | |
34 | |
35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, | |
36 :author_key => :author_id | |
37 acts_as_watchable | |
38 | |
39 attr_protected :locked, :sticky | |
40 validates_presence_of :board, :subject, :content | |
41 validates_length_of :subject, :maximum => 255 | |
42 | |
43 after_create :add_author_as_watcher | |
44 | |
45 def visible?(user=User.current) | |
46 !user.nil? && user.allowed_to?(:view_messages, project) | |
47 end | |
48 | |
49 def validate_on_create | |
50 # Can not reply to a locked topic | |
51 errors.add_to_base 'Topic is locked' if root.locked? && self != root | |
52 end | |
53 | |
54 def after_create | |
55 if parent | |
56 parent.reload.update_attribute(:last_reply_id, self.id) | |
57 end | |
58 board.reset_counters! | |
59 end | |
60 | |
61 def after_update | |
62 if board_id_changed? | |
63 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id]) | |
64 Board.reset_counters!(board_id_was) | |
65 Board.reset_counters!(board_id) | |
66 end | |
67 end | |
68 | |
69 def after_destroy | |
70 board.reset_counters! | |
71 end | |
72 | |
73 def sticky=(arg) | |
74 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) | |
75 end | |
76 | |
77 def sticky? | |
78 sticky == 1 | |
79 end | |
80 | |
81 def project | |
82 board.project | |
83 end | |
84 | |
85 def editable_by?(usr) | |
86 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) | |
87 end | |
88 | |
89 def destroyable_by?(usr) | |
90 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) | |
91 end | |
92 | |
93 private | |
94 | |
95 def add_author_as_watcher | |
96 Watcher.create(:watchable => self.root, :user => author) | |
97 end | |
98 end |