Quote booleans according the rules defined by the adapter
* SQLite schema has been updated * Postgresql schema needs to be fixed too Simplify AR::Base#toggle to store the boolean, not the quoted value * expand the tests git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2474 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
0639e1ca7c
commit
656fb866f9
|
@ -1094,7 +1094,7 @@ module ActiveRecord #:nodoc:
|
|||
|
||||
# Turns an +attribute+ that's currently true into false and vice versa. Returns self.
|
||||
def toggle(attribute)
|
||||
self[attribute] = quote(!send("#{attribute}?", column_for_attribute(attribute)))
|
||||
self[attribute] = !send("#{attribute}?")
|
||||
self
|
||||
end
|
||||
|
||||
|
|
|
@ -14,11 +14,11 @@ module ActiveRecord
|
|||
"'#{quote_string(value)}'" # ' (for ruby-mode)
|
||||
end
|
||||
when NilClass then "NULL"
|
||||
when TrueClass then (column && column.type == :boolean ? quoted_true : "1")
|
||||
when FalseClass then (column && column.type == :boolean ? quoted_false : "0")
|
||||
when TrueClass then (column && column.type == :integer ? '1' : quoted_true)
|
||||
when FalseClass then (column && column.type == :integer ? '0' : quoted_false)
|
||||
when Float, Fixnum, Bignum then value.to_s
|
||||
when Date then "'#{value.to_s}'"
|
||||
when Time, DateTime then "'#{value.strftime("%Y-%m-%d %H:%M:%S")}'"
|
||||
when Time, DateTime then "'#{quoted_date(value)}'"
|
||||
else "'#{quote_string(value.to_yaml)}'"
|
||||
end
|
||||
end
|
||||
|
@ -42,6 +42,10 @@ module ActiveRecord
|
|||
def quoted_false
|
||||
"'f'"
|
||||
end
|
||||
|
||||
def quoted_date(value)
|
||||
value.strftime("%Y-%m-%d %H:%M:%S")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -380,7 +380,7 @@ class BasicsTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_update_by_condition
|
||||
Topic.update_all "content = 'bulk updated!'", "approved = 1"
|
||||
Topic.update_all "content = 'bulk updated!'", ["approved = ?", true]
|
||||
assert_equal "Have a nice day", Topic.find(1).content
|
||||
assert_equal "bulk updated!", Topic.find(2).content
|
||||
end
|
||||
|
@ -812,6 +812,11 @@ class BasicsTest < Test::Unit::TestCase
|
|||
assert !topics(:first).approved?
|
||||
topics(:first).toggle!(:approved)
|
||||
assert topics(:first).approved?
|
||||
topic = topics(:first)
|
||||
topic.toggle(:approved)
|
||||
assert !topic.approved?
|
||||
topic.reload
|
||||
assert topic.approved?
|
||||
end
|
||||
|
||||
def test_reload
|
||||
|
|
|
@ -38,8 +38,8 @@ class FinderTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_deprecated_find_on_conditions
|
||||
assert Topic.find_on_conditions(1, "approved = 0")
|
||||
assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, "approved = 1") }
|
||||
assert Topic.find_on_conditions(1, ["approved = ?", false])
|
||||
assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, ["approved = ?", true]) }
|
||||
end
|
||||
|
||||
def test_condition_interpolation
|
||||
|
|
|
@ -100,8 +100,8 @@ class FinderTest < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
def test_find_on_conditions
|
||||
assert Topic.find(1, :conditions => "approved = 0")
|
||||
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => "approved = 1") }
|
||||
assert Topic.find(1, :conditions => ["approved = ?", false])
|
||||
assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
|
||||
end
|
||||
|
||||
def test_condition_interpolation
|
||||
|
|
|
@ -24,7 +24,7 @@ CREATE TABLE 'topics' (
|
|||
'bonus_time' TIME DEFAULT NULL,
|
||||
'last_read' DATE DEFAULT NULL,
|
||||
'content' TEXT,
|
||||
'approved' INTEGER DEFAULT 1,
|
||||
'approved' boolean DEFAULT 'f',
|
||||
'replies_count' INTEGER DEFAULT 0,
|
||||
'parent_id' INTEGER DEFAULT NULL,
|
||||
'type' VARCHAR(255) DEFAULT NULL
|
||||
|
|
|
@ -7,7 +7,7 @@ first:
|
|||
last_read: 2004-04-15
|
||||
bonus_time: 2005-01-30t15:28:00.00+01:00
|
||||
content: Have a nice day
|
||||
approved: '0'
|
||||
approved: false
|
||||
replies_count: 0
|
||||
|
||||
second:
|
||||
|
@ -16,6 +16,6 @@ second:
|
|||
author_name: Mary
|
||||
written_on: 2003-07-15t15:28:00.00+01:00
|
||||
content: Have a nice day
|
||||
approved: '1'
|
||||
approved: true
|
||||
replies_count: 2
|
||||
parent_id: 1
|
||||
|
|
Loading…
Reference in New Issue