Permalink
Browse files

to_s(:db) works with ranges of strings and numerics

  • Loading branch information...
1 parent 99e2a22 commit 946ebcf4a1ab1a3cc66b148a5650bf30157e9356 @nashby committed May 13, 2012
@@ -1,2 +1,3 @@
require 'active_support/core_ext/numeric/bytes'
require 'active_support/core_ext/numeric/time'
+require 'active_support/core_ext/numeric/conversions'
@@ -0,0 +1,5 @@
+class Numeric #:nodoc:
+ def to_formatted_s(format = :default)
+ self
+ end
+end
@@ -1,6 +1,15 @@
class Range
RANGE_FORMATS = {
- :db => Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
+ :db => Proc.new do |start, stop|
+ formatted_start = start.to_formatted_s(:db)
+ formatted_stop = stop.to_formatted_s(:db)
+
+ if formatted_start.is_a?(String)
+ "BETWEEN '#{formatted_start}' AND '#{formatted_stop}'"
+ else
+ "BETWEEN #{formatted_start} AND #{formatted_stop}"
+ end
+ end
}
# Gives a human readable format of the range.
@@ -45,4 +45,8 @@ def to_datetime
::DateTime.civil(*date_values)
end
end
+
+ def to_formatted_s(format = :default) #:nodoc:
+ self
+ end
end
@@ -1,6 +1,7 @@
require 'abstract_unit'
require 'active_support/time'
require 'active_support/core_ext/range'
+require 'active_support/core_ext/numeric'
class RangeTest < ActiveSupport::TestCase
def test_to_s_from_dates
@@ -13,6 +14,16 @@ def test_to_s_from_times
assert_equal "BETWEEN '2005-12-10 15:30:00' AND '2005-12-10 17:30:00'", date_range.to_s(:db)
end
+ def test_to_s_from_numerics
+ range = 1..5
+ assert_equal "BETWEEN 1 AND 5", range.to_s(:db)
+ end
+
+ def test_to_s_from_strings
+ range = '1'..'5'
+ assert_equal "BETWEEN '1' AND '5'", range.to_s(:db)
+ end
+
def test_overlaps_last_inclusive
assert((1..5).overlaps?(5..10))
end

0 comments on commit 946ebcf

Please sign in to comment.