annotate .svn/pristine/0f/0f12084a6a3192cb0d61cbd2f3ef681df419cac4.svn-base @ 929:5f33065ddc4b redmine-1.3

Update to Redmine SVN rev 9414 on 1.3-stable branch
author Chris Cannam
date Wed, 27 Jun 2012 14:54:18 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 # Redmine - project management software
Chris@909 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
Chris@909 3 #
Chris@909 4 # This program is free software; you can redistribute it and/or
Chris@909 5 # modify it under the terms of the GNU General Public License
Chris@909 6 # as published by the Free Software Foundation; either version 2
Chris@909 7 # of the License, or (at your option) any later version.
Chris@909 8 #
Chris@909 9 # This program is distributed in the hope that it will be useful,
Chris@909 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@909 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@909 12 # GNU General Public License for more details.
Chris@909 13 #
Chris@909 14 # You should have received a copy of the GNU General Public License
Chris@909 15 # along with this program; if not, write to the Free Software
Chris@909 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@909 17
Chris@909 18 class Message < ActiveRecord::Base
Chris@909 19 belongs_to :board
Chris@909 20 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
Chris@909 21 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
Chris@909 22 acts_as_attachable
Chris@909 23 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
Chris@909 24
Chris@909 25 acts_as_searchable :columns => ['subject', 'content'],
Chris@909 26 :include => {:board => :project},
Chris@909 27 :project_key => "#{Board.table_name}.project_id",
Chris@909 28 :date_column => "#{table_name}.created_on"
Chris@909 29 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
Chris@909 30 :description => :content,
Chris@909 31 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
Chris@909 32 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
Chris@909 33 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
Chris@909 34
Chris@909 35 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
Chris@909 36 :author_key => :author_id
Chris@909 37 acts_as_watchable
Chris@909 38
Chris@909 39 attr_protected :locked, :sticky
Chris@909 40 validates_presence_of :board, :subject, :content
Chris@909 41 validates_length_of :subject, :maximum => 255
Chris@909 42 validate :cannot_reply_to_locked_topic, :on => :create
Chris@909 43
Chris@909 44 after_create :add_author_as_watcher, :update_parent_last_reply
Chris@909 45 after_update :update_messages_board
Chris@909 46 after_destroy :reset_board_counters
Chris@909 47
Chris@909 48 named_scope :visible, lambda {|*args| { :include => {:board => :project},
Chris@909 49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
Chris@909 50
Chris@909 51 def visible?(user=User.current)
Chris@909 52 !user.nil? && user.allowed_to?(:view_messages, project)
Chris@909 53 end
Chris@909 54
Chris@909 55 def cannot_reply_to_locked_topic
Chris@909 56 # Can not reply to a locked topic
Chris@909 57 errors.add :base, 'Topic is locked' if root.locked? && self != root
Chris@909 58 end
Chris@909 59
Chris@909 60 def update_parent_last_reply
Chris@909 61 if parent
Chris@909 62 parent.reload.update_attribute(:last_reply_id, self.id)
Chris@909 63 end
Chris@909 64 board.reset_counters!
Chris@909 65 end
Chris@909 66
Chris@909 67 def update_messages_board
Chris@909 68 if board_id_changed?
Chris@909 69 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
Chris@909 70 Board.reset_counters!(board_id_was)
Chris@909 71 Board.reset_counters!(board_id)
Chris@909 72 end
Chris@909 73 end
Chris@909 74
Chris@909 75 def reset_board_counters
Chris@909 76 board.reset_counters!
Chris@909 77 end
Chris@909 78
Chris@909 79 def sticky=(arg)
Chris@909 80 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
Chris@909 81 end
Chris@909 82
Chris@909 83 def sticky?
Chris@909 84 sticky == 1
Chris@909 85 end
Chris@909 86
Chris@909 87 def project
Chris@909 88 board.project
Chris@909 89 end
Chris@909 90
Chris@909 91 def editable_by?(usr)
Chris@909 92 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
Chris@909 93 end
Chris@909 94
Chris@909 95 def destroyable_by?(usr)
Chris@909 96 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
Chris@909 97 end
Chris@909 98
Chris@909 99 private
Chris@909 100
Chris@909 101 def add_author_as_watcher
Chris@909 102 Watcher.create(:watchable => self.root, :user => author)
Chris@909 103 end
Chris@909 104 end