Mercurial > hg > soundsoftware-site
comparison app/models/.svn/text-base/issue_relation.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | af80e5618e9b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # redMine - project management software | |
2 # Copyright (C) 2006-2007 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 IssueRelation < ActiveRecord::Base | |
19 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' | |
20 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id' | |
21 | |
22 TYPE_RELATES = "relates" | |
23 TYPE_DUPLICATES = "duplicates" | |
24 TYPE_DUPLICATED = "duplicated" | |
25 TYPE_BLOCKS = "blocks" | |
26 TYPE_BLOCKED = "blocked" | |
27 TYPE_PRECEDES = "precedes" | |
28 TYPE_FOLLOWS = "follows" | |
29 | |
30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES }, | |
31 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by, :order => 2, :sym => TYPE_DUPLICATED }, | |
32 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates, :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES }, | |
33 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by, :order => 4, :sym => TYPE_BLOCKED }, | |
34 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks, :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS }, | |
35 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows, :order => 6, :sym => TYPE_FOLLOWS }, | |
36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES } | |
37 }.freeze | |
38 | |
39 validates_presence_of :issue_from, :issue_to, :relation_type | |
40 validates_inclusion_of :relation_type, :in => TYPES.keys | |
41 validates_numericality_of :delay, :allow_nil => true | |
42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id | |
43 | |
44 attr_protected :issue_from_id, :issue_to_id | |
45 | |
46 def validate | |
47 if issue_from && issue_to | |
48 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id | |
49 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations? | |
50 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from | |
51 errors.add_to_base :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to) | |
52 end | |
53 end | |
54 | |
55 def other_issue(issue) | |
56 (self.issue_from_id == issue.id) ? issue_to : issue_from | |
57 end | |
58 | |
59 # Returns the relation type for +issue+ | |
60 def relation_type_for(issue) | |
61 if TYPES[relation_type] | |
62 if self.issue_from_id == issue.id | |
63 relation_type | |
64 else | |
65 TYPES[relation_type][:sym] | |
66 end | |
67 end | |
68 end | |
69 | |
70 def label_for(issue) | |
71 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow | |
72 end | |
73 | |
74 def before_save | |
75 reverse_if_needed | |
76 | |
77 if TYPE_PRECEDES == relation_type | |
78 self.delay ||= 0 | |
79 else | |
80 self.delay = nil | |
81 end | |
82 set_issue_to_dates | |
83 end | |
84 | |
85 def set_issue_to_dates | |
86 soonest_start = self.successor_soonest_start | |
87 if soonest_start | |
88 issue_to.reschedule_after(soonest_start) | |
89 end | |
90 end | |
91 | |
92 def successor_soonest_start | |
93 return nil unless (TYPE_PRECEDES == self.relation_type) && (issue_from.start_date || issue_from.due_date) | |
94 (issue_from.due_date || issue_from.start_date) + 1 + delay | |
95 end | |
96 | |
97 def <=>(relation) | |
98 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] | |
99 end | |
100 | |
101 private | |
102 | |
103 # Reverses the relation if needed so that it gets stored in the proper way | |
104 def reverse_if_needed | |
105 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] | |
106 issue_tmp = issue_to | |
107 self.issue_to = issue_from | |
108 self.issue_from = issue_tmp | |
109 self.relation_type = TYPES[relation_type][:reverse] | |
110 end | |
111 end | |
112 end |