comparison app/models/message.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2011 Jean-Philippe Lang 2 # Copyright (C) 2006-2012 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.
39 39
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, :reset_counters!
45 after_update :update_messages_board 45 after_update :update_messages_board
46 after_destroy :reset_board_counters 46 after_destroy :reset_counters!
47 47
48 named_scope :visible, lambda {|*args| { :include => {:board => :project}, 48 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 safe_attributes 'subject', 'content' 51 safe_attributes 'subject', 'content'
52 safe_attributes 'locked', 'sticky', 'board_id', 52 safe_attributes 'locked', 'sticky', 'board_id',
53 :if => lambda {|message, user| 53 :if => lambda {|message, user|
61 def cannot_reply_to_locked_topic 61 def cannot_reply_to_locked_topic
62 # Can not reply to a locked topic 62 # Can not reply to a locked topic
63 errors.add :base, 'Topic is locked' if root.locked? && self != root 63 errors.add :base, 'Topic is locked' if root.locked? && self != root
64 end 64 end
65 65
66 def update_parent_last_reply
67 if parent
68 parent.reload.update_attribute(:last_reply_id, self.id)
69 end
70 board.reset_counters!
71 end
72
73 def update_messages_board 66 def update_messages_board
74 if board_id_changed? 67 if board_id_changed?
75 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id]) 68 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
76 Board.reset_counters!(board_id_was) 69 Board.reset_counters!(board_id_was)
77 Board.reset_counters!(board_id) 70 Board.reset_counters!(board_id)
78 end 71 end
79 end 72 end
80 73
81 def reset_board_counters 74 def reset_counters!
75 if parent && parent.id
76 Message.update_all({:last_reply_id => parent.children.maximum(:id)}, {:id => parent.id})
77 end
82 board.reset_counters! 78 board.reset_counters!
83 end 79 end
84 80
85 def sticky=(arg) 81 def sticky=(arg)
86 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0) 82 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)