annotate app/models/message.rb @ 861:b8105f717bf7 bug_182

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