@@ -1,96 +1,101 | |||
|
1 | 1 | # redMine - project management software |
|
2 | 2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class IssueRelation < ActiveRecord::Base |
|
19 | 19 | belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' |
|
20 | 20 | belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' |
|
21 | 21 | |
|
22 | 22 | TYPE_RELATES = "relates" |
|
23 | 23 | TYPE_DUPLICATES = "duplicates" |
|
24 | TYPE_DUPLICATED = "duplicated" | |
|
24 | 25 | TYPE_BLOCKS = "blocks" |
|
26 | TYPE_BLOCKED = "blocked" | |
|
25 | 27 | TYPE_PRECEDES = "precedes" |
|
26 | 28 | TYPE_FOLLOWS = "follows" |
|
27 | 29 | |
|
28 | 30 | TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1 }, |
|
29 | 31 | TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2 }, |
|
30 |
TYPE_ |
|
|
31 |
TYPE_ |
|
|
32 |
TYPE_ |
|
|
32 | TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :reverse => TYPE_DUPLICATES }, | |
|
33 | TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4 }, | |
|
34 | TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :reverse => TYPE_BLOCKS }, | |
|
35 | TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6 }, | |
|
36 | TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :reverse => TYPE_PRECEDES } | |
|
33 | 37 | }.freeze |
|
34 | 38 | |
|
35 | 39 | validates_presence_of :issue_from, :issue_to, :relation_type |
|
36 | 40 | validates_inclusion_of :relation_type, :in => TYPES.keys |
|
37 | 41 | validates_numericality_of :delay, :allow_nil => true |
|
38 | 42 | validates_uniqueness_of :issue_to_id, :scope => :issue_from_id |
|
39 | 43 | |
|
40 | 44 | attr_protected :issue_from_id, :issue_to_id |
|
41 | 45 | |
|
42 | 46 | def validate |
|
43 | 47 | if issue_from && issue_to |
|
44 | 48 | errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id |
|
45 | 49 | errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations? |
|
46 | 50 | errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from |
|
47 | 51 | end |
|
48 | 52 | end |
|
49 | 53 | |
|
50 | 54 | def other_issue(issue) |
|
51 | 55 | (self.issue_from_id == issue.id) ? issue_to : issue_from |
|
52 | 56 | end |
|
53 | 57 | |
|
54 | 58 | def label_for(issue) |
|
55 | 59 | TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow |
|
56 | 60 | end |
|
57 | 61 | |
|
58 | 62 | def before_save |
|
59 | 63 | reverse_if_needed |
|
60 | 64 | |
|
61 | 65 | if TYPE_PRECEDES == relation_type |
|
62 | 66 | self.delay ||= 0 |
|
63 | 67 | else |
|
64 | 68 | self.delay = nil |
|
65 | 69 | end |
|
66 | 70 | set_issue_to_dates |
|
67 | 71 | end |
|
68 | 72 | |
|
69 | 73 | def set_issue_to_dates |
|
70 | 74 | soonest_start = self.successor_soonest_start |
|
71 | 75 | if soonest_start && (!issue_to.start_date || issue_to.start_date < soonest_start) |
|
72 | 76 | issue_to.start_date, issue_to.due_date = successor_soonest_start, successor_soonest_start + issue_to.duration |
|
73 | 77 | issue_to.save |
|
74 | 78 | end |
|
75 | 79 | end |
|
76 | 80 | |
|
77 | 81 | def successor_soonest_start |
|
78 | 82 | return nil unless (TYPE_PRECEDES == self.relation_type) && (issue_from.start_date || issue_from.due_date) |
|
79 | 83 | (issue_from.due_date || issue_from.start_date) + 1 + delay |
|
80 | 84 | end |
|
81 | 85 | |
|
82 | 86 | def <=>(relation) |
|
83 | 87 | TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] |
|
84 | 88 | end |
|
85 | 89 | |
|
86 | 90 | private |
|
87 | 91 | |
|
92 | # Reverses the relation if needed so that it gets stored in the proper way | |
|
88 | 93 | def reverse_if_needed |
|
89 | if (TYPE_FOLLOWS == relation_type) | |
|
94 | if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] | |
|
90 | 95 | issue_tmp = issue_to |
|
91 | 96 | self.issue_to = issue_from |
|
92 | 97 | self.issue_from = issue_tmp |
|
93 |
self.relation_type = TYPE |
|
|
98 | self.relation_type = TYPES[relation_type][:reverse] | |
|
94 | 99 | end |
|
95 | 100 | end |
|
96 | 101 | end |
General Comments 0
You need to be logged in to leave comments.
Login now