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

Use QUOTED_TRUE and QUOTED_FALSE instead of magic strings

Because we define `QUOTED_TRUE` as `"1"` and `QUOTED_FALSE` as `"0"`.
And add test cases to ensure this commit does not break current
behavior even if the value of `attributes_before_type_cast` is false.
This commit is contained in:
yui-knk 2016-04-05 10:04:38 +09:00
parent bd49325e3b
commit 72ecb9fc42
2 changed files with 16 additions and 6 deletions

View file

@ -939,8 +939,8 @@ module ActiveRecord
class MysqlString < Type::String # :nodoc:
def serialize(value)
case value
when true then "1"
when false then "0"
when true then QUOTED_TRUE
when false then QUOTED_FALSE
else super
end
end
@ -949,8 +949,8 @@ module ActiveRecord
def cast_value(value)
case value
when true then "1"
when false then "0"
when true then QUOTED_TRUE
when false then QUOTED_FALSE
else super
end
end

View file

@ -43,11 +43,16 @@ class Mysql2BooleanTest < ActiveRecord::Mysql2TestCase
boolean = BooleanType.create!(archived: true, published: true)
attributes = boolean.reload.attributes_before_type_cast
assert_equal 1, attributes["archived"]
assert_equal "1", attributes["published"]
boolean = BooleanType.create!(archived: false, published: false)
attributes = boolean.reload.attributes_before_type_cast
assert_equal 0, attributes["archived"]
assert_equal "0", attributes["published"]
assert_equal 1, @connection.type_cast(true)
assert_equal 0, @connection.type_cast(false)
end
test "test type casting without emulated booleans" do
@ -55,11 +60,16 @@ class Mysql2BooleanTest < ActiveRecord::Mysql2TestCase
boolean = BooleanType.create!(archived: true, published: true)
attributes = boolean.reload.attributes_before_type_cast
assert_equal 1, attributes["archived"]
assert_equal "1", attributes["published"]
boolean = BooleanType.create!(archived: false, published: false)
attributes = boolean.reload.attributes_before_type_cast
assert_equal 0, attributes["archived"]
assert_equal "0", attributes["published"]
assert_equal 1, @connection.type_cast(true)
assert_equal 0, @connection.type_cast(false)
end
test "with booleans stored as 1 and 0" do