aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick Rebsch <pjrebsch@gmail.com>2019-03-01 20:31:59 -0500
committerPatrick Rebsch <pjrebsch@gmail.com>2019-03-07 19:22:29 -0500
commit3e6d3e430e53c550e3c282af6f23b803e1652b15 (patch)
tree59f7b6db3bec125e632a7cb908945312473cf69d
parenta62683f3e4326b222b6ad95b8b2dfcc31026d227 (diff)
downloadrails-3e6d3e430e53c550e3c282af6f23b803e1652b15.tar.gz
rails-3e6d3e430e53c550e3c282af6f23b803e1652b15.tar.bz2
rails-3e6d3e430e53c550e3c282af6f23b803e1652b15.zip
Quote empty ranges like other empty enumerables
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/sanitization.rb5
-rw-r--r--activerecord/test/cases/sanitize_test.rb13
3 files changed, 20 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index cff6a3147b..c0ef3cbe7c 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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`.
diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb
index e6197752bc..750766714d 100644
--- a/activerecord/lib/active_record/sanitization.rb
+++ b/activerecord/lib/active_record/sanitization.rb
@@ -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)
diff --git a/activerecord/test/cases/sanitize_test.rb b/activerecord/test/cases/sanitize_test.rb
index 18b27bd6d1..6c884b4f45 100644
--- a/activerecord/test/cases/sanitize_test.rb
+++ b/activerecord/test/cases/sanitize_test.rb
@@ -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("?", "")