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

Ensure casting by boolean attribute when querying

`QueryAttribute#value_for_database` calls only `type.serialize`, and
`Boolean#serialize` is a no-op unlike other attribute types.

It caused the issue #32624. Whether or not `serialize` will invoke
`cast` is undefined in our test cases, but it actually does not work
properly unless it does so for now.

Fixes #32624.
This commit is contained in:
Ryuta Kamizono 2018-05-29 04:58:26 +09:00
parent 168598c0f1
commit 34cc301f03
3 changed files with 47 additions and 43 deletions

View file

@ -20,6 +20,10 @@ module ActiveModel
:boolean
end
def serialize(value) # :nodoc:
cast(value)
end
private
def cast_value(value)

View file

@ -14,7 +14,6 @@ require "models/computer"
require "models/project"
require "models/default"
require "models/auto_id"
require "models/boolean"
require "models/column_name"
require "models/subscriber"
require "models/comment"
@ -716,48 +715,6 @@ class BasicsTest < ActiveRecord::TestCase
assert_equal expected_attributes, category.attributes
end
def test_boolean
b_nil = Boolean.create("value" => nil)
nil_id = b_nil.id
b_false = Boolean.create("value" => false)
false_id = b_false.id
b_true = Boolean.create("value" => true)
true_id = b_true.id
b_nil = Boolean.find(nil_id)
assert_nil b_nil.value
b_false = Boolean.find(false_id)
assert_not_predicate b_false, :value?
b_true = Boolean.find(true_id)
assert_predicate b_true, :value?
end
def test_boolean_without_questionmark
b_true = Boolean.create("value" => true)
true_id = b_true.id
subclass = Class.new(Boolean).find true_id
superclass = Boolean.find true_id
assert_equal superclass.read_attribute(:has_fun), subclass.read_attribute(:has_fun)
end
def test_boolean_cast_from_string
b_blank = Boolean.create("value" => "")
blank_id = b_blank.id
b_false = Boolean.create("value" => "0")
false_id = b_false.id
b_true = Boolean.create("value" => "1")
true_id = b_true.id
b_blank = Boolean.find(blank_id)
assert_nil b_blank.value
b_false = Boolean.find(false_id)
assert_not_predicate b_false, :value?
b_true = Boolean.find(true_id)
assert_predicate b_true, :value?
end
def test_new_record_returns_boolean
assert_equal false, Topic.new.persisted?
assert_equal true, Topic.find(1).persisted?

View file

@ -0,0 +1,43 @@
# frozen_string_literal: true
require "cases/helper"
require "models/boolean"
class BooleanTest < ActiveRecord::TestCase
def test_boolean
b_nil = Boolean.create!(value: nil)
b_false = Boolean.create!(value: false)
b_true = Boolean.create!(value: true)
assert_nil Boolean.find(b_nil.id).value
assert_not_predicate Boolean.find(b_false.id), :value?
assert_predicate Boolean.find(b_true.id), :value?
end
def test_boolean_without_questionmark
b_true = Boolean.create!(value: true)
subclass = Class.new(Boolean).find(b_true.id)
superclass = Boolean.find(b_true.id)
assert_equal superclass.read_attribute(:has_fun), subclass.read_attribute(:has_fun)
end
def test_boolean_cast_from_string
b_blank = Boolean.create!(value: "")
b_false = Boolean.create!(value: "0")
b_true = Boolean.create!(value: "1")
assert_nil Boolean.find(b_blank.id).value
assert_not_predicate Boolean.find(b_false.id), :value?
assert_predicate Boolean.find(b_true.id), :value?
end
def test_find_by_boolean_string
b_false = Boolean.create!(value: "false")
b_true = Boolean.create!(value: "true")
assert_equal b_false, Boolean.find_by(value: "false")
assert_equal b_true, Boolean.find_by(value: "true")
end
end