@@ -1,123 +1,124 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2014 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class IssueStatus < ActiveRecord::Base |
|
18 | class IssueStatus < ActiveRecord::Base | |
19 | before_destroy :check_integrity |
|
19 | before_destroy :check_integrity | |
20 | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" |
|
20 | has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id" | |
21 | acts_as_list |
|
21 | acts_as_list | |
22 |
|
22 | |||
23 | after_update :handle_is_closed_change |
|
23 | after_update :handle_is_closed_change | |
24 | before_destroy :delete_workflow_rules |
|
24 | before_destroy :delete_workflow_rules | |
25 | after_save :update_default |
|
25 | after_save :update_default | |
26 |
|
26 | |||
27 | validates_presence_of :name |
|
27 | validates_presence_of :name | |
28 | validates_uniqueness_of :name |
|
28 | validates_uniqueness_of :name | |
29 | validates_length_of :name, :maximum => 30 |
|
29 | validates_length_of :name, :maximum => 30 | |
30 | validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true |
|
30 | validates_inclusion_of :default_done_ratio, :in => 0..100, :allow_nil => true | |
31 |
|
31 | |||
32 | scope :sorted, lambda { order("#{table_name}.position ASC") } |
|
32 | scope :sorted, lambda { order("#{table_name}.position ASC") } | |
33 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} |
|
33 | scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)} | |
34 |
|
34 | |||
35 | def update_default |
|
35 | def update_default | |
36 | IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default? |
|
36 | IssueStatus.where(['id <> ?', id]).update_all({:is_default => false}) if self.is_default? | |
37 | end |
|
37 | end | |
38 |
|
38 | |||
39 | # Returns the default status for new issues |
|
39 | # Returns the default status for new issues | |
40 | def self.default |
|
40 | def self.default | |
41 | where(:is_default => true).first |
|
41 | where(:is_default => true).first | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+ |
|
44 | # Update all the +Issues+ setting their done_ratio to the value of their +IssueStatus+ | |
45 | def self.update_issue_done_ratios |
|
45 | def self.update_issue_done_ratios | |
46 | if Issue.use_status_for_done_ratio? |
|
46 | if Issue.use_status_for_done_ratio? | |
47 | IssueStatus.where("default_done_ratio >= 0").each do |status| |
|
47 | IssueStatus.where("default_done_ratio >= 0").each do |status| | |
48 | Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio}) |
|
48 | Issue.where({:status_id => status.id}).update_all({:done_ratio => status.default_done_ratio}) | |
49 | end |
|
49 | end | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | return Issue.use_status_for_done_ratio? |
|
52 | return Issue.use_status_for_done_ratio? | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | # Returns an array of all statuses the given role can switch to |
|
55 | # Returns an array of all statuses the given role can switch to | |
56 | # Uses association cache when called more than one time |
|
56 | # Uses association cache when called more than one time | |
57 | def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
|
57 | def new_statuses_allowed_to(roles, tracker, author=false, assignee=false) | |
58 | if roles && tracker |
|
58 | if roles && tracker | |
59 | role_ids = roles.collect(&:id) |
|
59 | role_ids = roles.collect(&:id) | |
60 | transitions = workflows.select do |w| |
|
60 | transitions = workflows.select do |w| | |
61 | role_ids.include?(w.role_id) && |
|
61 | role_ids.include?(w.role_id) && | |
62 | w.tracker_id == tracker.id && |
|
62 | w.tracker_id == tracker.id && | |
63 | ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee)) |
|
63 | ((!w.author && !w.assignee) || (author && w.author) || (assignee && w.assignee)) | |
64 | end |
|
64 | end | |
65 | transitions.map(&:new_status).compact.sort |
|
65 | transitions.map(&:new_status).compact.sort | |
66 | else |
|
66 | else | |
67 | [] |
|
67 | [] | |
68 | end |
|
68 | end | |
69 | end |
|
69 | end | |
70 |
|
70 | |||
71 | # Same thing as above but uses a database query |
|
71 | # Same thing as above but uses a database query | |
72 | # More efficient than the previous method if called just once |
|
72 | # More efficient than the previous method if called just once | |
73 | def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) |
|
73 | def find_new_statuses_allowed_to(roles, tracker, author=false, assignee=false) | |
74 | if roles.present? && tracker |
|
74 | if roles.present? && tracker | |
75 | conditions = "(author = :false AND assignee = :false)" |
|
75 | conditions = "(author = :false AND assignee = :false)" | |
76 | conditions << " OR author = :true" if author |
|
76 | conditions << " OR author = :true" if author | |
77 | conditions << " OR assignee = :true" if assignee |
|
77 | conditions << " OR assignee = :true" if assignee | |
78 |
|
78 | |||
79 | workflows. |
|
79 | workflows. | |
80 | includes(:new_status). |
|
80 | includes(:new_status). | |
81 | where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})", |
|
81 | where(["role_id IN (:role_ids) AND tracker_id = :tracker_id AND (#{conditions})", | |
82 | {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false} |
|
82 | {:role_ids => roles.collect(&:id), :tracker_id => tracker.id, :true => true, :false => false} | |
83 | ]).all. |
|
83 | ]).all. | |
84 | map(&:new_status).compact.sort |
|
84 | map(&:new_status).compact.sort | |
85 | else |
|
85 | else | |
86 | [] |
|
86 | [] | |
87 | end |
|
87 | end | |
88 | end |
|
88 | end | |
89 |
|
89 | |||
90 | def <=>(status) |
|
90 | def <=>(status) | |
91 | position <=> status.position |
|
91 | position <=> status.position | |
92 | end |
|
92 | end | |
93 |
|
93 | |||
94 | def to_s; name end |
|
94 | def to_s; name end | |
95 |
|
95 | |||
96 | private |
|
96 | private | |
97 |
|
97 | |||
98 | # Updates issues closed_on attribute when an existing status is set as closed. |
|
98 | # Updates issues closed_on attribute when an existing status is set as closed. | |
99 | def handle_is_closed_change |
|
99 | def handle_is_closed_change | |
100 | if is_closed_changed? && is_closed == true |
|
100 | if is_closed_changed? && is_closed == true | |
101 | # First we update issues that have a journal for when the current status was set, |
|
101 | # First we update issues that have a journal for when the current status was set, | |
102 | # a subselect is used to update all issues with a single query |
|
102 | # a subselect is used to update all issues with a single query | |
103 | subselect = "SELECT MAX(j.created_on) FROM #{Journal.table_name} j" + |
|
103 | subselect = "SELECT MAX(j.created_on) FROM #{Journal.table_name} j" + | |
104 | " JOIN #{JournalDetail.table_name} d ON d.journal_id = j.id" + |
|
104 | " JOIN #{JournalDetail.table_name} d ON d.journal_id = j.id" + | |
105 | " WHERE j.journalized_type = 'Issue' AND j.journalized_id = #{Issue.table_name}.id" + |
|
105 | " WHERE j.journalized_type = 'Issue' AND j.journalized_id = #{Issue.table_name}.id" + | |
106 | " AND d.property = 'attr' AND d.prop_key = 'status_id' AND d.value = :status_id" |
|
106 | " AND d.property = 'attr' AND d.prop_key = 'status_id' AND d.value = :status_id" | |
107 |
Issue.where(:status_id => id, :closed_on => nil). |
|
107 | Issue.where(:status_id => id, :closed_on => nil). | |
|
108 | update_all(["closed_on = (#{subselect})", {:status_id => id.to_s}]) | |||
108 |
|
109 | |||
109 | # Then we update issues that don't have a journal which means the |
|
110 | # Then we update issues that don't have a journal which means the | |
110 | # current status was set on creation |
|
111 | # current status was set on creation | |
111 | Issue.where(:status_id => id, :closed_on => nil).update_all("closed_on = created_on") |
|
112 | Issue.where(:status_id => id, :closed_on => nil).update_all("closed_on = created_on") | |
112 | end |
|
113 | end | |
113 | end |
|
114 | end | |
114 |
|
115 | |||
115 | def check_integrity |
|
116 | def check_integrity | |
116 | raise "Can't delete status" if Issue.where(:status_id => id).any? |
|
117 | raise "Can't delete status" if Issue.where(:status_id => id).any? | |
117 | end |
|
118 | end | |
118 |
|
119 | |||
119 | # Deletes associated workflows |
|
120 | # Deletes associated workflows | |
120 | def delete_workflow_rules |
|
121 | def delete_workflow_rules | |
121 | WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) |
|
122 | WorkflowRule.delete_all(["old_status_id = :id OR new_status_id = :id", {:id => id}]) | |
122 | end |
|
123 | end | |
123 | end |
|
124 | end |
General Comments 0
You need to be logged in to leave comments.
Login now