Skip to content

Commit

Permalink
Fix to_s(:db) for range comprising of alphabets.
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-kapoor committed Oct 16, 2017
1 parent e986cb4 commit 5e6fa51
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Expand Up @@ -2,7 +2,13 @@

module ActiveSupport::RangeWithFormat
RANGE_FORMATS = {
db: Proc.new { |start, stop| "BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'" }
db: -> (start, stop) do
case start
when String then "BETWEEN '#{start}' AND '#{stop}'"
else
"BETWEEN '#{start.to_s(:db)}' AND '#{stop.to_s(:db)}'"
end
end
}

# Convert range to a formatted string. See RANGE_FORMATS for predefined formats.
Expand Down
5 changes: 5 additions & 0 deletions activesupport/test/core_ext/range_ext_test.rb
Expand Up @@ -16,6 +16,11 @@ 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_with_alphabets
alphabet_range = ("a".."z")
assert_equal "BETWEEN 'a' AND 'z'", alphabet_range.to_s(:db)
end

def test_to_s_with_numeric
number_range = (1..100)
assert_equal "BETWEEN '1' AND '100'", number_range.to_s(:db)
Expand Down

0 comments on commit 5e6fa51

Please sign in to comment.