Class: Message

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Redmine::SafeAttributes
Defined in:
app/models/message.rb

Overview

– copyright ChiliProject is a project management system.

Copyright (C) 2010-2013 the ChiliProject Team

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

See doc/COPYRIGHT.rdoc for more details. ++

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, included, #safe_attribute_names, #safe_attributes=

Instance Method Details

#after_createObject


67
68
69
70
71
72
# File 'app/models/message.rb', line 67

def after_create
  if parent
    parent.reload.update_attribute(:last_reply_id, self.id)
  end
  board.reset_counters!
end

#after_destroyObject


82
83
84
85
# File 'app/models/message.rb', line 82

def after_destroy
  parent.reset_last_reply_id! if parent
  board.reset_counters!
end

#after_updateObject


74
75
76
77
78
79
80
# File 'app/models/message.rb', line 74

def after_update
  if board_id_changed?
    Message.update_all("board_id = #{board_id}", ["id = ? OR parent_id = ?", root.id, root.id])
    Board.reset_counters!(board_id_was)
    Board.reset_counters!(board_id)
  end
end

#destroyable_by?(usr) ⇒ Boolean

Returns:

  • (Boolean)

108
109
110
# File 'app/models/message.rb', line 108

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

#editable_by?(usr) ⇒ Boolean

Returns:

  • (Boolean)

104
105
106
# File 'app/models/message.rb', line 104

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

#projectObject


100
101
102
# File 'app/models/message.rb', line 100

def project
  board.project
end

#reset_last_reply_id!Object


87
88
89
90
# File 'app/models/message.rb', line 87

def reset_last_reply_id!
  clid = children.present? ? children.last.id : nil
  self.update_attribute(:last_reply_id, clid)
end

#sticky=(arg) ⇒ Object


92
93
94
# File 'app/models/message.rb', line 92

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

#sticky?Boolean

Returns:

  • (Boolean)

96
97
98
# File 'app/models/message.rb', line 96

def sticky?
  sticky == 1
end

#validate_on_createObject


62
63
64
65
# File 'app/models/message.rb', line 62

def validate_on_create
  # Can not reply to a locked topic
  errors.add_to_base 'Topic is locked' if root.locked? && self != root
end

#visible?(user = User.current) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
# File 'app/models/message.rb', line 58

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