annotate .svn/pristine/a6/a630a39c4ba7dc7a2645fe3c5b9313ac357232d8.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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