From a153736e2069f662286bfff0001e15f44c023883 Mon Sep 17 00:00:00 2001 From: jwollert Date: Fri, 13 May 2011 16:27:01 +0200 Subject: [PATCH] Revert "Revert "map value, but convert nils different now. So they can stay at the end of the table"" This reverts commit ba5599a558c274f29951dff94571c0ead2c3550a. --- lib/report/query_utils.rb | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/report/query_utils.rb b/lib/report/query_utils.rb index 86a7651573..a497e37714 100644 --- a/lib/report/query_utils.rb +++ b/lib/report/query_utils.rb @@ -175,10 +175,39 @@ module Report::QueryUtils "-- code specific for #{adapter_name}\n\t" << super(field) end + ## + # Converts value with a given behavior, but treats nil differently. + # Params + # - value: the value to convert + # - weight_of_nil (optional): How a nil should be treated. + # :infinit - makes a nil weight really heavy, which will make it stay + # at the very end when sorting + # :negative_infinit - opposite of :infinit, let's the nil stay at the very beginning + # any other object - nil's will be replaced by thyt object + # - block (optional) - defines how to convert values which are not nil + # if no block is given, values stay untouched + def convert_unless_nil(value, weight_of_nil = :infinit) + if value.nil? + if weight_of_nil == :infinit + 1.0/0 # Infinity, which is greater than any string or number + elsif weight_of_nil == :negative_infinit + -1.0/0 # negative Infinity, which is smaller than any string or number + else + weight_of_nil + end + else + if block_given? + yield value + else + value + end + end + end + def map_field(key, value) case key.to_s - when "singleton_value", /_id$/ then value.to_i - else value.to_s + when "singleton_value", /_id$/ then convert_unless_nil(value) {|v| v.to_i } + else convert_unless_nil(value) {|v| v.to_s } end end