You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

message.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # redMine - project management software
  2. # Copyright (C) 2006-2007 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. class Message < ActiveRecord::Base
  18. belongs_to :board
  19. belongs_to :author, :class_name => 'User', :foreign_key => 'author_id'
  20. acts_as_tree :counter_cache => :replies_count, :order => "#{Message.table_name}.created_on ASC"
  21. acts_as_attachable
  22. belongs_to :last_reply, :class_name => 'Message', :foreign_key => 'last_reply_id'
  23. acts_as_searchable :columns => ['subject', 'content'],
  24. :include => {:board => :project},
  25. :project_key => 'project_id',
  26. :date_column => "#{table_name}.created_on"
  27. acts_as_event :title => Proc.new {|o| "#{o.board.name}: #{o.subject}"},
  28. :description => :content,
  29. :type => Proc.new {|o| o.parent_id.nil? ? 'message' : 'reply'},
  30. :url => Proc.new {|o| {:controller => 'messages', :action => 'show', :board_id => o.board_id}.merge(o.parent_id.nil? ? {:id => o.id} :
  31. {:id => o.parent_id, :anchor => "message-#{o.id}"})}
  32. acts_as_activity_provider :find_options => {:include => [{:board => :project}, :author]},
  33. :author_key => :author_id
  34. acts_as_watchable
  35. attr_protected :locked, :sticky
  36. validates_presence_of :board, :subject, :content
  37. validates_length_of :subject, :maximum => 255
  38. after_create :add_author_as_watcher
  39. def validate_on_create
  40. # Can not reply to a locked topic
  41. errors.add_to_base 'Topic is locked' if root.locked? && self != root
  42. end
  43. def after_create
  44. if parent
  45. parent.reload.update_attribute(:last_reply_id, self.id)
  46. end
  47. board.reset_counters!
  48. end
  49. def after_update
  50. if board_id_changed?
  51. Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
  52. Board.reset_counters!(board_id_was)
  53. Board.reset_counters!(board_id)
  54. end
  55. end
  56. def after_destroy
  57. board.reset_counters!
  58. end
  59. def sticky?
  60. sticky == 1
  61. end
  62. def project
  63. board.project
  64. end
  65. def editable_by?(usr)
  66. usr && usr.logged? && (usr.allowed_to?(:edit_messages, project) || (self.author == usr && usr.allowed_to?(:edit_own_messages, project)))
  67. end
  68. def destroyable_by?(usr)
  69. usr && usr.logged? && (usr.allowed_to?(:delete_messages, project) || (self.author == usr && usr.allowed_to?(:delete_own_messages, project)))
  70. end
  71. private
  72. def add_author_as_watcher
  73. Watcher.create(:watchable => self.root, :user => author)
  74. end
  75. end