To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / .svn / text-base / issue_status.rb.svn-base @ 442:753f1380d6bc

History | View | Annotate | Download (3.32 KB)

1
# redMine - project management software
2
# Copyright (C) 2006  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class IssueStatus < ActiveRecord::Base
19
  before_destroy :check_integrity  
20
  has_many :workflows, :foreign_key => "old_status_id"
21
  acts_as_list
22
  
23
  before_destroy :delete_workflows
24

    
25
  validates_presence_of :name
26
  validates_uniqueness_of :name
27
  validates_length_of :name, :maximum => 30
28
  validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true
29

    
30
  def after_save
31
    IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default?
32
  end  
33
  
34
  # Returns the default status for new issues
35
  def self.default
36
    find(:first, :conditions =>["is_default=?", true])
37
  end
38
  
39
  # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
40
  def self.update_issue_done_ratios
41
    if Issue.use_status_for_done_ratio?
42
      IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status|
43
        Issue.update_all(["done_ratio = ?", status.default_done_ratio],
44
                         ["status_id = ?", status.id])
45
      end
46
    end
47

    
48
    return Issue.use_status_for_done_ratio?
49
  end
50

    
51
  # Returns an array of all statuses the given role can switch to
52
  # Uses association cache when called more than one time
53
  def new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
54
    if roles && tracker
55
      role_ids = roles.collect(&:id)
56
      transitions = workflows.select do |w|
57
        role_ids.include?(w.role_id) &&
58
        w.tracker_id == tracker.id && 
59
        (author || !w.author) &&
60
        (assignee || !w.assignee)
61
      end
62
      transitions.collect{|w| w.new_status}.compact.sort
63
    else
64
      []
65
    end
66
  end
67
  
68
  # Same thing as above but uses a database query
69
  # More efficient than the previous method if called just once
70
  def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false)
71
    if roles && tracker
72
      conditions = {:role_id => roles.collect(&:id), :tracker_id => tracker.id}
73
      conditions[:author] = false unless author
74
      conditions[:assignee] = false unless assignee
75
      
76
      workflows.find(:all,
77
                     :include => :new_status,
78
                     :conditions => conditions).collect{|w| w.new_status}.compact.sort
79
    else
80
      []
81
    end
82
  end
83

    
84
  def <=>(status)
85
    position <=> status.position
86
  end
87
  
88
  def to_s; name end
89

    
90
private
91
  def check_integrity
92
    raise "Can't delete status" if Issue.find(:first, :conditions => ["status_id=?", self.id])
93
  end
94
  
95
  # Deletes associated workflows
96
  def delete_workflows
97
    Workflow.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}])
98
  end
99
end