##// END OF EJS Templates
Use .klass instead of .class_name.constantize....
Jean-Philippe Lang -
r13557:18abb3fd8815
parent child
Show More
@@ -1,435 +1,435
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
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.to_a
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, :project => (issue.project_id != child.project_id)), :class => 'subject', :style => 'width: 50%') +
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 values = issue.visible_custom_field_values
164 164 return if values.empty?
165 165 ordered_values = []
166 166 half = (values.size / 2.0).ceil
167 167 half.times do |i|
168 168 ordered_values << values[i]
169 169 ordered_values << values[i + half]
170 170 end
171 171 s = "<tr>\n"
172 172 n = 0
173 173 ordered_values.compact.each do |value|
174 174 css = "cf_#{value.custom_field.id}"
175 175 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
176 176 s << "\t<th class=\"#{css}\">#{ h(value.custom_field.name) }:</th><td class=\"#{css}\">#{ h(show_value(value)) }</td>\n"
177 177 n += 1
178 178 end
179 179 s << "</tr>\n"
180 180 s.html_safe
181 181 end
182 182
183 183 # Returns the number of descendants for an array of issues
184 184 def issues_descendant_count(issues)
185 185 ids = issues.reject(&:leaf?).map {|issue| issue.descendants.ids}.flatten.uniq
186 186 ids -= issues.map(&:id)
187 187 ids.size
188 188 end
189 189
190 190 def issues_destroy_confirmation_message(issues)
191 191 issues = [issues] unless issues.is_a?(Array)
192 192 message = l(:text_issues_destroy_confirmation)
193 193
194 194 descendant_count = issues_descendant_count(issues)
195 195 if descendant_count > 0
196 196 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
197 197 end
198 198 message
199 199 end
200 200
201 201 def sidebar_queries
202 202 unless @sidebar_queries
203 203 @sidebar_queries = IssueQuery.visible.
204 204 order("#{Query.table_name}.name ASC").
205 205 # Project specific queries and global queries
206 206 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
207 207 to_a
208 208 end
209 209 @sidebar_queries
210 210 end
211 211
212 212 def query_links(title, queries)
213 213 return '' if queries.empty?
214 214 # links to #index on issues/show
215 215 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
216 216
217 217 content_tag('h3', title) + "\n" +
218 218 content_tag('ul',
219 219 queries.collect {|query|
220 220 css = 'query'
221 221 css << ' selected' if query == @query
222 222 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
223 223 }.join("\n").html_safe,
224 224 :class => 'queries'
225 225 ) + "\n"
226 226 end
227 227
228 228 def render_sidebar_queries
229 229 out = ''.html_safe
230 230 out << query_links(l(:label_my_queries), sidebar_queries.select(&:is_private?))
231 231 out << query_links(l(:label_query_plural), sidebar_queries.reject(&:is_private?))
232 232 out
233 233 end
234 234
235 235 def email_issue_attributes(issue, user)
236 236 items = []
237 237 %w(author status priority assigned_to category fixed_version).each do |attribute|
238 238 unless issue.disabled_core_fields.include?(attribute+"_id")
239 239 items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
240 240 end
241 241 end
242 242 issue.visible_custom_field_values(user).each do |value|
243 243 items << "#{value.custom_field.name}: #{show_value(value, false)}"
244 244 end
245 245 items
246 246 end
247 247
248 248 def render_email_issue_attributes(issue, user, html=false)
249 249 items = email_issue_attributes(issue, user)
250 250 if html
251 251 content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe)
252 252 else
253 253 items.map{|s| "* #{s}"}.join("\n")
254 254 end
255 255 end
256 256
257 257 # Returns the textual representation of a journal details
258 258 # as an array of strings
259 259 def details_to_strings(details, no_html=false, options={})
260 260 options[:only_path] = (options[:only_path] == false ? false : true)
261 261 strings = []
262 262 values_by_field = {}
263 263 details.each do |detail|
264 264 if detail.property == 'cf'
265 265 field = detail.custom_field
266 266 if field && field.multiple?
267 267 values_by_field[field] ||= {:added => [], :deleted => []}
268 268 if detail.old_value
269 269 values_by_field[field][:deleted] << detail.old_value
270 270 end
271 271 if detail.value
272 272 values_by_field[field][:added] << detail.value
273 273 end
274 274 next
275 275 end
276 276 end
277 277 strings << show_detail(detail, no_html, options)
278 278 end
279 279 values_by_field.each do |field, changes|
280 280 detail = JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s)
281 281 detail.instance_variable_set "@custom_field", field
282 282 if changes[:added].any?
283 283 detail.value = changes[:added]
284 284 strings << show_detail(detail, no_html, options)
285 285 elsif changes[:deleted].any?
286 286 detail.old_value = changes[:deleted]
287 287 strings << show_detail(detail, no_html, options)
288 288 end
289 289 end
290 290 strings
291 291 end
292 292
293 293 # Returns the textual representation of a single journal detail
294 294 def show_detail(detail, no_html=false, options={})
295 295 multiple = false
296 296 case detail.property
297 297 when 'attr'
298 298 field = detail.prop_key.to_s.gsub(/\_id$/, "")
299 299 label = l(("field_" + field).to_sym)
300 300 case detail.prop_key
301 301 when 'due_date', 'start_date'
302 302 value = format_date(detail.value.to_date) if detail.value
303 303 old_value = format_date(detail.old_value.to_date) if detail.old_value
304 304
305 305 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
306 306 'priority_id', 'category_id', 'fixed_version_id'
307 307 value = find_name_by_reflection(field, detail.value)
308 308 old_value = find_name_by_reflection(field, detail.old_value)
309 309
310 310 when 'estimated_hours'
311 311 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
312 312 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
313 313
314 314 when 'parent_id'
315 315 label = l(:field_parent_issue)
316 316 value = "##{detail.value}" unless detail.value.blank?
317 317 old_value = "##{detail.old_value}" unless detail.old_value.blank?
318 318
319 319 when 'is_private'
320 320 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
321 321 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
322 322 end
323 323 when 'cf'
324 324 custom_field = detail.custom_field
325 325 if custom_field
326 326 multiple = custom_field.multiple?
327 327 label = custom_field.name
328 328 value = format_value(detail.value, custom_field) if detail.value
329 329 old_value = format_value(detail.old_value, custom_field) if detail.old_value
330 330 end
331 331 when 'attachment'
332 332 label = l(:label_attachment)
333 333 when 'relation'
334 334 if detail.value && !detail.old_value
335 335 rel_issue = Issue.visible.find_by_id(detail.value)
336 336 value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
337 337 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
338 338 elsif detail.old_value && !detail.value
339 339 rel_issue = Issue.visible.find_by_id(detail.old_value)
340 340 old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
341 341 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
342 342 end
343 343 relation_type = IssueRelation::TYPES[detail.prop_key]
344 344 label = l(relation_type[:name]) if relation_type
345 345 end
346 346 call_hook(:helper_issues_show_detail_after_setting,
347 347 {:detail => detail, :label => label, :value => value, :old_value => old_value })
348 348
349 349 label ||= detail.prop_key
350 350 value ||= detail.value
351 351 old_value ||= detail.old_value
352 352
353 353 unless no_html
354 354 label = content_tag('strong', label)
355 355 old_value = content_tag("i", h(old_value)) if detail.old_value
356 356 if detail.old_value && detail.value.blank? && detail.property != 'relation'
357 357 old_value = content_tag("del", old_value)
358 358 end
359 359 if detail.property == 'attachment' && value.present? &&
360 360 atta = detail.journal.journalized.attachments.detect {|a| a.id == detail.prop_key.to_i}
361 361 # Link to the attachment if it has not been removed
362 362 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
363 363 if options[:only_path] != false && atta.is_text?
364 364 value += link_to(
365 365 image_tag('magnifier.png'),
366 366 :controller => 'attachments', :action => 'show',
367 367 :id => atta, :filename => atta.filename
368 368 )
369 369 end
370 370 else
371 371 value = content_tag("i", h(value)) if value
372 372 end
373 373 end
374 374
375 375 if detail.property == 'attr' && detail.prop_key == 'description'
376 376 s = l(:text_journal_changed_no_detail, :label => label)
377 377 unless no_html
378 378 diff_link = link_to 'diff',
379 379 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
380 380 :detail_id => detail.id, :only_path => options[:only_path]},
381 381 :title => l(:label_view_diff)
382 382 s << " (#{ diff_link })"
383 383 end
384 384 s.html_safe
385 385 elsif detail.value.present?
386 386 case detail.property
387 387 when 'attr', 'cf'
388 388 if detail.old_value.present?
389 389 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
390 390 elsif multiple
391 391 l(:text_journal_added, :label => label, :value => value).html_safe
392 392 else
393 393 l(:text_journal_set_to, :label => label, :value => value).html_safe
394 394 end
395 395 when 'attachment', 'relation'
396 396 l(:text_journal_added, :label => label, :value => value).html_safe
397 397 end
398 398 else
399 399 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
400 400 end
401 401 end
402 402
403 403 # Find the name of an associated record stored in the field attribute
404 404 def find_name_by_reflection(field, id)
405 405 unless id.present?
406 406 return nil
407 407 end
408 408 @detail_value_name_by_reflection ||= Hash.new do |hash, key|
409 409 association = Issue.reflect_on_association(key.first.to_sym)
410 410 if association
411 record = association.class_name.constantize.find_by_id(key.last)
411 record = association.klass.find_by_id(key.last)
412 412 if record
413 413 record.name.force_encoding('UTF-8')
414 414 hash[key] = record.name
415 415 end
416 416 end
417 417 hash[key] ||= nil
418 418 end
419 419 @detail_value_name_by_reflection[[field, id]]
420 420 end
421 421
422 422 # Renders issue children recursively
423 423 def render_api_issue_children(issue, api)
424 424 return if issue.leaf?
425 425 api.array :children do
426 426 issue.children.each do |child|
427 427 api.issue(:id => child.id) do
428 428 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
429 429 api.subject child.subject
430 430 render_api_issue_children(child, api)
431 431 end
432 432 end
433 433 end
434 434 end
435 435 end
General Comments 0
You need to be logged in to leave comments. Login now