To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / issue_status.rb @ 440:6253d777aa12
History | View | Annotate | Download (3.37 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_format_of :name, :with => /^[\w\s\'\-]*$/i |
| 29 |
validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true |
| 30 |
|
| 31 |
def after_save |
| 32 |
IssueStatus.update_all("is_default=#{connection.quoted_false}", ['id <> ?', id]) if self.is_default? |
| 33 |
end
|
| 34 |
|
| 35 |
# Returns the default status for new issues
|
| 36 |
def self.default |
| 37 |
find(:first, :conditions =>["is_default=?", true]) |
| 38 |
end
|
| 39 |
|
| 40 |
# Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
|
| 41 |
def self.update_issue_done_ratios |
| 42 |
if Issue.use_status_for_done_ratio? |
| 43 |
IssueStatus.find(:all, :conditions => ["default_done_ratio >= 0"]).each do |status| |
| 44 |
Issue.update_all(["done_ratio = ?", status.default_done_ratio], |
| 45 |
["status_id = ?", status.id])
|
| 46 |
end
|
| 47 |
end
|
| 48 |
|
| 49 |
return Issue.use_status_for_done_ratio? |
| 50 |
end
|
| 51 |
|
| 52 |
# Returns an array of all statuses the given role can switch to
|
| 53 |
# Uses association cache when called more than one time
|
| 54 |
def new_statuses_allowed_to(roles, tracker) |
| 55 |
if roles && tracker
|
| 56 |
role_ids = roles.collect(&:id)
|
| 57 |
new_statuses = workflows.select {|w| role_ids.include?(w.role_id) && w.tracker_id == tracker.id}.collect{|w| w.new_status}.compact.sort
|
| 58 |
else
|
| 59 |
[] |
| 60 |
end
|
| 61 |
end
|
| 62 |
|
| 63 |
# Same thing as above but uses a database query
|
| 64 |
# More efficient than the previous method if called just once
|
| 65 |
def find_new_statuses_allowed_to(roles, tracker) |
| 66 |
if roles && tracker
|
| 67 |
workflows.find(:all,
|
| 68 |
:include => :new_status, |
| 69 |
:conditions => { :role_id => roles.collect(&:id), |
| 70 |
:tracker_id => tracker.id}).collect{ |w| w.new_status }.compact.sort
|
| 71 |
else
|
| 72 |
[] |
| 73 |
end
|
| 74 |
end
|
| 75 |
|
| 76 |
def new_status_allowed_to?(status, roles, tracker) |
| 77 |
if status && roles && tracker
|
| 78 |
!workflows.find(:first, :conditions => {:new_status_id => status.id, :role_id => roles.collect(&:id), :tracker_id => tracker.id}).nil? |
| 79 |
else
|
| 80 |
false
|
| 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
|