##// END OF EJS Templates
Refactor: Remove duplicated case statements....
Eric Davis -
r3440:c07696b57896
parent child
Show More
@@ -1,209 +1,193
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 module IssuesHelper
18 module IssuesHelper
19 include ApplicationHelper
19 include ApplicationHelper
20
20
21 def render_issue_tooltip(issue)
21 def render_issue_tooltip(issue)
22 @cached_label_start_date ||= l(:field_start_date)
22 @cached_label_start_date ||= l(:field_start_date)
23 @cached_label_due_date ||= l(:field_due_date)
23 @cached_label_due_date ||= l(:field_due_date)
24 @cached_label_assigned_to ||= l(:field_assigned_to)
24 @cached_label_assigned_to ||= l(:field_assigned_to)
25 @cached_label_priority ||= l(:field_priority)
25 @cached_label_priority ||= l(:field_priority)
26
26
27 link_to_issue(issue) + "<br /><br />" +
27 link_to_issue(issue) + "<br /><br />" +
28 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
28 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
29 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
29 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
30 "<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" +
30 "<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" +
31 "<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}"
31 "<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}"
32 end
32 end
33
33
34 def render_custom_fields_rows(issue)
34 def render_custom_fields_rows(issue)
35 return if issue.custom_field_values.empty?
35 return if issue.custom_field_values.empty?
36 ordered_values = []
36 ordered_values = []
37 half = (issue.custom_field_values.size / 2.0).ceil
37 half = (issue.custom_field_values.size / 2.0).ceil
38 half.times do |i|
38 half.times do |i|
39 ordered_values << issue.custom_field_values[i]
39 ordered_values << issue.custom_field_values[i]
40 ordered_values << issue.custom_field_values[i + half]
40 ordered_values << issue.custom_field_values[i + half]
41 end
41 end
42 s = "<tr>\n"
42 s = "<tr>\n"
43 n = 0
43 n = 0
44 ordered_values.compact.each do |value|
44 ordered_values.compact.each do |value|
45 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
45 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
46 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
46 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
47 n += 1
47 n += 1
48 end
48 end
49 s << "</tr>\n"
49 s << "</tr>\n"
50 s
50 s
51 end
51 end
52
52
53 def sidebar_queries
53 def sidebar_queries
54 unless @sidebar_queries
54 unless @sidebar_queries
55 # User can see public queries and his own queries
55 # User can see public queries and his own queries
56 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
56 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
57 # Project specific queries and global queries
57 # Project specific queries and global queries
58 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
58 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
59 @sidebar_queries = Query.find(:all,
59 @sidebar_queries = Query.find(:all,
60 :select => 'id, name',
60 :select => 'id, name',
61 :order => "name ASC",
61 :order => "name ASC",
62 :conditions => visible.conditions)
62 :conditions => visible.conditions)
63 end
63 end
64 @sidebar_queries
64 @sidebar_queries
65 end
65 end
66
66
67 def show_detail(detail, no_html=false)
67 def show_detail(detail, no_html=false)
68 case detail.property
68 case detail.property
69 when 'attr'
69 when 'attr'
70 field = detail.prop_key.to_s.gsub(/\_id$/, "")
70 field = detail.prop_key.to_s.gsub(/\_id$/, "")
71 label = l(("field_" + field).to_sym)
71 label = l(("field_" + field).to_sym)
72 case detail.prop_key
72 case
73 when 'due_date', 'start_date'
73 when ['due_date', 'start_date'].include?(detail.prop_key)
74 value = format_date(detail.value.to_date) if detail.value
74 value = format_date(detail.value.to_date) if detail.value
75 old_value = format_date(detail.old_value.to_date) if detail.old_value
75 old_value = format_date(detail.old_value.to_date) if detail.old_value
76 when 'project_id'
76
77 value = find_name_by_reflection(field, detail.value)
77 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
78 old_value = find_name_by_reflection(field, detail.old_value)
79 when 'status_id'
80 value = find_name_by_reflection(field, detail.value)
81 old_value = find_name_by_reflection(field, detail.old_value)
82 when 'tracker_id'
83 value = find_name_by_reflection(field, detail.value)
84 old_value = find_name_by_reflection(field, detail.old_value)
85 when 'assigned_to_id'
86 value = find_name_by_reflection(field, detail.value)
87 old_value = find_name_by_reflection(field, detail.old_value)
88 when 'priority_id'
89 value = find_name_by_reflection(field, detail.value)
90 old_value = find_name_by_reflection(field, detail.old_value)
91 when 'category_id'
92 value = find_name_by_reflection(field, detail.value)
93 old_value = find_name_by_reflection(field, detail.old_value)
94 when 'fixed_version_id'
95 value = find_name_by_reflection(field, detail.value)
78 value = find_name_by_reflection(field, detail.value)
96 old_value = find_name_by_reflection(field, detail.old_value)
79 old_value = find_name_by_reflection(field, detail.old_value)
97 when 'estimated_hours'
80
81 when detail.prop_key == 'estimated_hours'
98 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
82 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
99 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
83 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
100 end
84 end
101 when 'cf'
85 when 'cf'
102 custom_field = CustomField.find_by_id(detail.prop_key)
86 custom_field = CustomField.find_by_id(detail.prop_key)
103 if custom_field
87 if custom_field
104 label = custom_field.name
88 label = custom_field.name
105 value = format_value(detail.value, custom_field.field_format) if detail.value
89 value = format_value(detail.value, custom_field.field_format) if detail.value
106 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
90 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
107 end
91 end
108 when 'attachment'
92 when 'attachment'
109 label = l(:label_attachment)
93 label = l(:label_attachment)
110 end
94 end
111 call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
95 call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
112
96
113 label ||= detail.prop_key
97 label ||= detail.prop_key
114 value ||= detail.value
98 value ||= detail.value
115 old_value ||= detail.old_value
99 old_value ||= detail.old_value
116
100
117 unless no_html
101 unless no_html
118 label = content_tag('strong', label)
102 label = content_tag('strong', label)
119 old_value = content_tag("i", h(old_value)) if detail.old_value
103 old_value = content_tag("i", h(old_value)) if detail.old_value
120 old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?)
104 old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?)
121 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
105 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
122 # Link to the attachment if it has not been removed
106 # Link to the attachment if it has not been removed
123 value = link_to_attachment(a)
107 value = link_to_attachment(a)
124 else
108 else
125 value = content_tag("i", h(value)) if value
109 value = content_tag("i", h(value)) if value
126 end
110 end
127 end
111 end
128
112
129 if !detail.value.blank?
113 if !detail.value.blank?
130 case detail.property
114 case detail.property
131 when 'attr', 'cf'
115 when 'attr', 'cf'
132 if !detail.old_value.blank?
116 if !detail.old_value.blank?
133 l(:text_journal_changed, :label => label, :old => old_value, :new => value)
117 l(:text_journal_changed, :label => label, :old => old_value, :new => value)
134 else
118 else
135 l(:text_journal_set_to, :label => label, :value => value)
119 l(:text_journal_set_to, :label => label, :value => value)
136 end
120 end
137 when 'attachment'
121 when 'attachment'
138 l(:text_journal_added, :label => label, :value => value)
122 l(:text_journal_added, :label => label, :value => value)
139 end
123 end
140 else
124 else
141 l(:text_journal_deleted, :label => label, :old => old_value)
125 l(:text_journal_deleted, :label => label, :old => old_value)
142 end
126 end
143 end
127 end
144
128
145 # Find the name of an associated record stored in the field attribute
129 # Find the name of an associated record stored in the field attribute
146 def find_name_by_reflection(field, id)
130 def find_name_by_reflection(field, id)
147 association = Issue.reflect_on_association(field.to_sym)
131 association = Issue.reflect_on_association(field.to_sym)
148 if association
132 if association
149 record = association.class_name.constantize.find_by_id(id)
133 record = association.class_name.constantize.find_by_id(id)
150 return record.name if record
134 return record.name if record
151 end
135 end
152 end
136 end
153
137
154 def issues_to_csv(issues, project = nil)
138 def issues_to_csv(issues, project = nil)
155 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
139 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
156 decimal_separator = l(:general_csv_decimal_separator)
140 decimal_separator = l(:general_csv_decimal_separator)
157 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
141 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
158 # csv header fields
142 # csv header fields
159 headers = [ "#",
143 headers = [ "#",
160 l(:field_status),
144 l(:field_status),
161 l(:field_project),
145 l(:field_project),
162 l(:field_tracker),
146 l(:field_tracker),
163 l(:field_priority),
147 l(:field_priority),
164 l(:field_subject),
148 l(:field_subject),
165 l(:field_assigned_to),
149 l(:field_assigned_to),
166 l(:field_category),
150 l(:field_category),
167 l(:field_fixed_version),
151 l(:field_fixed_version),
168 l(:field_author),
152 l(:field_author),
169 l(:field_start_date),
153 l(:field_start_date),
170 l(:field_due_date),
154 l(:field_due_date),
171 l(:field_done_ratio),
155 l(:field_done_ratio),
172 l(:field_estimated_hours),
156 l(:field_estimated_hours),
173 l(:field_created_on),
157 l(:field_created_on),
174 l(:field_updated_on)
158 l(:field_updated_on)
175 ]
159 ]
176 # Export project custom fields if project is given
160 # Export project custom fields if project is given
177 # otherwise export custom fields marked as "For all projects"
161 # otherwise export custom fields marked as "For all projects"
178 custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
162 custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
179 custom_fields.each {|f| headers << f.name}
163 custom_fields.each {|f| headers << f.name}
180 # Description in the last column
164 # Description in the last column
181 headers << l(:field_description)
165 headers << l(:field_description)
182 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
166 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
183 # csv lines
167 # csv lines
184 issues.each do |issue|
168 issues.each do |issue|
185 fields = [issue.id,
169 fields = [issue.id,
186 issue.status.name,
170 issue.status.name,
187 issue.project.name,
171 issue.project.name,
188 issue.tracker.name,
172 issue.tracker.name,
189 issue.priority.name,
173 issue.priority.name,
190 issue.subject,
174 issue.subject,
191 issue.assigned_to,
175 issue.assigned_to,
192 issue.category,
176 issue.category,
193 issue.fixed_version,
177 issue.fixed_version,
194 issue.author.name,
178 issue.author.name,
195 format_date(issue.start_date),
179 format_date(issue.start_date),
196 format_date(issue.due_date),
180 format_date(issue.due_date),
197 issue.done_ratio,
181 issue.done_ratio,
198 issue.estimated_hours.to_s.gsub('.', decimal_separator),
182 issue.estimated_hours.to_s.gsub('.', decimal_separator),
199 format_time(issue.created_on),
183 format_time(issue.created_on),
200 format_time(issue.updated_on)
184 format_time(issue.updated_on)
201 ]
185 ]
202 custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
186 custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
203 fields << issue.description
187 fields << issue.description
204 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
188 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
205 end
189 end
206 end
190 end
207 export
191 export
208 end
192 end
209 end
193 end
General Comments 0
You need to be logged in to leave comments. Login now