Skip to content
This repository
Browse code

Merge pull request #6967 from frodsan/quite_as_duration

fix quoting for ActiveSupport::Duration instances
  • Loading branch information...
commit 6c9c50c30fe2e9a0e39216801f96b52a2df3097d 2 parents 3257355 + b5bb353
Rafael Mendonça França authored July 05, 2012
2  activerecord/lib/active_record/connection_adapters/abstract/quoting.rb
@@ -31,7 +31,7 @@ def quote(value, column = nil)
31 31
           # BigDecimals need to be put in a non-normalized form and quoted.
32 32
         when nil        then "NULL"
33 33
         when BigDecimal then value.to_s('F')
34  
-        when Numeric    then value.to_s
  34
+        when Numeric, ActiveSupport::Duration then value.to_s
35 35
         when Date, Time then "'#{quoted_date(value)}'"
36 36
         when Symbol     then "'#{quote_string(value.to_s)}'"
37 37
         when Class      then "'#{value.to_s}'"
6  activerecord/test/cases/column_test.rb
@@ -76,6 +76,12 @@ def test_type_cast_date
76 76
         date_string = Time.now.utc.strftime("%F")
77 77
         assert_equal date_string, column.type_cast(date_string).strftime("%F")
78 78
       end
  79
+
  80
+      def test_type_cast_duration_to_integer
  81
+        column = Column.new("field", nil, "integer")
  82
+        assert_equal 1800, column.type_cast(30.minutes)
  83
+        assert_equal 7200, column.type_cast(2.hours)
  84
+      end
79 85
     end
80 86
   end
81 87
 end
8  activerecord/test/cases/quoting_test.rb
@@ -216,6 +216,14 @@ def string_to_binary(value)
216 216
       def test_string_with_crazy_column
217 217
         assert_equal "'lo\\\\l'", @quoter.quote('lo\l', FakeColumn.new(:foo))
218 218
       end
  219
+
  220
+      def test_quote_duration
  221
+        assert_equal "1800", @quoter.quote(30.minutes)
  222
+      end
  223
+
  224
+      def test_quote_duration_int_column
  225
+        assert_equal "7200", @quoter.quote(2.hours, FakeColumn.new(:integer))
  226
+      end
219 227
     end
220 228
   end
221 229
 end

0 notes on commit 6c9c50c

Please sign in to comment.
Something went wrong with that request. Please try again.