##// END OF EJS Templates
Rails3: model: replace deprecated 'after_destroy' method at Message model...
Toshi MARUYAMA -
r7337:4ac5a2c338ce
parent child
Show More
@@ -1,102 +1,103
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 => "#{Board.table_name}.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, :r => o.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, :update_parent_last_reply
44 44 after_update :update_messages_board
45 after_destroy :reset_board_counters
45 46
46 47 named_scope :visible, lambda {|*args| { :include => {:board => :project},
47 48 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
48 49
49 50 def visible?(user=User.current)
50 51 !user.nil? && user.allowed_to?(:view_messages, project)
51 52 end
52 53
53 54 def validate_on_create
54 55 # Can not reply to a locked topic
55 56 errors.add_to_base 'Topic is locked' if root.locked? && self != root
56 57 end
57 58
58 59 def update_parent_last_reply
59 60 if parent
60 61 parent.reload.update_attribute(:last_reply_id, self.id)
61 62 end
62 63 board.reset_counters!
63 64 end
64 65
65 66 def update_messages_board
66 67 if board_id_changed?
67 68 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
68 69 Board.reset_counters!(board_id_was)
69 70 Board.reset_counters!(board_id)
70 71 end
71 72 end
72 73
73 def after_destroy
74 def reset_board_counters
74 75 board.reset_counters!
75 76 end
76 77
77 78 def sticky=(arg)
78 79 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
79 80 end
80 81
81 82 def sticky?
82 83 sticky == 1
83 84 end
84 85
85 86 def project
86 87 board.project
87 88 end
88 89
89 90 def editable_by?(usr)
90 91 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
91 92 end
92 93
93 94 def destroyable_by?(usr)
94 95 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
95 96 end
96 97
97 98 private
98 99
99 100 def add_author_as_watcher
100 101 Watcher.create(:watchable => self.root, :user => author)
101 102 end
102 103 end
General Comments 0
You need to be logged in to leave comments. Login now