From 4a4a71349a45bdc8a55071e535bc0a8b9c02a5ee Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Sat, 9 Jul 2011 21:34:35 +0000 Subject: [PATCH] Fixes "less than", "greater than" filters on custom fields with postgres (#6180). git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6216 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/models/query.rb | 12 ++++++++++-- test/unit/query_test.rb | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/models/query.rb b/app/models/query.rb index 2b0776cbe..165bbac84 100644 --- a/app/models/query.rb +++ b/app/models/query.rb @@ -616,9 +616,17 @@ class Query < ActiveRecord::Base sql = "#{db_table}.#{db_field} IS NOT NULL" sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter when ">=" - sql = "#{db_table}.#{db_field} >= #{value.first.to_i}" + if is_custom_filter + sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_i}" + else + sql = "#{db_table}.#{db_field} >= #{value.first.to_i}" + end when "<=" - sql = "#{db_table}.#{db_field} <= #{value.first.to_i}" + if is_custom_filter + sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_i}" + else + sql = "#{db_table}.#{db_field} <= #{value.first.to_i}" + end when "o" sql = "#{IssueStatus.table_name}.is_closed=#{connection.quoted_false}" if field == "status_id" when "c" diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 646e4099f..55a67f26a 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -107,6 +107,29 @@ class QueryTest < ActiveSupport::TestCase assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40") find_issues_with_query(query) end + + def test_operator_greater_than_on_custom_field + f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) + query = Query.new(:project => Project.find(1), :name => '_') + query.add_filter("cf_#{f.id}", '>=', ['40']) + assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) >= 40") + find_issues_with_query(query) + end + + def test_operator_lesser_than + query = Query.new(:project => Project.find(1), :name => '_') + query.add_filter('done_ratio', '<=', ['30']) + assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30") + find_issues_with_query(query) + end + + def test_operator_lesser_than_on_custom_field + f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) + query = Query.new(:project => Project.find(1), :name => '_') + query.add_filter("cf_#{f.id}", '<=', ['30']) + assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30") + find_issues_with_query(query) + end def test_operator_in_more_than Issue.find(7).update_attribute(:due_date, (Date.today + 15)) -- 2.39.5