annotate .svn/pristine/90/90aec66985f72f5aae2d43342147a899e2ef0d82.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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