annotate app/models/message.rb @ 1472:f0b798dad2d6 feature_526

Close obsolete branch feature_526
author Chris Cannam
date Tue, 20 Nov 2012 19:45:51 +0000
parents 5f33065ddc4b
children 433d4f72a19b
rev   line source
Chris@441 1 # Redmine - project management software
Chris@441 2 # Copyright (C) 2006-2011 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@441 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@441 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@929 19 include Redmine::SafeAttributes
Chris@0 20 belongs_to :board
Chris@0 21 belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
Chris@0 22 acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
Chris@0 23 acts_as_attachable
Chris@0 24 belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
Chris@441 25
Chris@0 26 acts_as_searchable :columns => ['subject', 'content'],
Chris@0 27 :include => {:board => :project},
Chris@441 28 :project_key => "#{Board.table_name}.project_id",
Chris@0 29 :date_column => "#{table_name}.created_on"
Chris@0 30 acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
Chris@0 31 :description => :content,
Chris@0 32 :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
Chris@909 33 :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
Chris@0 34 {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
Chris@0 35
Chris@0 36 acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
Chris@0 37 :author_key => :author_id
Chris@0 38 acts_as_watchable
Chris@441 39
Chris@0 40 validates_presence_of :board, :subject, :content
Chris@0 41 validates_length_of :subject, :maximum => 255
Chris@909 42 validate :cannot_reply_to_locked_topic, :on => :create
Chris@441 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@441 47
Chris@210 48 named_scope :visible, lambda {|*args| { :include => {:board => :project},
Chris@441 49 :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
Chris@441 50
Chris@929 51 safe_attributes 'subject', 'content'
Chris@929 52 safe_attributes 'locked', 'sticky', 'board_id',
Chris@929 53 :if => lambda {|message, user|
Chris@929 54 user.allowed_to?(:edit_messages, message.project)
Chris@929 55 }
Chris@929 56
Chris@0 57 def visible?(user=User.current)
Chris@0 58 !user.nil? && user.allowed_to?(:view_messages, project)
Chris@0 59 end
Chris@441 60
Chris@909 61 def cannot_reply_to_locked_topic
Chris@0 62 # Can not reply to a locked topic
Chris@909 63 errors.add :base, 'Topic is locked' if root.locked? && self != root
Chris@0 64 end
Chris@441 65
Chris@909 66 def update_parent_last_reply
Chris@0 67 if parent
Chris@0 68 parent.reload.update_attribute(:last_reply_id, self.id)
Chris@0 69 end
Chris@0 70 board.reset_counters!
Chris@0 71 end
Chris@441 72
Chris@909 73 def update_messages_board
Chris@0 74 if board_id_changed?
Chris@0 75 Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
Chris@0 76 Board.reset_counters!(board_id_was)
Chris@0 77 Board.reset_counters!(board_id)
Chris@0 78 end
Chris@0 79 end
Chris@441 80
Chris@909 81 def reset_board_counters
Chris@0 82 board.reset_counters!
Chris@0 83 end
Chris@441 84
Chris@0 85 def sticky=(arg)
Chris@0 86 write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
Chris@0 87 end
Chris@441 88
Chris@0 89 def sticky?
Chris@0 90 sticky == 1
Chris@0 91 end
Chris@441 92
Chris@0 93 def project
Chris@0 94 board.project
Chris@0 95 end
Chris@0 96
Chris@0 97 def editable_by?(usr)
Chris@0 98 usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
Chris@0 99 end
Chris@0 100
Chris@0 101 def destroyable_by?(usr)
Chris@0 102 usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
Chris@0 103 end
Chris@441 104
Chris@0 105 private
Chris@441 106
Chris@0 107 def add_author_as_watcher
Chris@0 108 Watcher.create(:watchable => self.root, :user => author)
Chris@0 109 end
Chris@0 110 end