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