Class: Board

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

Overview

Redmine - project management software Copyright (C) 2006-2014 Jean-Philippe Lang

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.

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.

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 Method Summary collapse

Instance Method Summary collapse

Methods included from Redmine::SafeAttributes

#delete_unsafe_attributes, included, #safe_attribute?, #safe_attribute_names, #safe_attributes=

Class Method Details

.board_tree(boards, parent_id = nil, level = 0) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/models/board.rb', line 75

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

.reset_counters!(board_id) ⇒ Object

Updates topics_count, messages_count and last_message_id attributes for board_id


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

def self.reset_counters!(board_id)
  board_id = board_id.to_i
  Board.where(:id => board_id).
    update_all(["topics_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id AND parent_id IS NULL)," +
             " messages_count = (SELECT COUNT(*) FROM #{Message.table_name} WHERE board_id=:id)," +
             " last_message_id = (SELECT MAX(id) FROM #{Message.table_name} WHERE board_id=:id)", :id => board_id])
end

Instance Method Details

#reload(*args) ⇒ Object


44
45
46
47
# File 'app/models/board.rb', line 44

def reload(*args)
  @valid_parents = nil
  super
end

#reset_counters!Object


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

def reset_counters!
  self.class.reset_counters!(id)
end

#to_sObject


49
50
51
# File 'app/models/board.rb', line 49

def to_s
  name
end

#topicsObject

Returns a scope for the board topics (messages without parent)


54
55
56
# File 'app/models/board.rb', line 54

def topics
  messages.where(:parent_id => nil)
end

#valid_parentsObject


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

def valid_parents
  @valid_parents ||= project.boards - self_and_descendants
end

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

Returns:

  • (Boolean)

40
41
42
# File 'app/models/board.rb', line 40

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