##// END OF EJS Templates
add journal after creating/deleting issue relation (#1005)...
Toshi MARUYAMA -
r11655:1f9e1ca3181b
parent child
Show More
@@ -1,392 +1,403
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module IssuesHelper
21 21 include ApplicationHelper
22 22
23 23 def issue_list(issues, &block)
24 24 ancestors = []
25 25 issues.each do |issue|
26 26 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
27 27 ancestors.pop
28 28 end
29 29 yield issue, ancestors.size
30 30 ancestors << issue unless issue.leaf?
31 31 end
32 32 end
33 33
34 34 # Renders a HTML/CSS tooltip
35 35 #
36 36 # To use, a trigger div is needed. This is a div with the class of "tooltip"
37 37 # that contains this method wrapped in a span with the class of "tip"
38 38 #
39 39 # <div class="tooltip"><%= link_to_issue(issue) %>
40 40 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
41 41 # </div>
42 42 #
43 43 def render_issue_tooltip(issue)
44 44 @cached_label_status ||= l(:field_status)
45 45 @cached_label_start_date ||= l(:field_start_date)
46 46 @cached_label_due_date ||= l(:field_due_date)
47 47 @cached_label_assigned_to ||= l(:field_assigned_to)
48 48 @cached_label_priority ||= l(:field_priority)
49 49 @cached_label_project ||= l(:field_project)
50 50
51 51 link_to_issue(issue) + "<br /><br />".html_safe +
52 52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
53 53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
54 54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
55 55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
56 56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
57 57 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
58 58 end
59 59
60 60 def issue_heading(issue)
61 61 h("#{issue.tracker} ##{issue.id}")
62 62 end
63 63
64 64 def render_issue_subject_with_tree(issue)
65 65 s = ''
66 66 ancestors = issue.root? ? [] : issue.ancestors.visible.all
67 67 ancestors.each do |ancestor|
68 68 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
69 69 end
70 70 s << '<div>'
71 71 subject = h(issue.subject)
72 72 if issue.is_private?
73 73 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
74 74 end
75 75 s << content_tag('h3', subject)
76 76 s << '</div>' * (ancestors.size + 1)
77 77 s.html_safe
78 78 end
79 79
80 80 def render_descendants_tree(issue)
81 81 s = '<form><table class="list issues">'
82 82 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
83 83 css = "issue issue-#{child.id} hascontextmenu"
84 84 css << " idnt idnt-#{level}" if level > 0
85 85 s << content_tag('tr',
86 86 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
87 87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
88 88 content_tag('td', h(child.status)) +
89 89 content_tag('td', link_to_user(child.assigned_to)) +
90 90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
91 91 :class => css)
92 92 end
93 93 s << '</table></form>'
94 94 s.html_safe
95 95 end
96 96
97 97 # Returns an array of error messages for bulk edited issues
98 98 def bulk_edit_error_messages(issues)
99 99 messages = {}
100 100 issues.each do |issue|
101 101 issue.errors.full_messages.each do |message|
102 102 messages[message] ||= []
103 103 messages[message] << issue
104 104 end
105 105 end
106 106 messages.map { |message, issues|
107 107 "#{message}: " + issues.map {|i| "##{i.id}"}.join(', ')
108 108 }
109 109 end
110 110
111 111 # Returns a link for adding a new subtask to the given issue
112 112 def link_to_new_subtask(issue)
113 113 attrs = {
114 114 :tracker_id => issue.tracker,
115 115 :parent_issue_id => issue
116 116 }
117 117 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
118 118 end
119 119
120 120 class IssueFieldsRows
121 121 include ActionView::Helpers::TagHelper
122 122
123 123 def initialize
124 124 @left = []
125 125 @right = []
126 126 end
127 127
128 128 def left(*args)
129 129 args.any? ? @left << cells(*args) : @left
130 130 end
131 131
132 132 def right(*args)
133 133 args.any? ? @right << cells(*args) : @right
134 134 end
135 135
136 136 def size
137 137 @left.size > @right.size ? @left.size : @right.size
138 138 end
139 139
140 140 def to_html
141 141 html = ''.html_safe
142 142 blank = content_tag('th', '') + content_tag('td', '')
143 143 size.times do |i|
144 144 left = @left[i] || blank
145 145 right = @right[i] || blank
146 146 html << content_tag('tr', left + right)
147 147 end
148 148 html
149 149 end
150 150
151 151 def cells(label, text, options={})
152 152 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
153 153 end
154 154 end
155 155
156 156 def issue_fields_rows
157 157 r = IssueFieldsRows.new
158 158 yield r
159 159 r.to_html
160 160 end
161 161
162 162 def render_custom_fields_rows(issue)
163 163 return if issue.custom_field_values.empty?
164 164 ordered_values = []
165 165 half = (issue.custom_field_values.size / 2.0).ceil
166 166 half.times do |i|
167 167 ordered_values << issue.custom_field_values[i]
168 168 ordered_values << issue.custom_field_values[i + half]
169 169 end
170 170 s = "<tr>\n"
171 171 n = 0
172 172 ordered_values.compact.each do |value|
173 173 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
174 174 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
175 175 n += 1
176 176 end
177 177 s << "</tr>\n"
178 178 s.html_safe
179 179 end
180 180
181 181 def issues_destroy_confirmation_message(issues)
182 182 issues = [issues] unless issues.is_a?(Array)
183 183 message = l(:text_issues_destroy_confirmation)
184 184 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
185 185 if descendant_count > 0
186 186 issues.each do |issue|
187 187 next if issue.root?
188 188 issues.each do |other_issue|
189 189 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
190 190 end
191 191 end
192 192 if descendant_count > 0
193 193 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
194 194 end
195 195 end
196 196 message
197 197 end
198 198
199 199 def sidebar_queries
200 200 unless @sidebar_queries
201 201 @sidebar_queries = IssueQuery.visible.
202 202 order("#{Query.table_name}.name ASC").
203 203 # Project specific queries and global queries
204 204 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
205 205 all
206 206 end
207 207 @sidebar_queries
208 208 end
209 209
210 210 def query_links(title, queries)
211 211 return '' if queries.empty?
212 212 # links to #index on issues/show
213 213 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
214 214
215 215 content_tag('h3', title) + "\n" +
216 216 content_tag('ul',
217 217 queries.collect {|query|
218 218 css = 'query'
219 219 css << ' selected' if query == @query
220 220 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
221 221 }.join("\n").html_safe,
222 222 :class => 'queries'
223 223 ) + "\n"
224 224 end
225 225
226 226 def render_sidebar_queries
227 227 out = ''.html_safe
228 228 out << query_links(l(:label_my_queries), sidebar_queries.reject(&:is_public?))
229 229 out << query_links(l(:label_query_plural), sidebar_queries.select(&:is_public?))
230 230 out
231 231 end
232 232
233 233 # Returns the textual representation of a journal details
234 234 # as an array of strings
235 235 def details_to_strings(details, no_html=false, options={})
236 236 options[:only_path] = (options[:only_path] == false ? false : true)
237 237 strings = []
238 238 values_by_field = {}
239 239 details.each do |detail|
240 240 if detail.property == 'cf'
241 241 field_id = detail.prop_key
242 242 field = CustomField.find_by_id(field_id)
243 243 if field && field.multiple?
244 244 values_by_field[field_id] ||= {:added => [], :deleted => []}
245 245 if detail.old_value
246 246 values_by_field[field_id][:deleted] << detail.old_value
247 247 end
248 248 if detail.value
249 249 values_by_field[field_id][:added] << detail.value
250 250 end
251 251 next
252 252 end
253 253 end
254 254 strings << show_detail(detail, no_html, options)
255 255 end
256 256 values_by_field.each do |field_id, changes|
257 257 detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
258 258 if changes[:added].any?
259 259 detail.value = changes[:added]
260 260 strings << show_detail(detail, no_html, options)
261 261 elsif changes[:deleted].any?
262 262 detail.old_value = changes[:deleted]
263 263 strings << show_detail(detail, no_html, options)
264 264 end
265 265 end
266 266 strings
267 267 end
268 268
269 269 # Returns the textual representation of a single journal detail
270 270 def show_detail(detail, no_html=false, options={})
271 271 multiple = false
272 272 case detail.property
273 273 when 'attr'
274 274 field = detail.prop_key.to_s.gsub(/\_id$/, "")
275 275 label = l(("field_" + field).to_sym)
276 276 case detail.prop_key
277 277 when 'due_date', 'start_date'
278 278 value = format_date(detail.value.to_date) if detail.value
279 279 old_value = format_date(detail.old_value.to_date) if detail.old_value
280 280
281 281 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
282 282 'priority_id', 'category_id', 'fixed_version_id'
283 283 value = find_name_by_reflection(field, detail.value)
284 284 old_value = find_name_by_reflection(field, detail.old_value)
285 285
286 286 when 'estimated_hours'
287 287 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
288 288 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
289 289
290 290 when 'parent_id'
291 291 label = l(:field_parent_issue)
292 292 value = "##{detail.value}" unless detail.value.blank?
293 293 old_value = "##{detail.old_value}" unless detail.old_value.blank?
294 294
295 295 when 'is_private'
296 296 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
297 297 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
298 298 end
299 299 when 'cf'
300 300 custom_field = CustomField.find_by_id(detail.prop_key)
301 301 if custom_field
302 302 multiple = custom_field.multiple?
303 303 label = custom_field.name
304 304 value = format_value(detail.value, custom_field.field_format) if detail.value
305 305 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
306 306 end
307 307 when 'attachment'
308 308 label = l(:label_attachment)
309 when 'relation'
310 if detail.value && !detail.old_value
311 rel_issue = Issue.find_by_id(detail.value)
312 value = rel_issue.nil? ? "#{l(:label_issue)} #{detail.value}" :
313 (no_html ? rel_issue : link_to_issue(rel_issue))
314 elsif detail.old_value && !detail.value
315 rel_issue = Issue.find_by_id(detail.old_value)
316 old_value = rel_issue.nil? ? "#{l(:label_issue)} #{detail.old_value}" :
317 (no_html ? rel_issue : link_to_issue(rel_issue))
318 end
319 label = l(detail.prop_key.to_sym)
309 320 end
310 321 call_hook(:helper_issues_show_detail_after_setting,
311 322 {:detail => detail, :label => label, :value => value, :old_value => old_value })
312 323
313 324 label ||= detail.prop_key
314 325 value ||= detail.value
315 326 old_value ||= detail.old_value
316 327
317 328 unless no_html
318 329 label = content_tag('strong', label)
319 330 old_value = content_tag("i", h(old_value)) if detail.old_value
320 331 old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank?
321 332 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
322 333 # Link to the attachment if it has not been removed
323 334 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
324 335 if options[:only_path] != false && atta.is_text?
325 336 value += link_to(
326 337 image_tag('magnifier.png'),
327 338 :controller => 'attachments', :action => 'show',
328 339 :id => atta, :filename => atta.filename
329 340 )
330 341 end
331 342 else
332 343 value = content_tag("i", h(value)) if value
333 344 end
334 345 end
335 346
336 347 if detail.property == 'attr' && detail.prop_key == 'description'
337 348 s = l(:text_journal_changed_no_detail, :label => label)
338 349 unless no_html
339 350 diff_link = link_to 'diff',
340 351 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
341 352 :detail_id => detail.id, :only_path => options[:only_path]},
342 353 :title => l(:label_view_diff)
343 354 s << " (#{ diff_link })"
344 355 end
345 356 s.html_safe
346 357 elsif detail.value.present?
347 358 case detail.property
348 359 when 'attr', 'cf'
349 360 if detail.old_value.present?
350 361 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
351 362 elsif multiple
352 363 l(:text_journal_added, :label => label, :value => value).html_safe
353 364 else
354 365 l(:text_journal_set_to, :label => label, :value => value).html_safe
355 366 end
356 when 'attachment'
367 when 'attachment', 'relation'
357 368 l(:text_journal_added, :label => label, :value => value).html_safe
358 369 end
359 370 else
360 371 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
361 372 end
362 373 end
363 374
364 375 # Find the name of an associated record stored in the field attribute
365 376 def find_name_by_reflection(field, id)
366 377 unless id.present?
367 378 return nil
368 379 end
369 380 association = Issue.reflect_on_association(field.to_sym)
370 381 if association
371 382 record = association.class_name.constantize.find_by_id(id)
372 383 if record
373 384 record.name.force_encoding('UTF-8') if record.name.respond_to?(:force_encoding)
374 385 return record.name
375 386 end
376 387 end
377 388 end
378 389
379 390 # Renders issue children recursively
380 391 def render_api_issue_children(issue, api)
381 392 return if issue.leaf?
382 393 api.array :children do
383 394 issue.children.each do |child|
384 395 api.issue(:id => child.id) do
385 396 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
386 397 api.subject child.subject
387 398 render_api_issue_children(child, api)
388 399 end
389 400 end
390 401 end
391 402 end
392 403 end
@@ -1,182 +1,210
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 # Class used to represent the relations of an issue
20 20 class Relations < Array
21 21 include Redmine::I18n
22 22
23 23 def initialize(issue, *args)
24 24 @issue = issue
25 25 super(*args)
26 26 end
27 27
28 28 def to_s(*args)
29 29 map {|relation| "#{l(relation.label_for(@issue))} ##{relation.other_issue(@issue).id}"}.join(', ')
30 30 end
31 31 end
32 32
33 33 belongs_to :issue_from, :class_name => 'Issue', :foreign_key => 'issue_from_id'
34 34 belongs_to :issue_to, :class_name => 'Issue', :foreign_key => 'issue_to_id'
35 35
36 36 TYPE_RELATES = "relates"
37 37 TYPE_DUPLICATES = "duplicates"
38 38 TYPE_DUPLICATED = "duplicated"
39 39 TYPE_BLOCKS = "blocks"
40 40 TYPE_BLOCKED = "blocked"
41 41 TYPE_PRECEDES = "precedes"
42 42 TYPE_FOLLOWS = "follows"
43 43 TYPE_COPIED_TO = "copied_to"
44 44 TYPE_COPIED_FROM = "copied_from"
45 45
46 46 TYPES = {
47 47 TYPE_RELATES => { :name => :label_relates_to, :sym_name => :label_relates_to,
48 48 :order => 1, :sym => TYPE_RELATES },
49 49 TYPE_DUPLICATES => { :name => :label_duplicates, :sym_name => :label_duplicated_by,
50 50 :order => 2, :sym => TYPE_DUPLICATED },
51 51 TYPE_DUPLICATED => { :name => :label_duplicated_by, :sym_name => :label_duplicates,
52 52 :order => 3, :sym => TYPE_DUPLICATES, :reverse => TYPE_DUPLICATES },
53 53 TYPE_BLOCKS => { :name => :label_blocks, :sym_name => :label_blocked_by,
54 54 :order => 4, :sym => TYPE_BLOCKED },
55 55 TYPE_BLOCKED => { :name => :label_blocked_by, :sym_name => :label_blocks,
56 56 :order => 5, :sym => TYPE_BLOCKS, :reverse => TYPE_BLOCKS },
57 57 TYPE_PRECEDES => { :name => :label_precedes, :sym_name => :label_follows,
58 58 :order => 6, :sym => TYPE_FOLLOWS },
59 59 TYPE_FOLLOWS => { :name => :label_follows, :sym_name => :label_precedes,
60 60 :order => 7, :sym => TYPE_PRECEDES, :reverse => TYPE_PRECEDES },
61 61 TYPE_COPIED_TO => { :name => :label_copied_to, :sym_name => :label_copied_from,
62 62 :order => 8, :sym => TYPE_COPIED_FROM },
63 63 TYPE_COPIED_FROM => { :name => :label_copied_from, :sym_name => :label_copied_to,
64 64 :order => 9, :sym => TYPE_COPIED_TO, :reverse => TYPE_COPIED_TO }
65 65 }.freeze
66 66
67 67 validates_presence_of :issue_from, :issue_to, :relation_type
68 68 validates_inclusion_of :relation_type, :in => TYPES.keys
69 69 validates_numericality_of :delay, :allow_nil => true
70 70 validates_uniqueness_of :issue_to_id, :scope => :issue_from_id
71 71 validate :validate_issue_relation
72 72
73 73 attr_protected :issue_from_id, :issue_to_id
74 74 before_save :handle_issue_order
75 after_create :create_journal_after_create
76 after_destroy :create_journal_after_delete
75 77
76 78 def visible?(user=User.current)
77 79 (issue_from.nil? || issue_from.visible?(user)) && (issue_to.nil? || issue_to.visible?(user))
78 80 end
79 81
80 82 def deletable?(user=User.current)
81 83 visible?(user) &&
82 84 ((issue_from.nil? || user.allowed_to?(:manage_issue_relations, issue_from.project)) ||
83 85 (issue_to.nil? || user.allowed_to?(:manage_issue_relations, issue_to.project)))
84 86 end
85 87
86 88 def initialize(attributes=nil, *args)
87 89 super
88 90 if new_record?
89 91 if relation_type.blank?
90 92 self.relation_type = IssueRelation::TYPE_RELATES
91 93 end
92 94 end
93 95 end
94 96
95 97 def validate_issue_relation
96 98 if issue_from && issue_to
97 99 errors.add :issue_to_id, :invalid if issue_from_id == issue_to_id
98 100 unless issue_from.project_id == issue_to.project_id ||
99 101 Setting.cross_project_issue_relations?
100 102 errors.add :issue_to_id, :not_same_project
101 103 end
102 104 # detect circular dependencies depending wether the relation should be reversed
103 105 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
104 106 errors.add :base, :circular_dependency if issue_from.all_dependent_issues.include? issue_to
105 107 else
106 108 errors.add :base, :circular_dependency if issue_to.all_dependent_issues.include? issue_from
107 109 end
108 110 if issue_from.is_descendant_of?(issue_to) || issue_from.is_ancestor_of?(issue_to)
109 111 errors.add :base, :cant_link_an_issue_with_a_descendant
110 112 end
111 113 end
112 114 end
113 115
114 116 def other_issue(issue)
115 117 (self.issue_from_id == issue.id) ? issue_to : issue_from
116 118 end
117 119
118 120 # Returns the relation type for +issue+
119 121 def relation_type_for(issue)
120 122 if TYPES[relation_type]
121 123 if self.issue_from_id == issue.id
122 124 relation_type
123 125 else
124 126 TYPES[relation_type][:sym]
125 127 end
126 128 end
127 129 end
128 130
129 131 def label_for(issue)
130 132 TYPES[relation_type] ?
131 133 TYPES[relation_type][(self.issue_from_id == issue.id) ? :name : :sym_name] :
132 134 :unknow
133 135 end
134 136
135 137 def css_classes_for(issue)
136 138 "rel-#{relation_type_for(issue)}"
137 139 end
138 140
139 141 def handle_issue_order
140 142 reverse_if_needed
141 143
142 144 if TYPE_PRECEDES == relation_type
143 145 self.delay ||= 0
144 146 else
145 147 self.delay = nil
146 148 end
147 149 set_issue_to_dates
148 150 end
149 151
150 152 def set_issue_to_dates
151 153 soonest_start = self.successor_soonest_start
152 154 if soonest_start && issue_to
153 155 issue_to.reschedule_on!(soonest_start)
154 156 end
155 157 end
156 158
157 159 def successor_soonest_start
158 160 if (TYPE_PRECEDES == self.relation_type) && delay && issue_from &&
159 161 (issue_from.start_date || issue_from.due_date)
160 162 (issue_from.due_date || issue_from.start_date) + 1 + delay
161 163 end
162 164 end
163 165
164 166 def <=>(relation)
165 167 r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
166 168 r == 0 ? id <=> relation.id : r
167 169 end
168 170
169 171 private
170 172
171 173 # Reverses the relation if needed so that it gets stored in the proper way
172 174 # Should not be reversed before validation so that it can be displayed back
173 175 # as entered on new relation form
174 176 def reverse_if_needed
175 177 if TYPES.has_key?(relation_type) && TYPES[relation_type][:reverse]
176 178 issue_tmp = issue_to
177 179 self.issue_to = issue_from
178 180 self.issue_from = issue_tmp
179 181 self.relation_type = TYPES[relation_type][:reverse]
180 182 end
181 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 => label_for(issue_from).to_s,
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 => label_for(issue_to).to_s,
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 => label_for(issue_from).to_s,
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 => label_for(issue_to).to_s,
207 :old_value => issue_from.id)
208 journal.save
209 end
182 210 end
@@ -1,213 +1,245
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 require File.expand_path('../../../test_helper', __FILE__)
19 19
20 20 class IssuesHelperTest < ActionView::TestCase
21 21 include ApplicationHelper
22 22 include Redmine::I18n
23 23 include IssuesHelper
24 24 include CustomFieldsHelper
25 25 include ERB::Util
26 26
27 27 fixtures :projects, :trackers, :issue_statuses, :issues,
28 28 :enumerations, :users, :issue_categories,
29 29 :projects_trackers,
30 30 :roles,
31 31 :member_roles,
32 32 :members,
33 33 :enabled_modules,
34 34 :custom_fields,
35 35 :attachments,
36 36 :versions
37 37
38 38 def setup
39 39 super
40 40 set_language_if_valid('en')
41 41 User.current = nil
42 42 end
43 43
44 44 def test_issue_heading
45 45 assert_equal "Bug #1", issue_heading(Issue.find(1))
46 46 end
47 47
48 48 def test_issues_destroy_confirmation_message_with_one_root_issue
49 49 assert_equal l(:text_issues_destroy_confirmation),
50 50 issues_destroy_confirmation_message(Issue.find(1))
51 51 end
52 52
53 53 def test_issues_destroy_confirmation_message_with_an_arrayt_of_root_issues
54 54 assert_equal l(:text_issues_destroy_confirmation),
55 55 issues_destroy_confirmation_message(Issue.find([1, 2]))
56 56 end
57 57
58 58 def test_issues_destroy_confirmation_message_with_one_parent_issue
59 59 Issue.find(2).update_attribute :parent_issue_id, 1
60 60 assert_equal l(:text_issues_destroy_confirmation) + "\n" +
61 61 l(:text_issues_destroy_descendants_confirmation, :count => 1),
62 62 issues_destroy_confirmation_message(Issue.find(1))
63 63 end
64 64
65 65 def test_issues_destroy_confirmation_message_with_one_parent_issue_and_its_child
66 66 Issue.find(2).update_attribute :parent_issue_id, 1
67 67 assert_equal l(:text_issues_destroy_confirmation),
68 68 issues_destroy_confirmation_message(Issue.find([1, 2]))
69 69 end
70 70
71 71 test 'show_detail with no_html should show a changing attribute' do
72 72 detail = JournalDetail.new(:property => 'attr', :old_value => '40',
73 73 :value => '100', :prop_key => 'done_ratio')
74 74 assert_equal "% Done changed from 40 to 100", show_detail(detail, true)
75 75 end
76 76
77 77 test 'show_detail with no_html should show a new attribute' do
78 78 detail = JournalDetail.new(:property => 'attr', :old_value => nil,
79 79 :value => '100', :prop_key => 'done_ratio')
80 80 assert_equal "% Done set to 100", show_detail(detail, true)
81 81 end
82 82
83 83 test 'show_detail with no_html should show a deleted attribute' do
84 84 detail = JournalDetail.new(:property => 'attr', :old_value => '50',
85 85 :value => nil, :prop_key => 'done_ratio')
86 86 assert_equal "% Done deleted (50)", show_detail(detail, true)
87 87 end
88 88
89 89 test 'show_detail with html should show a changing attribute with HTML highlights' do
90 90 detail = JournalDetail.new(:property => 'attr', :old_value => '40',
91 91 :value => '100', :prop_key => 'done_ratio')
92 92 html = show_detail(detail, false)
93 93 assert_include '<strong>% Done</strong>', html
94 94 assert_include '<i>40</i>', html
95 95 assert_include '<i>100</i>', html
96 96 end
97 97
98 98 test 'show_detail with html should show a new attribute with HTML highlights' do
99 99 detail = JournalDetail.new(:property => 'attr', :old_value => nil,
100 100 :value => '100', :prop_key => 'done_ratio')
101 101 html = show_detail(detail, false)
102 102 assert_include '<strong>% Done</strong>', html
103 103 assert_include '<i>100</i>', html
104 104 end
105 105
106 106 test 'show_detail with html should show a deleted attribute with HTML highlights' do
107 107 detail = JournalDetail.new(:property => 'attr', :old_value => '50',
108 108 :value => nil, :prop_key => 'done_ratio')
109 109 html = show_detail(detail, false)
110 110 assert_include '<strong>% Done</strong>', html
111 111 assert_include '<del><i>50</i></del>', html
112 112 end
113 113
114 114 test 'show_detail with a start_date attribute should format the dates' do
115 115 detail = JournalDetail.new(
116 116 :property => 'attr',
117 117 :old_value => '2010-01-01',
118 118 :value => '2010-01-31',
119 119 :prop_key => 'start_date'
120 120 )
121 121 with_settings :date_format => '%m/%d/%Y' do
122 122 assert_match "01/31/2010", show_detail(detail, true)
123 123 assert_match "01/01/2010", show_detail(detail, true)
124 124 end
125 125 end
126 126
127 127 test 'show_detail with a due_date attribute should format the dates' do
128 128 detail = JournalDetail.new(
129 129 :property => 'attr',
130 130 :old_value => '2010-01-01',
131 131 :value => '2010-01-31',
132 132 :prop_key => 'due_date'
133 133 )
134 134 with_settings :date_format => '%m/%d/%Y' do
135 135 assert_match "01/31/2010", show_detail(detail, true)
136 136 assert_match "01/01/2010", show_detail(detail, true)
137 137 end
138 138 end
139 139
140 140 test 'show_detail should show old and new values with a project attribute' do
141 141 detail = JournalDetail.new(:property => 'attr', :prop_key => 'project_id',
142 142 :old_value => 1, :value => 2)
143 143 assert_match 'eCookbook', show_detail(detail, true)
144 144 assert_match 'OnlineStore', show_detail(detail, true)
145 145 end
146 146
147 147 test 'show_detail should show old and new values with a issue status attribute' do
148 148 detail = JournalDetail.new(:property => 'attr', :prop_key => 'status_id',
149 149 :old_value => 1, :value => 2)
150 150 assert_match 'New', show_detail(detail, true)
151 151 assert_match 'Assigned', show_detail(detail, true)
152 152 end
153 153
154 154 test 'show_detail should show old and new values with a tracker attribute' do
155 155 detail = JournalDetail.new(:property => 'attr', :prop_key => 'tracker_id',
156 156 :old_value => 1, :value => 2)
157 157 assert_match 'Bug', show_detail(detail, true)
158 158 assert_match 'Feature request', show_detail(detail, true)
159 159 end
160 160
161 161 test 'show_detail should show old and new values with a assigned to attribute' do
162 162 detail = JournalDetail.new(:property => 'attr', :prop_key => 'assigned_to_id',
163 163 :old_value => 1, :value => 2)
164 164 assert_match 'Redmine Admin', show_detail(detail, true)
165 165 assert_match 'John Smith', show_detail(detail, true)
166 166 end
167 167
168 168 test 'show_detail should show old and new values with a priority attribute' do
169 169 detail = JournalDetail.new(:property => 'attr', :prop_key => 'priority_id',
170 170 :old_value => 4, :value => 5)
171 171 assert_match 'Low', show_detail(detail, true)
172 172 assert_match 'Normal', show_detail(detail, true)
173 173 end
174 174
175 175 test 'show_detail should show old and new values with a category attribute' do
176 176 detail = JournalDetail.new(:property => 'attr', :prop_key => 'category_id',
177 177 :old_value => 1, :value => 2)
178 178 assert_match 'Printing', show_detail(detail, true)
179 179 assert_match 'Recipes', show_detail(detail, true)
180 180 end
181 181
182 182 test 'show_detail should show old and new values with a fixed version attribute' do
183 183 detail = JournalDetail.new(:property => 'attr', :prop_key => 'fixed_version_id',
184 184 :old_value => 1, :value => 2)
185 185 assert_match '0.1', show_detail(detail, true)
186 186 assert_match '1.0', show_detail(detail, true)
187 187 end
188 188
189 189 test 'show_detail should show old and new values with a estimated hours attribute' do
190 190 detail = JournalDetail.new(:property => 'attr', :prop_key => 'estimated_hours',
191 191 :old_value => '5', :value => '6.3')
192 192 assert_match '5.00', show_detail(detail, true)
193 193 assert_match '6.30', show_detail(detail, true)
194 194 end
195 195
196 196 test 'show_detail should show old and new values with a custom field' do
197 197 detail = JournalDetail.new(:property => 'cf', :prop_key => '1',
198 198 :old_value => 'MySQL', :value => 'PostgreSQL')
199 199 assert_equal 'Database changed from MySQL to PostgreSQL', show_detail(detail, true)
200 200 end
201 201
202 202 test 'show_detail should show added file' do
203 203 detail = JournalDetail.new(:property => 'attachment', :prop_key => '1',
204 204 :old_value => nil, :value => 'error281.txt')
205 205 assert_match 'error281.txt', show_detail(detail, true)
206 206 end
207 207
208 208 test 'show_detail should show removed file' do
209 209 detail = JournalDetail.new(:property => 'attachment', :prop_key => '1',
210 210 :old_value => 'error281.txt', :value => nil)
211 211 assert_match 'error281.txt', show_detail(detail, true)
212 212 end
213
214 def test_show_detail_create_relation
215 detail = JournalDetail.new(:property => 'relation',
216 :prop_key => 'label_precedes',
217 :value => 1)
218 assert_equal "Precedes Bug #1: Can't print recipes added", show_detail(detail, true)
219 assert_match %r{<strong>Precedes</strong> <i><a href="/issues/1" class=".+">Bug #1</a>: Can&#x27;t print recipes</i> added},
220 show_detail(detail, false)
221 non_existed_issue_number = 9999
222 assert_nil Issue.find_by_id(non_existed_issue_number)
223 detail = JournalDetail.new(:property => 'relation',
224 :prop_key => 'label_precedes',
225 :value => non_existed_issue_number)
226 assert_equal "Precedes Issue #{non_existed_issue_number} added", show_detail(detail, true)
227 assert_equal "<strong>Precedes</strong> <i>Issue #{non_existed_issue_number}</i> added", show_detail(detail, false)
228 end
229
230 def test_show_detail_delete_relation
231 detail = JournalDetail.new(:property => 'relation',
232 :prop_key => 'label_precedes',
233 :old_value => 1)
234 assert_equal "Precedes deleted (Bug #1: Can't print recipes)", show_detail(detail, true)
235 assert_match %r{<strong>Precedes</strong> deleted \(<del><i><a href="/issues/1" class=".+">Bug #1</a>: Can&#x27;t print recipes</i></del>\)},
236 show_detail(detail, false)
237 non_existed_issue_number = 9999
238 assert_nil Issue.find_by_id(non_existed_issue_number)
239 detail = JournalDetail.new(:property => 'relation',
240 :prop_key => 'label_precedes',
241 :old_value => non_existed_issue_number)
242 assert_equal "Precedes deleted (Issue 9999)", show_detail(detail, true)
243 assert_equal "<strong>Precedes</strong> deleted (<del><i>Issue 9999</i></del>)", show_detail(detail, false)
244 end
213 245 end
@@ -1,170 +1,215
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class IssueRelationTest < ActiveSupport::TestCase
21 21 fixtures :projects,
22 22 :users,
23 23 :roles,
24 24 :members,
25 25 :member_roles,
26 26 :issues,
27 27 :issue_statuses,
28 28 :issue_relations,
29 29 :enabled_modules,
30 30 :enumerations,
31 31 :trackers
32 32
33 33 include Redmine::I18n
34 34
35 35 def test_create
36 36 from = Issue.find(1)
37 37 to = Issue.find(2)
38 38
39 39 relation = IssueRelation.new :issue_from => from, :issue_to => to,
40 40 :relation_type => IssueRelation::TYPE_PRECEDES
41 41 assert relation.save
42 42 relation.reload
43 43 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type
44 44 assert_equal from, relation.issue_from
45 45 assert_equal to, relation.issue_to
46 46 end
47 47
48 48 def test_create_minimum
49 49 relation = IssueRelation.new :issue_from => Issue.find(1), :issue_to => Issue.find(2)
50 50 assert relation.save
51 51 assert_equal IssueRelation::TYPE_RELATES, relation.relation_type
52 52 end
53 53
54 54 def test_follows_relation_should_be_reversed
55 55 from = Issue.find(1)
56 56 to = Issue.find(2)
57 57
58 58 relation = IssueRelation.new :issue_from => from, :issue_to => to,
59 59 :relation_type => IssueRelation::TYPE_FOLLOWS
60 60 assert relation.save
61 61 relation.reload
62 62 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type
63 63 assert_equal to, relation.issue_from
64 64 assert_equal from, relation.issue_to
65 65 end
66 66
67 67 def test_follows_relation_should_not_be_reversed_if_validation_fails
68 68 from = Issue.find(1)
69 69 to = Issue.find(2)
70 70
71 71 relation = IssueRelation.new :issue_from => from, :issue_to => to,
72 72 :relation_type => IssueRelation::TYPE_FOLLOWS,
73 73 :delay => 'xx'
74 74 assert !relation.save
75 75 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type
76 76 assert_equal from, relation.issue_from
77 77 assert_equal to, relation.issue_to
78 78 end
79 79
80 80 def test_relation_type_for
81 81 from = Issue.find(1)
82 82 to = Issue.find(2)
83 83
84 84 relation = IssueRelation.new :issue_from => from, :issue_to => to,
85 85 :relation_type => IssueRelation::TYPE_PRECEDES
86 86 assert_equal IssueRelation::TYPE_PRECEDES, relation.relation_type_for(from)
87 87 assert_equal IssueRelation::TYPE_FOLLOWS, relation.relation_type_for(to)
88 88 end
89 89
90 90 def test_set_issue_to_dates_without_issue_to
91 91 r = IssueRelation.new(:issue_from => Issue.new(:start_date => Date.today),
92 92 :relation_type => IssueRelation::TYPE_PRECEDES,
93 93 :delay => 1)
94 94 assert_nil r.set_issue_to_dates
95 95 end
96 96
97 97 def test_set_issue_to_dates_without_issues
98 98 r = IssueRelation.new(:relation_type => IssueRelation::TYPE_PRECEDES, :delay => 1)
99 99 assert_nil r.set_issue_to_dates
100 100 end
101 101
102 102 def test_validates_circular_dependency
103 103 IssueRelation.delete_all
104 104 assert IssueRelation.create!(
105 105 :issue_from => Issue.find(1), :issue_to => Issue.find(2),
106 106 :relation_type => IssueRelation::TYPE_PRECEDES
107 107 )
108 108 assert IssueRelation.create!(
109 109 :issue_from => Issue.find(2), :issue_to => Issue.find(3),
110 110 :relation_type => IssueRelation::TYPE_PRECEDES
111 111 )
112 112 r = IssueRelation.new(
113 113 :issue_from => Issue.find(3), :issue_to => Issue.find(1),
114 114 :relation_type => IssueRelation::TYPE_PRECEDES
115 115 )
116 116 assert !r.save
117 117 assert_not_nil r.errors[:base]
118 118 end
119 119
120 120 def test_validates_circular_dependency_of_subtask
121 121 set_language_if_valid 'en'
122 122 issue1 = Issue.generate!
123 123 issue2 = Issue.generate!
124 124 IssueRelation.create!(
125 125 :issue_from => issue1, :issue_to => issue2,
126 126 :relation_type => IssueRelation::TYPE_PRECEDES
127 127 )
128 128 child = Issue.generate!(:parent_issue_id => issue2.id)
129 129 issue1.reload
130 130 child.reload
131 131
132 132 r = IssueRelation.new(
133 133 :issue_from => child, :issue_to => issue1,
134 134 :relation_type => IssueRelation::TYPE_PRECEDES
135 135 )
136 136 assert !r.save
137 137 assert_include 'This relation would create a circular dependency', r.errors.full_messages
138 138 end
139 139
140 140 def test_subtasks_should_allow_precedes_relation
141 141 parent = Issue.generate!
142 142 child1 = Issue.generate!(:parent_issue_id => parent.id)
143 143 child2 = Issue.generate!(:parent_issue_id => parent.id)
144 144
145 145 r = IssueRelation.new(
146 146 :issue_from => child1, :issue_to => child2,
147 147 :relation_type => IssueRelation::TYPE_PRECEDES
148 148 )
149 149 assert r.valid?
150 150 assert r.save
151 151 end
152 152
153 153 def test_validates_circular_dependency_on_reverse_relations
154 154 IssueRelation.delete_all
155 155 assert IssueRelation.create!(
156 156 :issue_from => Issue.find(1), :issue_to => Issue.find(3),
157 157 :relation_type => IssueRelation::TYPE_BLOCKS
158 158 )
159 159 assert IssueRelation.create!(
160 160 :issue_from => Issue.find(1), :issue_to => Issue.find(2),
161 161 :relation_type => IssueRelation::TYPE_BLOCKED
162 162 )
163 163 r = IssueRelation.new(
164 164 :issue_from => Issue.find(2), :issue_to => Issue.find(1),
165 165 :relation_type => IssueRelation::TYPE_BLOCKED
166 166 )
167 167 assert !r.save
168 168 assert_not_nil r.errors[:base]
169 169 end
170
171 def test_create_should_make_journal_entry
172 from = Issue.find(1)
173 to = Issue.find(2)
174 from_journals = from.journals.size
175 to_journals = to.journals.size
176 relation = IssueRelation.new(:issue_from => from, :issue_to => to,
177 :relation_type => IssueRelation::TYPE_PRECEDES)
178 assert relation.save
179 from.reload
180 to.reload
181 relation.reload
182 assert_equal from.journals.size, (from_journals + 1)
183 assert_equal to.journals.size, (to_journals + 1)
184 assert_equal 'relation', from.journals.last.details.last.property
185 assert_equal 'label_precedes', from.journals.last.details.last.prop_key
186 assert_equal '2', from.journals.last.details.last.value
187 assert_nil from.journals.last.details.last.old_value
188 assert_equal 'relation', to.journals.last.details.last.property
189 assert_equal 'label_follows', to.journals.last.details.last.prop_key
190 assert_equal '1', to.journals.last.details.last.value
191 assert_nil to.journals.last.details.last.old_value
192 end
193
194 def test_delete_should_make_journal_entry
195 relation = IssueRelation.find(1)
196 id = relation.id
197 from = relation.issue_from
198 to = relation.issue_to
199 from_journals = from.journals.size
200 to_journals = to.journals.size
201 assert relation.destroy
202 from.reload
203 to.reload
204 assert_equal from.journals.size, (from_journals + 1)
205 assert_equal to.journals.size, (to_journals + 1)
206 assert_equal 'relation', from.journals.last.details.last.property
207 assert_equal 'label_blocks', from.journals.last.details.last.prop_key
208 assert_equal '9', from.journals.last.details.last.old_value
209 assert_nil from.journals.last.details.last.value
210 assert_equal 'relation', to.journals.last.details.last.property
211 assert_equal 'label_blocked_by', to.journals.last.details.last.prop_key
212 assert_equal '10', to.journals.last.details.last.old_value
213 assert_nil to.journals.last.details.last.value
214 end
170 215 end
General Comments 0
You need to be logged in to leave comments. Login now