board.rb
89 lines
| 3.2 KiB
| text/x-ruby
|
RubyLexer
|
r6726 | # Redmine - project management software | ||
|
r9453 | # Copyright (C) 2006-2012 Jean-Philippe Lang | ||
|
r526 | # | ||
# 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. | ||||
|
r6726 | # | ||
|
r526 | # This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
# GNU General Public License for more details. | ||||
|
r6726 | # | ||
|
r526 | # You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
class Board < ActiveRecord::Base | ||||
|
r9020 | include Redmine::SafeAttributes | ||
|
r526 | belongs_to :project | ||
has_many :topics, :class_name => 'Message', :conditions => "#{Message.table_name}.parent_id IS NULL", :order => "#{Message.table_name}.created_on DESC" | ||||
|
r4317 | has_many :messages, :dependent => :destroy, :order => "#{Message.table_name}.created_on DESC" | ||
|
r526 | belongs_to :last_message, :class_name => 'Message', :foreign_key => :last_message_id | ||
|
r9959 | acts_as_tree :dependent => :nullify | ||
acts_as_list :scope => '(project_id = #{project_id} AND parent_id #{parent_id ? "= #{parent_id}" : "IS NULL"})' | ||||
|
r527 | acts_as_watchable | ||
|
r6726 | |||
|
r526 | validates_presence_of :name, :description | ||
validates_length_of :name, :maximum => 30 | ||||
validates_length_of :description, :maximum => 255 | ||||
|
r9959 | validate :validate_board | ||
|
r6726 | |||
|
r9355 | scope :visible, lambda {|*args| { :include => :project, | ||
|
r7720 | :conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } } | ||
|
r9959 | safe_attributes 'name', 'description', 'parent_id', 'move_to' | ||
|
r9020 | |||
|
r3053 | def visible?(user=User.current) | ||
!user.nil? && user.allowed_to?(:view_messages, project) | ||||
end | ||||
|
r6726 | |||
|
r9959 | def reload(*args) | ||
@valid_parents = nil | ||||
super | ||||
end | ||||
|
r2590 | def to_s | ||
name | ||||
end | ||||
|
r6726 | |||
|
r9959 | def valid_parents | ||
@valid_parents ||= project.boards - self_and_descendants | ||||
end | ||||
|
r2560 | def reset_counters! | ||
self.class.reset_counters!(id) | ||||
end | ||||
|
r6726 | |||
|
r2560 | # Updates topics_count, messages_count and last_message_id attributes for +board_id+ | ||
def self.reset_counters!(board_id) | ||||
board_id = board_id.to_i | ||||
update_all("topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id} AND parent_id IS NULL)," + | ||||
" messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=#{board_id})," + | ||||
" last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=#{board_id})", | ||||
["id = ?", board_id]) | ||||
end | ||||
|
r9959 | |||
def self.board_tree(boards, parent_id=nil, level=0) | ||||
tree = [] | ||||
boards.select {|board| board.parent_id == parent_id}.sort_by(&:position).each do |board| | ||||
tree << [board, level] | ||||
tree += board_tree(boards, board.id, level+1) | ||||
end | ||||
if block_given? | ||||
tree.each do |board, level| | ||||
yield board, level | ||||
end | ||||
end | ||||
tree | ||||
end | ||||
protected | ||||
def validate_board | ||||
if parent_id && parent_id_changed? | ||||
errors.add(:parent_id, :invalid) unless valid_parents.include?(parent) | ||||
end | ||||
end | ||||
|
r526 | end | ||