1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Quote empty ranges like other empty enumerables

This commit is contained in:
Patrick Rebsch 2019-03-01 20:31:59 -05:00
parent a62683f3e4
commit 3e6d3e430e
3 changed files with 20 additions and 2 deletions

View file

@ -1,3 +1,7 @@
* Quote empty ranges like other empty enumerables.
*Patrick Rebsch*
* Add `insert_all`/`insert_all!`/`upsert_all` methods to `ActiveRecord::Persistence`,
allowing bulk inserts akin to the bulk updates provided by `update_all` and
bulk deletes by `delete_all`.

View file

@ -165,10 +165,11 @@ module ActiveRecord
def quote_bound_value(value, c = connection)
if value.respond_to?(:map) && !value.acts_like?(:string)
if value.respond_to?(:empty?) && value.empty?
quoted = value.map { |v| c.quote(v) }
if quoted.empty?
c.quote(nil)
else
value.map { |v| c.quote(v) }.join(",")
quoted.join(",")
end
else
c.quote(value)

View file

@ -148,6 +148,19 @@ class SanitizeTest < ActiveRecord::TestCase
assert_equal "foo in (#{quoted_nil})", bind("foo in (?)", [])
end
def test_bind_range
quoted_abc = %(#{ActiveRecord::Base.connection.quote('a')},#{ActiveRecord::Base.connection.quote('b')},#{ActiveRecord::Base.connection.quote('c')})
assert_equal "0", bind("?", 0..0)
assert_equal "1,2,3", bind("?", 1..3)
assert_equal quoted_abc, bind("?", "a"..."d")
end
def test_bind_empty_range
quoted_nil = ActiveRecord::Base.connection.quote(nil)
assert_equal quoted_nil, bind("?", 0...0)
assert_equal quoted_nil, bind("?", "a"..."a")
end
def test_bind_empty_string
quoted_empty = ActiveRecord::Base.connection.quote("")
assert_equal quoted_empty, bind("?", "")