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 / .svn / text-base / message.rb.svn-base @ 441:cbce1fd3b1b7

History | View | Annotate | Download (3.7 KB)

1 441:cbce1fd3b1b7 Chris
# Redmine - project management software
2
# Copyright (C) 2006-2011  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
  belongs_to :board
20
  belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
21
  acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
22
  acts_as_attachable
23
  belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
24 441:cbce1fd3b1b7 Chris
25 0:513646585e45 Chris
  acts_as_searchable :columns => ['subject', 'content'],
26
                     :include => {:board => :project},
27 441:cbce1fd3b1b7 Chris
                     :project_key => "#{Board.table_name}.project_id",
28 0:513646585e45 Chris
                     :date_column => "#{table_name}.created_on"
29
  acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
30
                :description => :content,
31
                :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
32
                :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
33
                                                                                                                                       {:id => o.parent_id, :r => o.id, :anchor => "message-#{o.id}"})}
34
35
  acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
36
                            :author_key => :author_id
37
  acts_as_watchable
38 441:cbce1fd3b1b7 Chris
39 0:513646585e45 Chris
  attr_protected :locked, :sticky
40
  validates_presence_of :board, :subject, :content
41
  validates_length_of :subject, :maximum => 255
42 441:cbce1fd3b1b7 Chris
43 0:513646585e45 Chris
  after_create :add_author_as_watcher
44 441:cbce1fd3b1b7 Chris
45 210:0579821a129a Chris
  named_scope :visible, lambda {|*args| { :include => {:board => :project},
46 441:cbce1fd3b1b7 Chris
                                          :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
47
48 0:513646585e45 Chris
  def visible?(user=User.current)
49
    !user.nil? && user.allowed_to?(:view_messages, project)
50
  end
51 441:cbce1fd3b1b7 Chris
52 0:513646585e45 Chris
  def validate_on_create
53
    # Can not reply to a locked topic
54
    errors.add_to_base 'Topic is locked' if root.locked? && self != root
55
  end
56 441:cbce1fd3b1b7 Chris
57 0:513646585e45 Chris
  def after_create
58
    if parent
59
      parent.reload.update_attribute(:last_reply_id, self.id)
60
    end
61
    board.reset_counters!
62
  end
63 441:cbce1fd3b1b7 Chris
64 0:513646585e45 Chris
  def after_update
65
    if board_id_changed?
66
      Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
67
      Board.reset_counters!(board_id_was)
68
      Board.reset_counters!(board_id)
69
    end
70
  end
71 441:cbce1fd3b1b7 Chris
72 0:513646585e45 Chris
  def after_destroy
73
    board.reset_counters!
74
  end
75 441:cbce1fd3b1b7 Chris
76 0:513646585e45 Chris
  def sticky=(arg)
77
    write_attribute :sticky, (arg == true || arg.to_s == '1' ? 1 : 0)
78
  end
79 441:cbce1fd3b1b7 Chris
80 0:513646585e45 Chris
  def sticky?
81
    sticky == 1
82
  end
83 441:cbce1fd3b1b7 Chris
84 0:513646585e45 Chris
  def project
85
    board.project
86
  end
87
88
  def editable_by?(usr)
89
    usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
90
  end
91
92
  def destroyable_by?(usr)
93
    usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
94
  end
95 441:cbce1fd3b1b7 Chris
96 0:513646585e45 Chris
  private
97 441:cbce1fd3b1b7 Chris
98 0:513646585e45 Chris
  def add_author_as_watcher
99
    Watcher.create(:watchable => self.root, :user => author)
100
  end
101
end