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 @ 1298:4f746d8966dd
History | View | Annotate | Download (3.58 KB)
| 1 | 909:cbb26bc654de | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1295:622f24f53b42 | Chris | # Copyright (C) 2006-2013 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 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 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # 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 | 909:cbb26bc654de | Chris | before_destroy :check_integrity
|
| 20 | 1115:433d4f72a19b | Chris | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" |
| 21 | 0:513646585e45 | Chris | acts_as_list |
| 22 | 909:cbb26bc654de | Chris | |
| 23 | 1115:433d4f72a19b | Chris | before_destroy :delete_workflow_rules
|
| 24 | 909:cbb26bc654de | Chris | after_save :update_default
|
| 25 | 0:513646585e45 | Chris | |
| 26 | validates_presence_of :name
|
||
| 27 | validates_uniqueness_of :name
|
||
| 28 | validates_length_of :name, :maximum => 30 |
||
| 29 | validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true |
||
| 30 | 909:cbb26bc654de | Chris | |
| 31 | 1295:622f24f53b42 | Chris | scope :sorted, lambda { order("#{table_name}.position ASC") } |
| 32 | 1115:433d4f72a19b | Chris | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
| 33 | 0:513646585e45 | Chris | |
| 34 | 909:cbb26bc654de | Chris | def update_default |
| 35 | 1115:433d4f72a19b | Chris | IssueStatus.update_all({:is_default => false}, ['id <> ?', id]) if self.is_default? |
| 36 | 909:cbb26bc654de | Chris | end
|
| 37 | |||
| 38 | 0:513646585e45 | Chris | # Returns the default status for new issues
|
| 39 | def self.default |
||
| 40 | 1115:433d4f72a19b | Chris | where(:is_default => true).first |
| 41 | 0:513646585e45 | Chris | end
|
| 42 | 909:cbb26bc654de | Chris | |
| 43 | 0:513646585e45 | Chris | # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+
|
| 44 | def self.update_issue_done_ratios |
||
| 45 | if Issue.use_status_for_done_ratio? |
||
| 46 | 1115:433d4f72a19b | Chris | IssueStatus.where("default_done_ratio >= 0").all.each do |status| |
| 47 | Issue.update_all({:done_ratio => status.default_done_ratio}, {:status_id => status.id}) |
||
| 48 | 0:513646585e45 | Chris | end
|
| 49 | end
|
||
| 50 | |||
| 51 | return Issue.use_status_for_done_ratio? |
||
| 52 | end
|
||
| 53 | |||
| 54 | # Returns an array of all statuses the given role can switch to
|
||
| 55 | # Uses association cache when called more than one time
|
||
| 56 | 245:051f544170fe | Chris | def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
| 57 | 0:513646585e45 | Chris | if roles && tracker
|
| 58 | role_ids = roles.collect(&:id)
|
||
| 59 | 245:051f544170fe | Chris | transitions = workflows.select do |w|
|
| 60 | role_ids.include?(w.role_id) && |
||
| 61 | 909:cbb26bc654de | Chris | w.tracker_id == tracker.id && |
| 62 | ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee)) |
||
| 63 | 245:051f544170fe | Chris | end
|
| 64 | 1115:433d4f72a19b | Chris | transitions.map(&:new_status).compact.sort
|
| 65 | 0:513646585e45 | Chris | else
|
| 66 | [] |
||
| 67 | end
|
||
| 68 | end
|
||
| 69 | 909:cbb26bc654de | Chris | |
| 70 | 0:513646585e45 | Chris | # Same thing as above but uses a database query
|
| 71 | # More efficient than the previous method if called just once
|
||
| 72 | 245:051f544170fe | Chris | def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
| 73 | 909:cbb26bc654de | Chris | if roles.present? && tracker
|
| 74 | conditions = "(author = :false AND assignee = :false)"
|
||
| 75 | conditions << " OR author = :true" if author |
||
| 76 | conditions << " OR assignee = :true" if assignee |
||
| 77 | |||
| 78 | 1115:433d4f72a19b | Chris | workflows. |
| 79 | includes(:new_status).
|
||
| 80 | where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})",
|
||
| 81 | 909:cbb26bc654de | Chris | {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false}
|
| 82 | 1115:433d4f72a19b | Chris | ]).all. |
| 83 | map(&:new_status).compact.sort
|
||
| 84 | 0:513646585e45 | Chris | else
|
| 85 | [] |
||
| 86 | end
|
||
| 87 | end
|
||
| 88 | |||
| 89 | def <=>(status) |
||
| 90 | position <=> status.position |
||
| 91 | end
|
||
| 92 | 909:cbb26bc654de | Chris | |
| 93 | 0:513646585e45 | Chris | def to_s; name end |
| 94 | |||
| 95 | 1115:433d4f72a19b | Chris | private |
| 96 | |||
| 97 | 0:513646585e45 | Chris | def check_integrity |
| 98 | 1115:433d4f72a19b | Chris | raise "Can't delete status" if Issue.where(:status_id => id).any? |
| 99 | 0:513646585e45 | Chris | end
|
| 100 | 909:cbb26bc654de | Chris | |
| 101 | 1:cca12e1c1fd4 | Chris | # Deletes associated workflows
|
| 102 | 1115:433d4f72a19b | Chris | def delete_workflow_rules |
| 103 | WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) |
||
| 104 | 1:cca12e1c1fd4 | Chris | end
|
| 105 | 0:513646585e45 | Chris | end |