case field_format
when 'string', 'text', 'list', 'date', 'bool'
# COALESCE is here to make sure that blank and NULL values are sorted equally
- "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
- " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
- " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
- " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
+ "COALESCE(#{join_alias}.value, '')"
when 'int', 'float'
# Make the database cast values into numeric
# Postgresql will raise an error if a value can not be casted!
# CustomValue validations should ensure that it doesn't occur
- "(SELECT CAST(cv_sort.value AS decimal(30,3)) FROM #{CustomValue.table_name} cv_sort" +
- " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
- " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
- " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)"
+ "CAST(#{join_alias}.value AS decimal(30,3))"
when 'user', 'version'
value_class.fields_for_order_statement(value_join_alias)
else
when 'list', 'date', 'bool', 'int'
order_statement
when 'user', 'version'
- "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" +
- " WHERE cv_sort.customized_type='#{self.class.customized_class.base_class.name}'" +
- " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" +
- " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')"
+ "COALESCE(#{join_alias}.value, '')"
else
nil
end
" AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)" +
" LEFT OUTER JOIN #{value_class.table_name} #{value_join_alias}" +
" ON CAST(#{join_alias}.value as decimal(30,0)) = #{value_join_alias}.id"
+ when 'int', 'float'
+ "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
+ " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
+ " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
+ " AND #{join_alias}.custom_field_id = #{id}" +
+ " AND #{join_alias}.value <> ''" +
+ " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
+ " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
+ " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
+ " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)"
+ when 'string', 'text', 'list', 'date', 'bool'
+ "LEFT OUTER JOIN #{CustomValue.table_name} #{join_alias}" +
+ " ON #{join_alias}.customized_type = '#{self.class.customized_class.base_class.name}'" +
+ " AND #{join_alias}.customized_id = #{self.class.customized_class.table_name}.id" +
+ " AND #{join_alias}.custom_field_id = #{id}" +
+ " AND #{join_alias}.id = (SELECT max(#{join_alias}_2.id) FROM #{CustomValue.table_name} #{join_alias}_2" +
+ " WHERE #{join_alias}_2.customized_type = #{join_alias}.customized_type" +
+ " AND #{join_alias}_2.customized_id = #{join_alias}.customized_id" +
+ " AND #{join_alias}_2.custom_field_id = #{join_alias}.custom_field_id)"
else
nil
end
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
assert c
assert c.sortable
- issues = Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where(
- q.statement
- ).order("#{c.sortable} ASC").all
+ issues = q.issues(:order => "#{c.sortable} ASC")
values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
assert !values.empty?
assert_equal values.sort, values
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' }
assert c
assert c.sortable
- issues = Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where(
- q.statement
- ).order("#{c.sortable} DESC").all
+ issues = q.issues(:order => "#{c.sortable} DESC")
values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s}
assert !values.empty?
assert_equal values.sort.reverse, values
c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' }
assert c
assert c.sortable
- issues = Issue.includes([:assigned_to, :status, :tracker, :project, :priority]).where(
- q.statement
- ).order("#{c.sortable} ASC").all
+ issues = q.issues(:order => "#{c.sortable} ASC")
values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact
assert !values.empty?
assert_equal values.sort, values