To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / message.rb @ 1027:b0e0ffb43fa1

History | View | Annotate | Download (4.07 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2011  Jean-Philippe Lang
3
#
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
#
9
# 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
#
14
# 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
  include Redmine::SafeAttributes
20
  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

    
26
  acts_as_searchable :columns => ['subject', 'content'],
27
                     :include => {:board => :project},
28
                     :project_key => "#{Board.table_name}.project_id",
29
                     :date_column => "#{table_name}.created_on"
30
  acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
31
                :description => :content,
32
                :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
33
                :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
34
                                                                                                                                       {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
35

    
36
  acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
37
                            :author_key => :author_id
38
  acts_as_watchable
39

    
40
  validates_presence_of :board, :subject, :content
41
  validates_length_of :subject, :maximum => 255
42
  validate :cannot_reply_to_locked_topic, :on => :create
43

    
44
  after_create :add_author_as_watcher, :update_parent_last_reply
45
  after_update :update_messages_board
46
  after_destroy :reset_board_counters
47

    
48
  named_scope :visible, lambda {|*args| { :include => {:board => :project},
49
                                          :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
50

    
51
  safe_attributes 'subject', 'content'
52
  safe_attributes 'locked', 'sticky', 'board_id',
53
    :if => lambda {|message, user|
54
      user.allowed_to?(:edit_messages, message.project)
55
    }
56

    
57
  def visible?(user=User.current)
58
    !user.nil? && user.allowed_to?(:view_messages, project)
59
  end
60

    
61
  def cannot_reply_to_locked_topic
62
    # Can not reply to a locked topic
63
    errors.add :base, 'Topic is locked' if root.locked? && self != root
64
  end
65

    
66
  def update_parent_last_reply
67
    if parent
68
      parent.reload.update_attribute(:last_reply_id, self.id)
69
    end
70
    board.reset_counters!
71
  end
72

    
73
  def update_messages_board
74
    if board_id_changed?
75
      Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
76
      Board.reset_counters!(board_id_was)
77
      Board.reset_counters!(board_id)
78
    end
79
  end
80

    
81
  def reset_board_counters
82
    board.reset_counters!
83
  end
84

    
85
  def sticky=(arg)
86
    write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
87
  end
88

    
89
  def sticky?
90
    sticky == 1
91
  end
92

    
93
  def project
94
    board.project
95
  end
96

    
97
  def editable_by?(usr)
98
    usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
99
  end
100

    
101
  def destroyable_by?(usr)
102
    usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
103
  end
104

    
105
  private
106

    
107
  def add_author_as_watcher
108
    Watcher.create(:watchable => self.root, :user => author)
109
  end
110
end