diff --git a/app/helpers/queries_helper.rb b/app/helpers/queries_helper.rb index 518226c..ecfac55 100644 --- a/app/helpers/queries_helper.rb +++ b/app/helpers/queries_helper.rb @@ -27,44 +27,38 @@ module QueriesHelper content_tag('th', column.caption) end - def column_value(column, issue) - if column.is_a?(QueryCustomFieldColumn) - cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id} - show_value(cv) - else - value = issue.send(column.name) - end - end - def column_content(column, issue) - if column.is_a?(QueryCustomFieldColumn) - cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id} - show_value(cv) - else - value = issue.send(column.name) - if value.is_a?(Date) - format_date(value) - elsif value.is_a?(Time) - format_time(value) + value = column.value(issue) + + case value.class.name + when 'String' + if column.name == :subject + link_to(h(value), :controller => 'issues', :action => 'show', :id => issue) + else + h(value) + end + when 'Time' + format_time(value) + when 'Date' + format_date(value) + when 'Fixnum', 'Float' + if column.name == :done_ratio + progress_bar(value, :width => '80px') else - case column.name - when :subject - h((!@project.nil? && @project != issue.project) ? "#{issue.project.name} - " : '') + - link_to(h(value), :controller => 'issues', :action => 'show', :id => issue) - when :project - link_to(h(value), :controller => 'projects', :action => 'show', :id => value) - when :assigned_to - link_to_user value - when :author - link_to_user value - when :done_ratio - progress_bar(value, :width => '80px') - when :fixed_version - link_to(h(value), { :controller => 'versions', :action => 'show', :id => issue.fixed_version_id }) - else - h(value) - end + value.to_s end + when 'User' + link_to_user value + when 'Project' + link_to(h(value), :controller => 'projects', :action => 'show', :id => value) + when 'Version' + link_to(h(value), :controller => 'versions', :action => 'show', :id => value) + when 'TrueClass' + l(:general_text_Yes) + when 'FalseClass' + l(:general_text_No) + else + h(value) end end end diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 5160fcf..c7031af 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -67,6 +67,25 @@ class CustomField < ActiveRecord::Base end end + def cast_value(value) + casted = nil + unless value.blank? + case field_format + when 'string', 'text', 'list' + casted = value + when 'date' + casted = begin; value.to_date; rescue; nil end + when 'bool' + casted = (value == '1' ? true : false) + when 'int' + casted = value.to_i + when 'float' + casted = value.to_f + end + end + casted + end + # Returns a ORDER BY clause that can used to sort customized # objects by their value of the custom field. # Returns false, if the custom field can not be used for sorting. diff --git a/app/models/query.rb b/app/models/query.rb index c691736..f7aeb0e 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -37,6 +37,10 @@ class QueryColumn def sortable? !sortable.nil? end + + def value(issue) + issue.send name + end end class QueryCustomFieldColumn < QueryColumn @@ -58,6 +62,11 @@ class QueryCustomFieldColumn < QueryColumn def custom_field @cf end + + def value(issue) + cv = issue.custom_values.detect {|v| v.custom_field_id == @cf.id} + cv && @cf.cast_value(cv.value) + end end class Query < ActiveRecord::Base @@ -407,16 +416,20 @@ class Query < ActiveRecord::Base # Returns the issue count by group or nil if query is not grouped def issue_count_by_group + r = nil if grouped? begin # Rails will raise an (unexpected) RecordNotFound if there's only a nil group value - Issue.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) + r = Issue.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) rescue ActiveRecord::RecordNotFound - {nil => issue_count} + r = {nil => issue_count} + end + c = group_by_column + if c.is_a?(QueryCustomFieldColumn) + r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} end - else - nil end + r rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end diff --git a/app/views/issues/_list.rhtml b/app/views/issues/_list.rhtml index f51a708..a7a7b06 100644 --- a/app/views/issues/_list.rhtml +++ b/app/views/issues/_list.rhtml @@ -13,12 +13,12 @@ <% previous_group = false %>
<% issues.each do |issue| -%> - <% if @query.grouped? && (group = column_value(@query.group_by_column, issue) || '') != previous_group %> + <% if @query.grouped? && (group = @query.group_by_column.value(issue)) != previous_group %> <% reset_cycle %>