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

Merge pull request #41516 from kamipo/serialized_value_should_be_type_casted_by_subtype

Serialized value on the enum attribute should be type casted by the subtype
This commit is contained in:
Ryuta Kamizono 2021-02-23 14:26:40 +09:00 committed by GitHub
commit 8c590afc7c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View file

@ -140,7 +140,7 @@ module ActiveRecord
end end
def serialize(value) def serialize(value)
mapping.fetch(value, value) subtype.serialize(mapping.fetch(value, value))
end end
def assert_valid_value(value) def assert_valid_value(value)

View file

@ -92,6 +92,14 @@ class EnumTest < ActiveRecord::TestCase
assert_equal books(:ddd), Book.where(last_read: "forgotten").first assert_equal books(:ddd), Book.where(last_read: "forgotten").first
end end
test "find via where should be type casted" do
book = Book.enabled.create!
assert_predicate book, :enabled?
enabled = Book.boolean_statuses[:enabled].to_s
assert_equal book, Book.where(boolean_status: enabled).last
end
test "build from scope" do test "build from scope" do
assert_predicate Book.written.build, :written? assert_predicate Book.written.build, :written?
assert_not_predicate Book.written.build, :proposed? assert_not_predicate Book.written.build, :proposed?