comparison app/models/issue_relation.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents 0c939c159af4
children 433d4f72a19b
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 # redMine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang 2 # Copyright (C) 2006-2011 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 IssueRelation < ActiveRecord::Base 18 class IssueRelation < ActiveRecord::Base
19 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id' 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' 20 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
21 21
22 TYPE_RELATES = "relates" 22 TYPE_RELATES = "relates"
23 TYPE_DUPLICATES = "duplicates" 23 TYPE_DUPLICATES = "duplicates"
24 TYPE_DUPLICATED = "duplicated" 24 TYPE_DUPLICATED = "duplicated"
25 TYPE_BLOCKS = "blocks" 25 TYPE_BLOCKS = "blocks"
26 TYPE_BLOCKED = "blocked" 26 TYPE_BLOCKED = "blocked"
27 TYPE_PRECEDES = "precedes" 27 TYPE_PRECEDES = "precedes"
28 TYPE_FOLLOWS = "follows" 28 TYPE_FOLLOWS = "follows"
29 29
30 TYPES = { TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to, :order => 1, :sym => TYPE_RELATES }, 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 }, 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 }, 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 }, 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 }, 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 }, 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 } 36 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes, :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES }
37 }.freeze 37 }.freeze
38 38
39 validates_presence_of :issue_from, :issue_to, :relation_type 39 validates_presence_of :issue_from, :issue_to, :relation_type
40 validates_inclusion_of :relation_type, :in => TYPES.keys 40 validates_inclusion_of :relation_type, :in => TYPES.keys
41 validates_numericality_of :delay, :allow_nil => true 41 validates_numericality_of :delay, :allow_nil => true
42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id 42 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
43 43
44 validate :validate_issue_relation
45
44 attr_protected :issue_from_id, :issue_to_id 46 attr_protected :issue_from_id, :issue_to_id
45 47
46 def validate 48 before_save :handle_issue_order
49
50 def visible?(user=User.current)
51 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
52 end
53
54 def deletable?(user=User.current)
55 visible?(user) &&
56 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
57 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
58 end
59
60 def after_initialize
61 if new_record?
62 if relation_type.blank?
63 self.relation_type = IssueRelation::TYPE_RELATES
64 end
65 end
66 end
67
68 def validate_issue_relation
47 if issue_from && issue_to 69 if issue_from && issue_to
48 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id 70 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? 71 errors.add :issue_to_id, :not_same_project unless issue_from.project_id == issue_to.project_id || Setting.cross_project_issue_relations?
50 #detect circular dependencies depending wether the relation should be reversed 72 #detect circular dependencies depending wether the relation should be reversed
51 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] 73 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
52 errors.add_to_base :circular_dependency if issue_from.all_dependent_issues.include? issue_to 74 errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
53 else 75 else
54 errors.add_to_base :circular_dependency if issue_to.all_dependent_issues.include? issue_from 76 errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
55 end 77 end
56 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) 78 errors.add :base, :cant_link_an_issue_with_a_descendant if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
57 end 79 end
58 end 80 end
59 81
60 def other_issue(issue) 82 def other_issue(issue)
61 (self.issue_from_id == issue.id) ? issue_to : issue_from 83 (self.issue_from_id == issue.id) ? issue_to : issue_from
62 end 84 end
63 85
64 # Returns the relation type for +issue+ 86 # Returns the relation type for +issue+
65 def relation_type_for(issue) 87 def relation_type_for(issue)
66 if TYPES[relation_type] 88 if TYPES[relation_type]
67 if self.issue_from_id == issue.id 89 if self.issue_from_id == issue.id
68 relation_type 90 relation_type
69 else 91 else
70 TYPES[relation_type][:sym] 92 TYPES[relation_type][:sym]
71 end 93 end
72 end 94 end
73 end 95 end
74 96
75 def label_for(issue) 97 def label_for(issue)
76 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow 98 TYPES[relation_type] ? TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] : :unknow
77 end 99 end
78 100
79 def before_save 101 def handle_issue_order
80 reverse_if_needed 102 reverse_if_needed
81 103
82 if TYPE_PRECEDES == relation_type 104 if TYPE_PRECEDES == relation_type
83 self.delay ||= 0 105 self.delay ||= 0
84 else 106 else
85 self.delay = nil 107 self.delay = nil
86 end 108 end
87 set_issue_to_dates 109 set_issue_to_dates
88 end 110 end
89 111
90 def set_issue_to_dates 112 def set_issue_to_dates
91 soonest_start = self.successor_soonest_start 113 soonest_start = self.successor_soonest_start
92 if soonest_start && issue_to 114 if soonest_start && issue_to
93 issue_to.reschedule_after(soonest_start) 115 issue_to.reschedule_after(soonest_start)
94 end 116 end
95 end 117 end
96 118
97 def successor_soonest_start 119 def successor_soonest_start
98 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date) 120 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from && (issue_from.start_date || issue_from.due_date)
99 (issue_from.due_date || issue_from.start_date) + 1 + delay 121 (issue_from.due_date || issue_from.start_date) + 1 + delay
100 end 122 end
101 end 123 end
102 124
103 def <=>(relation) 125 def <=>(relation)
104 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order] 126 TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
105 end 127 end
106 128
107 private 129 private
108 130
109 # Reverses the relation if needed so that it gets stored in the proper way 131 # Reverses the relation if needed so that it gets stored in the proper way
132 # Should not be reversed before validation so that it can be displayed back
133 # as entered on new relation form
110 def reverse_if_needed 134 def reverse_if_needed
111 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse] 135 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
112 issue_tmp = issue_to 136 issue_tmp = issue_to
113 self.issue_to = issue_from 137 self.issue_to = issue_from
114 self.issue_from = issue_tmp 138 self.issue_from = issue_tmp