comparison .svn/pristine/eb/eb2be242568acd65bb920d1901a7f7826ea4e035.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
1 # Redmine - project management software
2 # Copyright (C) 2006-2014 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 # Class used to represent the relations of an issue
20 class Relations < Array
21 include Redmine::I18n
22
23 def initialize(issue, *args)
24 @issue = issue
25 super(*args)
26 end
27
28 def to_s(*args)
29 map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ')
30 end
31 end
32
33 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
34 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
35
36 TYPE_RELATES = "relates"
37 TYPE_DUPLICATES = "duplicates"
38 TYPE_DUPLICATED = "duplicated"
39 TYPE_BLOCKS = "blocks"
40 TYPE_BLOCKED = "blocked"
41 TYPE_PRECEDES = "precedes"
42 TYPE_FOLLOWS = "follows"
43 TYPE_COPIED_TO = "copied_to"
44 TYPE_COPIED_FROM = "copied_from"
45
46 TYPES = {
47 TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to,
48 :order => 1, :sym => TYPE_RELATES },
49 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by,
50 :order => 2, :sym => TYPE_DUPLICATED },
51 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates,
52 :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
53 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by,
54 :order => 4, :sym => TYPE_BLOCKED },
55 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks,
56 :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
57 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows,
58 :order => 6, :sym => TYPE_FOLLOWS },
59 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes,
60 :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES },
61 TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from,
62 :order => 8, :sym => TYPE_COPIED_FROM },
63 TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to,
64 :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO }
65 }.freeze
66
67 validates_presence_of :issue_from, :issue_to, :relation_type
68 validates_inclusion_of :relation_type, :in => TYPES.keys
69 validates_numericality_of :delay, :allow_nil => true
70 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
71 validate :validate_issue_relation
72
73 attr_protected :issue_from_id, :issue_to_id
74 before_save :handle_issue_order
75 after_create :create_journal_after_create
76 after_destroy :create_journal_after_delete
77
78 def visible?(user=User.current)
79 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
80 end
81
82 def deletable?(user=User.current)
83 visible?(user) &&
84 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
85 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
86 end
87
88 def initialize(attributes=nil, *args)
89 super
90 if new_record?
91 if relation_type.blank?
92 self.relation_type = IssueRelation::TYPE_RELATES
93 end
94 end
95 end
96
97 def validate_issue_relation
98 if issue_from && issue_to
99 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
100 unless issue_from.project_id == issue_to.project_id ||
101 Setting.cross_project_issue_relations?
102 errors.add :issue_to_id, :not_same_project
103 end
104 # detect circular dependencies depending wether the relation should be reversed
105 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
106 errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
107 else
108 errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
109 end
110 if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
111 errors.add :base, :cant_link_an_issue_with_a_descendant
112 end
113 end
114 end
115
116 def other_issue(issue)
117 (self.issue_from_id == issue.id) ? issue_to : issue_from
118 end
119
120 # Returns the relation type for +issue+
121 def relation_type_for(issue)
122 if TYPES[relation_type]
123 if self.issue_from_id == issue.id
124 relation_type
125 else
126 TYPES[relation_type][:sym]
127 end
128 end
129 end
130
131 def label_for(issue)
132 TYPES[relation_type] ?
133 TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
134 :unknow
135 end
136
137 def css_classes_for(issue)
138 "rel-#{relation_type_for(issue)}"
139 end
140
141 def handle_issue_order
142 reverse_if_needed
143
144 if TYPE_PRECEDES == relation_type
145 self.delay ||= 0
146 else
147 self.delay = nil
148 end
149 set_issue_to_dates
150 end
151
152 def set_issue_to_dates
153 soonest_start = self.successor_soonest_start
154 if soonest_start && issue_to
155 issue_to.reschedule_on!(soonest_start)
156 end
157 end
158
159 def successor_soonest_start
160 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
161 (issue_from.start_date || issue_from.due_date)
162 (issue_from.due_date || issue_from.start_date) + 1 + delay
163 end
164 end
165
166 def <=>(relation)
167 r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
168 r == 0 ? id <=> relation.id : r
169 end
170
171 private
172
173 # Reverses the relation if needed so that it gets stored in the proper way
174 # Should not be reversed before validation so that it can be displayed back
175 # as entered on new relation form
176 def reverse_if_needed
177 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
178 issue_tmp = issue_to
179 self.issue_to = issue_from
180 self.issue_from = issue_tmp
181 self.relation_type = TYPES[relation_type][:reverse]
182 end
183 end
184
185 def create_journal_after_create
186 journal = issue_from.init_journal(User.current)
187 journal.details << JournalDetail.new(:property => 'relation',
188 :prop_key => relation_type_for(issue_from),
189 :value => issue_to.id)
190 journal.save
191 journal = issue_to.init_journal(User.current)
192 journal.details << JournalDetail.new(:property => 'relation',
193 :prop_key => relation_type_for(issue_to),
194 :value => issue_from.id)
195 journal.save
196 end
197
198 def create_journal_after_delete
199 journal = issue_from.init_journal(User.current)
200 journal.details << JournalDetail.new(:property => 'relation',
201 :prop_key => relation_type_for(issue_from),
202 :old_value => issue_to.id)
203 journal.save
204 journal = issue_to.init_journal(User.current)
205 journal.details << JournalDetail.new(:property => 'relation',
206 :prop_key => relation_type_for(issue_to),
207 :old_value => issue_from.id)
208 journal.save
209 end
210 end