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.

board.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Board < ApplicationRecord
  19. include Redmine::SafeAttributes
  20. belongs_to :project
  21. has_many :messages, lambda {order("#{Message.table_name}.created_on DESC")}, :dependent => :destroy
  22. belongs_to :last_message, :class_name => 'Message'
  23. acts_as_tree :dependent => :nullify
  24. acts_as_positioned :scope => [:project_id, :parent_id]
  25. acts_as_watchable
  26. validates_presence_of :name, :description
  27. validates_length_of :name, :maximum => 30
  28. validates_length_of :description, :maximum => 255
  29. validate :validate_board
  30. scope :visible, (lambda do |*args|
  31. joins(:project).
  32. where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
  33. end)
  34. safe_attributes 'name', 'description', 'parent_id', 'position'
  35. def visible?(user=User.current)
  36. !user.nil? && user.allowed_to?(:view_messages, project)
  37. end
  38. def reload(*args)
  39. @valid_parents = nil
  40. super
  41. end
  42. def to_s
  43. name
  44. end
  45. # Returns a scope for the board topics (messages without parent)
  46. def topics
  47. messages.where(:parent_id => nil)
  48. end
  49. def valid_parents
  50. @valid_parents ||= project.boards - self_and_descendants
  51. end
  52. def reset_counters!
  53. self.class.reset_counters!(id)
  54. end
  55. # Updates topics_count, messages_count and last_message_id attributes for +board_id+
  56. def self.reset_counters!(board_id)
  57. board_id = board_id.to_i
  58. Board.where(:id => board_id).
  59. update_all(
  60. ["topics_count = (SELECT COUNT(*) FROM #{Message.table_name}" \
  61. " WHERE board_id=:id AND parent_id IS NULL)," \
  62. " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id)," \
  63. " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=:id)",
  64. :id => board_id]
  65. )
  66. end
  67. def self.board_tree(boards, parent_id=nil, level=0)
  68. tree = []
  69. boards.select {|board| board.parent_id == parent_id}.sort_by(&:position).each do |board|
  70. tree << [board, level]
  71. tree += board_tree(boards, board.id, level+1)
  72. end
  73. if block_given?
  74. tree.each do |board, level|
  75. yield board, level
  76. end
  77. end
  78. tree
  79. end
  80. protected
  81. def validate_board
  82. if parent_id && parent_id_changed?
  83. errors.add(:parent_id, :invalid) unless valid_parents.include?(parent)
  84. end
  85. end
  86. end