public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Fix ActiveRecord::Base.quote_bound_value for ActiveSupper::Multibyte::Chars 
values.

- Adds String#acts_like_string?
- Adds Chars#acts_like_string?

Signed-off-by: Michael Koziarski <michael@koziarski.com>
[#1029 state:committed]
Manfred (author)
Thu Sep 11 13:38:20 -0700 2008
NZKoz (committer)
Thu Sep 11 13:51:26 -0700 2008
commit  babbc1580da9e4a23921ab68d47c7c0d2e8447da
tree    5f84cc282530fc8510812c9ca2b75f32e93f348f
parent  923f4ecad202e45ef117d592053c8b86549eb9d7
...
2152
2153
2154
2155
 
2156
2157
2158
...
2152
2153
2154
 
2155
2156
2157
2158
0
@@ -2152,7 +2152,7 @@ module ActiveRecord #:nodoc:
0
         end
0
 
0
         def quote_bound_value(value) #:nodoc:
0
-          if value.respond_to?(:map) && !value.is_a?(String)
0
+          if value.respond_to?(:map) && !value.acts_like?(:string)
0
             if value.respond_to?(:empty?) && value.empty?
0
               connection.quote(nil)
0
             else
...
297
298
299
300
301
302
303
...
392
393
394
395
 
396
397
398
...
455
456
457
 
 
 
 
 
 
 
 
 
458
459
460
...
297
298
299
 
300
301
302
...
391
392
393
 
394
395
396
397
...
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
0
@@ -297,7 +297,6 @@ class FinderTest < ActiveRecord::TestCase
0
     assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
0
   end
0
 
0
-
0
   def test_condition_interpolation
0
     assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
0
     assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])
0
@@ -392,7 +391,7 @@ class FinderTest < ActiveRecord::TestCase
0
       Company.find(:first, :conditions => ["id=? AND name = ?", 2])
0
     }
0
     assert_raises(ActiveRecord::PreparedStatementInvalid) {
0
-     Company.find(:first, :conditions => ["id=?", 2, 3, 4])
0
+     Company.find(:first, :conditions => ["id=?", 2, 3, 4])
0
     }
0
   end
0
 
0
@@ -455,6 +454,15 @@ class FinderTest < ActiveRecord::TestCase
0
     assert_equal ActiveRecord::Base.connection.quote(''), bind('?', '')
0
   end
0
 
0
+  def test_bind_chars
0
+    quoted_bambi = ActiveRecord::Base.connection.quote("Bambi")
0
+    quoted_bambi_and_thumper = ActiveRecord::Base.connection.quote("Bambi\nand\nThumper")
0
+    assert_equal "name=#{quoted_bambi}", bind('name=?', "Bambi")
0
+    assert_equal "name=#{quoted_bambi_and_thumper}", bind('name=?', "Bambi\nand\nThumper")
0
+    assert_equal "name=#{quoted_bambi}", bind('name=?', "Bambi".chars)
0
+    assert_equal "name=#{quoted_bambi_and_thumper}", bind('name=?', "Bambi\nand\nThumper".chars)
0
+  end
0
+
0
   def test_bind_record
0
     o = Struct.new(:quoted_id).new(1)
0
     assert_equal '1', bind('?', o)
...
6
7
8
 
9
10
11
...
15
16
17
 
18
...
6
7
8
9
10
11
12
...
16
17
18
19
20
0
@@ -6,6 +6,7 @@ require 'active_support/core_ext/string/iterators'
0
 require 'active_support/core_ext/string/unicode'
0
 require 'active_support/core_ext/string/xchar'
0
 require 'active_support/core_ext/string/filters'
0
+require 'active_support/core_ext/string/behavior'
0
 
0
 class String #:nodoc:
0
   include ActiveSupport::CoreExtensions::String::Access
0
@@ -15,4 +16,5 @@ class String #:nodoc:
0
   include ActiveSupport::CoreExtensions::String::StartsEndsWith
0
   include ActiveSupport::CoreExtensions::String::Iterators
0
   include ActiveSupport::CoreExtensions::String::Unicode
0
+  include ActiveSupport::CoreExtensions::String::Behavior
0
 end
...
49
50
51
 
 
 
 
 
52
53
54
...
49
50
51
52
53
54
55
56
57
58
59
0
@@ -49,6 +49,11 @@ module ActiveSupport::Multibyte #:nodoc:
0
         false
0
     end
0
 
0
+    # Enable more predictable duck-typing on String-like classes. See Object#acts_like?.
0
+    def acts_like_string?
0
+      true
0
+    end
0
+
0
     # Create a new Chars instance.
0
     def initialize(str)
0
       @string = str.respond_to?(:string) ? str.string : str
...
201
202
203
 
 
 
 
 
 
204
...
201
202
203
204
205
206
207
208
209
210
0
@@ -201,3 +201,9 @@ class StringInflectionsTest < Test::Unit::TestCase
0
     end
0
   end
0
 end
0
+
0
+class StringBehaviourTest < Test::Unit::TestCase
0
+  def test_acts_like_string
0
+    assert 'Bambi'.acts_like_string?
0
+  end
0
+end
0
\ No newline at end of file
...
167
168
169
 
 
 
 
170
171
172
...
167
168
169
170
171
172
173
174
175
176
0
@@ -167,6 +167,10 @@ class CharsTest < Test::Unit::TestCase
0
     assert_equal false, 'test'.chars.respond_to?(:a_method_that_doesnt_exist)
0
   end
0
   
0
+  def test_acts_like_string
0
+    assert 'Bambi'.chars.acts_like_string?
0
+  end
0
+
0
   protected
0
 
0
   def with_kcode(kcode)

Comments