##// END OF EJS Templates
Rails3: model: replace deprecated errors.add_to_base at cannot_reply_to_locked_topic of Message...
Toshi MARUYAMA -
r7487:7183530e7a10
parent child
Show More
@@ -1,104 +1,104
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang
2 # Copyright (C) 2006-2011 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 belongs_to :board
19 belongs_to :board
20 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
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"
21 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
22 acts_as_attachable
22 acts_as_attachable
23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
24
24
25 acts_as_searchable :columns => ['subject', 'content'],
25 acts_as_searchable :columns => ['subject', 'content'],
26 :include => {:board => :project},
26 :include => {:board => :project},
27 :project_key => "#{Board.table_name}.project_id",
27 :project_key => "#{Board.table_name}.project_id",
28 :date_column => "#{table_name}.created_on"
28 :date_column => "#{table_name}.created_on"
29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
30 :description => :content,
30 :description => :content,
31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
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} :
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}"})}
33 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
34
34
35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
36 :author_key => :author_id
36 :author_key => :author_id
37 acts_as_watchable
37 acts_as_watchable
38
38
39 attr_protected :locked, :sticky
39 attr_protected :locked, :sticky
40 validates_presence_of :board, :subject, :content
40 validates_presence_of :board, :subject, :content
41 validates_length_of :subject, :maximum => 255
41 validates_length_of :subject, :maximum => 255
42 validate :cannot_reply_to_locked_topic, :on => :create
42 validate :cannot_reply_to_locked_topic, :on => :create
43
43
44 after_create :add_author_as_watcher, :update_parent_last_reply
44 after_create :add_author_as_watcher, :update_parent_last_reply
45 after_update :update_messages_board
45 after_update :update_messages_board
46 after_destroy :reset_board_counters
46 after_destroy :reset_board_counters
47
47
48 named_scope :visible, lambda {|*args| { :include => {:board => :project},
48 named_scope :visible, lambda {|*args| { :include => {:board => :project},
49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
50
50
51 def visible?(user=User.current)
51 def visible?(user=User.current)
52 !user.nil? && user.allowed_to?(:view_messages, project)
52 !user.nil? && user.allowed_to?(:view_messages, project)
53 end
53 end
54
54
55 def cannot_reply_to_locked_topic
55 def cannot_reply_to_locked_topic
56 # Can not reply to a locked topic
56 # Can not reply to a locked topic
57 errors.add_to_base 'Topic is locked' if root.locked? && self != root
57 errors.add :base, 'Topic is locked' if root.locked? && self != root
58 end
58 end
59
59
60 def update_parent_last_reply
60 def update_parent_last_reply
61 if parent
61 if parent
62 parent.reload.update_attribute(:last_reply_id, self.id)
62 parent.reload.update_attribute(:last_reply_id, self.id)
63 end
63 end
64 board.reset_counters!
64 board.reset_counters!
65 end
65 end
66
66
67 def update_messages_board
67 def update_messages_board
68 if board_id_changed?
68 if board_id_changed?
69 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
69 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
70 Board.reset_counters!(board_id_was)
70 Board.reset_counters!(board_id_was)
71 Board.reset_counters!(board_id)
71 Board.reset_counters!(board_id)
72 end
72 end
73 end
73 end
74
74
75 def reset_board_counters
75 def reset_board_counters
76 board.reset_counters!
76 board.reset_counters!
77 end
77 end
78
78
79 def sticky=(arg)
79 def sticky=(arg)
80 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
80 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
81 end
81 end
82
82
83 def sticky?
83 def sticky?
84 sticky == 1
84 sticky == 1
85 end
85 end
86
86
87 def project
87 def project
88 board.project
88 board.project
89 end
89 end
90
90
91 def editable_by?(usr)
91 def editable_by?(usr)
92 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
92 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
93 end
93 end
94
94
95 def destroyable_by?(usr)
95 def destroyable_by?(usr)
96 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
96 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
97 end
97 end
98
98
99 private
99 private
100
100
101 def add_author_as_watcher
101 def add_author_as_watcher
102 Watcher.create(:watchable => self.root, :user => author)
102 Watcher.create(:watchable => self.root, :user => author)
103 end
103 end
104 end
104 end
General Comments 0
You need to be logged in to leave comments. Login now