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.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 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 < ActiveRecord::Base
  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 {|*args|
  31. joins(:project).
  32. where(Project.allowed_to_condition(args.shift || User.current, :view_messages, *args))
  33. }
  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(["topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id AND parent_id IS NULL)," +
  60. " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id)," +
  61. " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=:id)", :id => board_id])
  62. end
  63. def self.board_tree(boards, parent_id=nil, level=0)
  64. tree = []
  65. boards.select {|board| board.parent_id == parent_id}.sort_by(&:position).each do |board|
  66. tree << [board, level]
  67. tree += board_tree(boards, board.id, level+1)
  68. end
  69. if block_given?
  70. tree.each do |board, level|
  71. yield board, level
  72. end
  73. end
  74. tree
  75. end
  76. protected
  77. def validate_board
  78. if parent_id && parent_id_changed?
  79. errors.add(:parent_id, :invalid) unless valid_parents.include?(parent)
  80. end
  81. end
  82. end