summaryrefslogtreecommitdiffstats
path: root/app/models/board.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2012-07-31 17:17:52 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2012-07-31 17:17:52 +0000
commitbc153cb61df37e2097c5bec529cabdb15e6887bf (patch)
tree05be0525dc3258c00349e7cc6b5ab333ca4139c0 /app/models/board.rb
parent9554f0133ff634264eb110c91ad317be28e6dd32 (diff)
downloadredmine-bc153cb61df37e2097c5bec529cabdb15e6887bf.tar.gz
redmine-bc153cb61df37e2097c5bec529cabdb15e6887bf.zip
Support for subforums (#3831).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@10142 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/board.rb')
-rw-r--r--app/models/board.rb37
1 files changed, 35 insertions, 2 deletions
diff --git a/app/models/board.rb b/app/models/board.rb
index 0b7d36667..42b5499e1 100644
--- a/app/models/board.rb
+++ b/app/models/board.rb
@@ -21,26 +21,37 @@ class Board < ActiveRecord::Base
has_many :topics, :class_name => 'Message', :conditions => "#{Message.table_name}.parent_id IS NULL", :order => "#{Message.table_name}.created_on DESC"
has_many :messages, :dependent => :destroy, :order => "#{Message.table_name}.created_on DESC"
belongs_to :last_message, :class_name => 'Message', :foreign_key => :last_message_id
- acts_as_list :scope => :project_id
+ acts_as_tree :dependent => :nullify
+ acts_as_list :scope => '(project_id = #{project_id} AND parent_id #{parent_id ? "= #{parent_id}" : "IS NULL"})'
acts_as_watchable
validates_presence_of :name, :description
validates_length_of :name, :maximum => 30
validates_length_of :description, :maximum => 255
+ validate :validate_board
scope :visible, lambda {|*args| { :include => :project,
:conditions => Project.allowed_to_condition(args.shift || User.current, :view_messages, *args) } }
- safe_attributes 'name', 'description', 'move_to'
+ safe_attributes 'name', 'description', 'parent_id', 'move_to'
def visible?(user=User.current)
!user.nil? && user.allowed_to?(:view_messages, project)
end
+ def reload(*args)
+ @valid_parents = nil
+ super
+ end
+
def to_s
name
end
+ def valid_parents
+ @valid_parents ||= project.boards - self_and_descendants
+ end
+
def reset_counters!
self.class.reset_counters!(id)
end
@@ -53,4 +64,26 @@ class Board < ActiveRecord::Base
" last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=#{board_id})",
["id = ?", board_id])
end
+
+ 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
end