##// END OF EJS Templates
Preload issue associations....
Jean-Philippe Lang -
r13858:e95542908a2a
parent child
Show More
@@ -1,492 +1,492
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2015 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 include Redmine::Export::PDF::IssuesPdfHelper
23 23
24 24 def issue_list(issues, &block)
25 25 ancestors = []
26 26 issues.each do |issue|
27 27 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
28 28 ancestors.pop
29 29 end
30 30 yield issue, ancestors.size
31 31 ancestors << issue unless issue.leaf?
32 32 end
33 33 end
34 34
35 35 def grouped_issue_list(issues, query, issue_count_by_group, &block)
36 36 previous_group, first = false, true
37 37 issue_list(issues) do |issue, level|
38 38 group_name = group_count = nil
39 39 if query.grouped? && ((group = query.group_by_column.value(issue)) != previous_group || first)
40 40 if group.blank? && group != false
41 41 group_name = "(#{l(:label_blank_value)})"
42 42 else
43 43 group_name = column_content(query.group_by_column, issue)
44 44 end
45 45 group_name ||= ""
46 46 group_count = issue_count_by_group[group]
47 47 end
48 48 yield issue, level, group_name, group_count
49 49 previous_group, first = group, false
50 50 end
51 51 end
52 52
53 53 # Renders a HTML/CSS tooltip
54 54 #
55 55 # To use, a trigger div is needed. This is a div with the class of "tooltip"
56 56 # that contains this method wrapped in a span with the class of "tip"
57 57 #
58 58 # <div class="tooltip"><%= link_to_issue(issue) %>
59 59 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
60 60 # </div>
61 61 #
62 62 def render_issue_tooltip(issue)
63 63 @cached_label_status ||= l(:field_status)
64 64 @cached_label_start_date ||= l(:field_start_date)
65 65 @cached_label_due_date ||= l(:field_due_date)
66 66 @cached_label_assigned_to ||= l(:field_assigned_to)
67 67 @cached_label_priority ||= l(:field_priority)
68 68 @cached_label_project ||= l(:field_project)
69 69
70 70 link_to_issue(issue) + "<br /><br />".html_safe +
71 71 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
72 72 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
73 73 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
74 74 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
75 75 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
76 76 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
77 77 end
78 78
79 79 def issue_heading(issue)
80 80 h("#{issue.tracker} ##{issue.id}")
81 81 end
82 82
83 83 def render_issue_subject_with_tree(issue)
84 84 s = ''
85 85 ancestors = issue.root? ? [] : issue.ancestors.visible.to_a
86 86 ancestors.each do |ancestor|
87 87 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
88 88 end
89 89 s << '<div>'
90 90 subject = h(issue.subject)
91 91 if issue.is_private?
92 92 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
93 93 end
94 94 s << content_tag('h3', subject)
95 95 s << '</div>' * (ancestors.size + 1)
96 96 s.html_safe
97 97 end
98 98
99 99 def render_descendants_tree(issue)
100 100 s = '<form><table class="list issues">'
101 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
101 issue_list(issue.descendants.visible.preload(:status, :priority, :tracker).sort_by(&:lft)) do |child, level|
102 102 css = "issue issue-#{child.id} hascontextmenu"
103 103 css << " idnt idnt-#{level}" if level > 0
104 104 s << content_tag('tr',
105 105 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
106 106 content_tag('td', link_to_issue(child, :project => (issue.project_id != child.project_id)), :class => 'subject', :style => 'width: 50%') +
107 107 content_tag('td', h(child.status)) +
108 108 content_tag('td', link_to_user(child.assigned_to)) +
109 109 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
110 110 :class => css)
111 111 end
112 112 s << '</table></form>'
113 113 s.html_safe
114 114 end
115 115
116 116 # Returns an array of error messages for bulk edited issues
117 117 def bulk_edit_error_messages(issues)
118 118 messages = {}
119 119 issues.each do |issue|
120 120 issue.errors.full_messages.each do |message|
121 121 messages[message] ||= []
122 122 messages[message] << issue
123 123 end
124 124 end
125 125 messages.map { |message, issues|
126 126 "#{message}: " + issues.map {|i| "##{i.id}"}.join(', ')
127 127 }
128 128 end
129 129
130 130 # Returns a link for adding a new subtask to the given issue
131 131 def link_to_new_subtask(issue)
132 132 attrs = {
133 133 :tracker_id => issue.tracker,
134 134 :parent_issue_id => issue
135 135 }
136 136 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
137 137 end
138 138
139 139 class IssueFieldsRows
140 140 include ActionView::Helpers::TagHelper
141 141
142 142 def initialize
143 143 @left = []
144 144 @right = []
145 145 end
146 146
147 147 def left(*args)
148 148 args.any? ? @left << cells(*args) : @left
149 149 end
150 150
151 151 def right(*args)
152 152 args.any? ? @right << cells(*args) : @right
153 153 end
154 154
155 155 def size
156 156 @left.size > @right.size ? @left.size : @right.size
157 157 end
158 158
159 159 def to_html
160 160 html = ''.html_safe
161 161 blank = content_tag('th', '') + content_tag('td', '')
162 162 size.times do |i|
163 163 left = @left[i] || blank
164 164 right = @right[i] || blank
165 165 html << content_tag('tr', left + right)
166 166 end
167 167 html
168 168 end
169 169
170 170 def cells(label, text, options={})
171 171 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
172 172 end
173 173 end
174 174
175 175 def issue_fields_rows
176 176 r = IssueFieldsRows.new
177 177 yield r
178 178 r.to_html
179 179 end
180 180
181 181 def render_custom_fields_rows(issue)
182 182 values = issue.visible_custom_field_values
183 183 return if values.empty?
184 184 ordered_values = []
185 185 half = (values.size / 2.0).ceil
186 186 half.times do |i|
187 187 ordered_values << values[i]
188 188 ordered_values << values[i + half]
189 189 end
190 190 s = "<tr>\n"
191 191 n = 0
192 192 ordered_values.compact.each do |value|
193 193 css = "cf_#{value.custom_field.id}"
194 194 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
195 195 s << "\t<th class=\"#{css}\">#{ custom_field_name_tag(value.custom_field) }:</th><td class=\"#{css}\">#{ h(show_value(value)) }</td>\n"
196 196 n += 1
197 197 end
198 198 s << "</tr>\n"
199 199 s.html_safe
200 200 end
201 201
202 202 # Returns the path for updating the issue form
203 203 # with project as the current project
204 204 def update_issue_form_path(project, issue)
205 205 options = {:format => 'js'}
206 206 if issue.new_record?
207 207 if project
208 208 new_project_issue_path(project, options)
209 209 else
210 210 new_issue_path(options)
211 211 end
212 212 else
213 213 edit_issue_path(issue, options)
214 214 end
215 215 end
216 216
217 217 # Returns the number of descendants for an array of issues
218 218 def issues_descendant_count(issues)
219 219 ids = issues.reject(&:leaf?).map {|issue| issue.descendants.ids}.flatten.uniq
220 220 ids -= issues.map(&:id)
221 221 ids.size
222 222 end
223 223
224 224 def issues_destroy_confirmation_message(issues)
225 225 issues = [issues] unless issues.is_a?(Array)
226 226 message = l(:text_issues_destroy_confirmation)
227 227
228 228 descendant_count = issues_descendant_count(issues)
229 229 if descendant_count > 0
230 230 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
231 231 end
232 232 message
233 233 end
234 234
235 235 # Returns an array of users that are proposed as watchers
236 236 # on the new issue form
237 237 def users_for_new_issue_watchers(issue)
238 238 users = issue.watcher_users
239 239 if issue.project.users.count <= 20
240 240 users = (users + issue.project.users.sort).uniq
241 241 end
242 242 users
243 243 end
244 244
245 245 def sidebar_queries
246 246 unless @sidebar_queries
247 247 @sidebar_queries = IssueQuery.visible.
248 248 order("#{Query.table_name}.name ASC").
249 249 # Project specific queries and global queries
250 250 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
251 251 to_a
252 252 end
253 253 @sidebar_queries
254 254 end
255 255
256 256 def query_links(title, queries)
257 257 return '' if queries.empty?
258 258 # links to #index on issues/show
259 259 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
260 260
261 261 content_tag('h3', title) + "\n" +
262 262 content_tag('ul',
263 263 queries.collect {|query|
264 264 css = 'query'
265 265 css << ' selected' if query == @query
266 266 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
267 267 }.join("\n").html_safe,
268 268 :class => 'queries'
269 269 ) + "\n"
270 270 end
271 271
272 272 def render_sidebar_queries
273 273 out = ''.html_safe
274 274 out << query_links(l(:label_my_queries), sidebar_queries.select(&:is_private?))
275 275 out << query_links(l(:label_query_plural), sidebar_queries.reject(&:is_private?))
276 276 out
277 277 end
278 278
279 279 def email_issue_attributes(issue, user)
280 280 items = []
281 281 %w(author status priority assigned_to category fixed_version).each do |attribute|
282 282 unless issue.disabled_core_fields.include?(attribute+"_id")
283 283 items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
284 284 end
285 285 end
286 286 issue.visible_custom_field_values(user).each do |value|
287 287 items << "#{value.custom_field.name}: #{show_value(value, false)}"
288 288 end
289 289 items
290 290 end
291 291
292 292 def render_email_issue_attributes(issue, user, html=false)
293 293 items = email_issue_attributes(issue, user)
294 294 if html
295 295 content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe)
296 296 else
297 297 items.map{|s| "* #{s}"}.join("\n")
298 298 end
299 299 end
300 300
301 301 # Returns the textual representation of a journal details
302 302 # as an array of strings
303 303 def details_to_strings(details, no_html=false, options={})
304 304 options[:only_path] = (options[:only_path] == false ? false : true)
305 305 strings = []
306 306 values_by_field = {}
307 307 details.each do |detail|
308 308 if detail.property == 'cf'
309 309 field = detail.custom_field
310 310 if field && field.multiple?
311 311 values_by_field[field] ||= {:added => [], :deleted => []}
312 312 if detail.old_value
313 313 values_by_field[field][:deleted] << detail.old_value
314 314 end
315 315 if detail.value
316 316 values_by_field[field][:added] << detail.value
317 317 end
318 318 next
319 319 end
320 320 end
321 321 strings << show_detail(detail, no_html, options)
322 322 end
323 323 if values_by_field.present?
324 324 multiple_values_detail = Struct.new(:property, :prop_key, :custom_field, :old_value, :value)
325 325 values_by_field.each do |field, changes|
326 326 if changes[:added].any?
327 327 detail = multiple_values_detail.new('cf', field.id.to_s, field)
328 328 detail.value = changes[:added]
329 329 strings << show_detail(detail, no_html, options)
330 330 end
331 331 if changes[:deleted].any?
332 332 detail = multiple_values_detail.new('cf', field.id.to_s, field)
333 333 detail.old_value = changes[:deleted]
334 334 strings << show_detail(detail, no_html, options)
335 335 end
336 336 end
337 337 end
338 338 strings
339 339 end
340 340
341 341 # Returns the textual representation of a single journal detail
342 342 def show_detail(detail, no_html=false, options={})
343 343 multiple = false
344 344 show_diff = false
345 345
346 346 case detail.property
347 347 when 'attr'
348 348 field = detail.prop_key.to_s.gsub(/\_id$/, "")
349 349 label = l(("field_" + field).to_sym)
350 350 case detail.prop_key
351 351 when 'due_date', 'start_date'
352 352 value = format_date(detail.value.to_date) if detail.value
353 353 old_value = format_date(detail.old_value.to_date) if detail.old_value
354 354
355 355 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
356 356 'priority_id', 'category_id', 'fixed_version_id'
357 357 value = find_name_by_reflection(field, detail.value)
358 358 old_value = find_name_by_reflection(field, detail.old_value)
359 359
360 360 when 'estimated_hours'
361 361 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
362 362 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
363 363
364 364 when 'parent_id'
365 365 label = l(:field_parent_issue)
366 366 value = "##{detail.value}" unless detail.value.blank?
367 367 old_value = "##{detail.old_value}" unless detail.old_value.blank?
368 368
369 369 when 'is_private'
370 370 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
371 371 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
372 372
373 373 when 'description'
374 374 show_diff = true
375 375 end
376 376 when 'cf'
377 377 custom_field = detail.custom_field
378 378 if custom_field
379 379 label = custom_field.name
380 380 if custom_field.format.class.change_as_diff
381 381 show_diff = true
382 382 else
383 383 multiple = custom_field.multiple?
384 384 value = format_value(detail.value, custom_field) if detail.value
385 385 old_value = format_value(detail.old_value, custom_field) if detail.old_value
386 386 end
387 387 end
388 388 when 'attachment'
389 389 label = l(:label_attachment)
390 390 when 'relation'
391 391 if detail.value && !detail.old_value
392 392 rel_issue = Issue.visible.find_by_id(detail.value)
393 393 value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
394 394 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
395 395 elsif detail.old_value && !detail.value
396 396 rel_issue = Issue.visible.find_by_id(detail.old_value)
397 397 old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
398 398 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
399 399 end
400 400 relation_type = IssueRelation::TYPES[detail.prop_key]
401 401 label = l(relation_type[:name]) if relation_type
402 402 end
403 403 call_hook(:helper_issues_show_detail_after_setting,
404 404 {:detail => detail, :label => label, :value => value, :old_value => old_value })
405 405
406 406 label ||= detail.prop_key
407 407 value ||= detail.value
408 408 old_value ||= detail.old_value
409 409
410 410 unless no_html
411 411 label = content_tag('strong', label)
412 412 old_value = content_tag("i", h(old_value)) if detail.old_value
413 413 if detail.old_value && detail.value.blank? && detail.property != 'relation'
414 414 old_value = content_tag("del", old_value)
415 415 end
416 416 if detail.property == 'attachment' && value.present? &&
417 417 atta = detail.journal.journalized.attachments.detect {|a| a.id == detail.prop_key.to_i}
418 418 # Link to the attachment if it has not been removed
419 419 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
420 420 if options[:only_path] != false && atta.is_text?
421 421 value += link_to(
422 422 image_tag('magnifier.png'),
423 423 :controller => 'attachments', :action => 'show',
424 424 :id => atta, :filename => atta.filename
425 425 )
426 426 end
427 427 else
428 428 value = content_tag("i", h(value)) if value
429 429 end
430 430 end
431 431
432 432 if show_diff
433 433 s = l(:text_journal_changed_no_detail, :label => label)
434 434 unless no_html
435 435 diff_link = link_to 'diff',
436 436 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
437 437 :detail_id => detail.id, :only_path => options[:only_path]},
438 438 :title => l(:label_view_diff)
439 439 s << " (#{ diff_link })"
440 440 end
441 441 s.html_safe
442 442 elsif detail.value.present?
443 443 case detail.property
444 444 when 'attr', 'cf'
445 445 if detail.old_value.present?
446 446 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
447 447 elsif multiple
448 448 l(:text_journal_added, :label => label, :value => value).html_safe
449 449 else
450 450 l(:text_journal_set_to, :label => label, :value => value).html_safe
451 451 end
452 452 when 'attachment', 'relation'
453 453 l(:text_journal_added, :label => label, :value => value).html_safe
454 454 end
455 455 else
456 456 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
457 457 end
458 458 end
459 459
460 460 # Find the name of an associated record stored in the field attribute
461 461 def find_name_by_reflection(field, id)
462 462 unless id.present?
463 463 return nil
464 464 end
465 465 @detail_value_name_by_reflection ||= Hash.new do |hash, key|
466 466 association = Issue.reflect_on_association(key.first.to_sym)
467 467 name = nil
468 468 if association
469 469 record = association.klass.find_by_id(key.last)
470 470 if record
471 471 name = record.name.force_encoding('UTF-8')
472 472 end
473 473 end
474 474 hash[key] = name
475 475 end
476 476 @detail_value_name_by_reflection[[field, id]]
477 477 end
478 478
479 479 # Renders issue children recursively
480 480 def render_api_issue_children(issue, api)
481 481 return if issue.leaf?
482 482 api.array :children do
483 483 issue.children.each do |child|
484 484 api.issue(:id => child.id) do
485 485 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
486 486 api.subject child.subject
487 487 render_api_issue_children(child, api)
488 488 end
489 489 end
490 490 end
491 491 end
492 492 end
General Comments 0
You need to be logged in to leave comments. Login now