@@ -1,115 +1,115 | |||||
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 Message < ActiveRecord::Base |
|
18 | class Message < ActiveRecord::Base | |
19 | include Redmine::SafeAttributes |
|
19 | include Redmine::SafeAttributes | |
20 | belongs_to :board |
|
20 | belongs_to :board | |
21 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' |
|
21 | belongs_to :author, :class_name => 'User', :foreign_key => 'author_id' | |
22 | acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" |
|
22 | acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC" | |
23 | acts_as_attachable |
|
23 | acts_as_attachable | |
24 | belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' |
|
24 | belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id' | |
25 |
|
25 | |||
26 | acts_as_searchable :columns => ['subject', 'content'], |
|
26 | acts_as_searchable :columns => ['subject', 'content'], | |
27 | :include => {:board => :project}, |
|
27 | :include => {:board => :project}, | |
28 | :project_key => "#{Board.table_name}.project_id", |
|
28 | :project_key => "#{Board.table_name}.project_id", | |
29 | :date_column => "#{table_name}.created_on" |
|
29 | :date_column => "#{table_name}.created_on" | |
30 | acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, |
|
30 | acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"}, | |
31 | :description => :content, |
|
31 | :description => :content, | |
32 | :group => :parent, |
|
32 | :group => :parent, | |
33 | :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, |
|
33 | :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'}, | |
34 | :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : |
|
34 | :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} : | |
35 | {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})} |
|
35 | {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})} | |
36 |
|
36 | |||
37 | acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, |
|
37 | acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]}, | |
38 | :author_key => :author_id |
|
38 | :author_key => :author_id | |
39 | acts_as_watchable |
|
39 | acts_as_watchable | |
40 |
|
40 | |||
41 | validates_presence_of :board, :subject, :content |
|
41 | validates_presence_of :board, :subject, :content | |
42 | validates_length_of :subject, :maximum => 255 |
|
42 | validates_length_of :subject, :maximum => 255 | |
43 | validate :cannot_reply_to_locked_topic, :on => :create |
|
43 | validate :cannot_reply_to_locked_topic, :on => :create | |
44 |
|
44 | |||
45 | after_create :add_author_as_watcher, :reset_counters! |
|
45 | after_create :add_author_as_watcher, :reset_counters! | |
46 | after_update :update_messages_board |
|
46 | after_update :update_messages_board | |
47 | after_destroy :reset_counters! |
|
47 | after_destroy :reset_counters! | |
48 | after_create :send_notification |
|
48 | after_create :send_notification | |
49 |
|
49 | |||
50 | scope :visible, lambda {|*args| |
|
50 | scope :visible, lambda {|*args| | |
51 | includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) |
|
51 | includes(:board => :project).where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args)) | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | safe_attributes 'subject', 'content' |
|
54 | safe_attributes 'subject', 'content' | |
55 | safe_attributes 'locked', 'sticky', 'board_id', |
|
55 | safe_attributes 'locked', 'sticky', 'board_id', | |
56 | :if => lambda {|message, user| |
|
56 | :if => lambda {|message, user| | |
57 | user.allowed_to?(:edit_messages, message.project) |
|
57 | user.allowed_to?(:edit_messages, message.project) | |
58 | } |
|
58 | } | |
59 |
|
59 | |||
60 | def visible?(user=User.current) |
|
60 | def visible?(user=User.current) | |
61 | !user.nil? && user.allowed_to?(:view_messages, project) |
|
61 | !user.nil? && user.allowed_to?(:view_messages, project) | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def cannot_reply_to_locked_topic |
|
64 | def cannot_reply_to_locked_topic | |
65 | # Can not reply to a locked topic |
|
65 | # Can not reply to a locked topic | |
66 | errors.add :base, 'Topic is locked' if root.locked? && self != root |
|
66 | errors.add :base, 'Topic is locked' if root.locked? && self != root | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def update_messages_board |
|
69 | def update_messages_board | |
70 | if board_id_changed? |
|
70 | if board_id_changed? | |
71 |
Message. |
|
71 | Message.where(["id = ? OR parent_id = ?", root.id, root.id]).update_all({:board_id => board_id}) | |
72 | Board.reset_counters!(board_id_was) |
|
72 | Board.reset_counters!(board_id_was) | |
73 | Board.reset_counters!(board_id) |
|
73 | Board.reset_counters!(board_id) | |
74 | end |
|
74 | end | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def reset_counters! |
|
77 | def reset_counters! | |
78 | if parent && parent.id |
|
78 | if parent && parent.id | |
79 |
Message.update_all({:last_reply_id => parent.children.maximum(:id)} |
|
79 | Message.where({:id => parent.id}).update_all({:last_reply_id => parent.children.maximum(:id)}) | |
80 | end |
|
80 | end | |
81 | board.reset_counters! |
|
81 | board.reset_counters! | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | def sticky=(arg) |
|
84 | def sticky=(arg) | |
85 | write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) |
|
85 | write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def sticky? |
|
88 | def sticky? | |
89 | sticky == 1 |
|
89 | sticky == 1 | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def project |
|
92 | def project | |
93 | board.project |
|
93 | board.project | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def editable_by?(usr) |
|
96 | def editable_by?(usr) | |
97 | usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) |
|
97 | usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project))) | |
98 | end |
|
98 | end | |
99 |
|
99 | |||
100 | def destroyable_by?(usr) |
|
100 | def destroyable_by?(usr) | |
101 | usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) |
|
101 | usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project))) | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | private |
|
104 | private | |
105 |
|
105 | |||
106 | def add_author_as_watcher |
|
106 | def add_author_as_watcher | |
107 | Watcher.create(:watchable => self.root, :user => author) |
|
107 | Watcher.create(:watchable => self.root, :user => author) | |
108 | end |
|
108 | end | |
109 |
|
109 | |||
110 | def send_notification |
|
110 | def send_notification | |
111 | if Setting.notified_events.include?('message_posted') |
|
111 | if Setting.notified_events.include?('message_posted') | |
112 | Mailer.message_posted(self).deliver |
|
112 | Mailer.message_posted(self).deliver | |
113 | end |
|
113 | end | |
114 | end |
|
114 | end | |
115 | end |
|
115 | end |
General Comments 0
You need to be logged in to leave comments.
Login now