From 43e3c43cbdcf948e6a06dd37dc5b7e44d5ee5838 Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Mon, 8 Mar 2010 16:47:52 +0000 Subject: [PATCH] Refactor: Extract similar logic in IssuesHelper#show_detail to a new method. git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3553 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/helpers/issues_helper.rb | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/app/helpers/issues_helper.rb b/app/helpers/issues_helper.rb index 0f28cc06..e90225f8 100644 --- a/app/helpers/issues_helper.rb +++ b/app/helpers/issues_helper.rb @@ -67,32 +67,33 @@ module IssuesHelper def show_detail(detail, no_html=false) case detail.property when 'attr' - label = l(("field_" + detail.prop_key.to_s.gsub(/\_id$/, "")).to_sym) + field = detail.prop_key.to_s.gsub(/\_id$/, "") + label = l(("field_" + field).to_sym) case detail.prop_key when 'due_date', 'start_date' value = format_date(detail.value.to_date) if detail.value old_value = format_date(detail.old_value.to_date) if detail.old_value when 'project_id' - p = Project.find_by_id(detail.value) and value = p.name if detail.value - p = Project.find_by_id(detail.old_value) and old_value = p.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'status_id' - s = IssueStatus.find_by_id(detail.value) and value = s.name if detail.value - s = IssueStatus.find_by_id(detail.old_value) and old_value = s.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'tracker_id' - t = Tracker.find_by_id(detail.value) and value = t.name if detail.value - t = Tracker.find_by_id(detail.old_value) and old_value = t.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'assigned_to_id' - u = User.find_by_id(detail.value) and value = u.name if detail.value - u = User.find_by_id(detail.old_value) and old_value = u.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'priority_id' - e = IssuePriority.find_by_id(detail.value) and value = e.name if detail.value - e = IssuePriority.find_by_id(detail.old_value) and old_value = e.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'category_id' - c = IssueCategory.find_by_id(detail.value) and value = c.name if detail.value - c = IssueCategory.find_by_id(detail.old_value) and old_value = c.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'fixed_version_id' - v = Version.find_by_id(detail.value) and value = v.name if detail.value - v = Version.find_by_id(detail.old_value) and old_value = v.name if detail.old_value + value = find_name_by_reflection(field, detail.value) + old_value = find_name_by_reflection(field, detail.old_value) when 'estimated_hours' value = "%0.02f" % detail.value.to_f unless detail.value.blank? old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank? @@ -140,6 +141,15 @@ module IssuesHelper l(:text_journal_deleted, :label => label, :old => old_value) end end + + # Find the name of an associated record stored in the field attribute + def find_name_by_reflection(field, id) + association = Issue.reflect_on_association(field.to_sym) + if association + record = association.class_name.constantize.find_by_id(id) + return record.name if record + end + end def issues_to_csv(issues, project = nil) ic = Iconv.new(l(:general_csv_encoding), 'UTF-8') -- 2.11.0