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

Serialized value on the enum attribute should be type casted by the subtype

`EnumType#serialize` just does `mapping.fetch(value, value)`, so it is
not always an appropriate value for the subtype. `subtype.serialize`
should also be done on the enum attribute as a decorator type.

effed76ec6/activerecord/lib/active_record/enum.rb (L146-L148)
This commit is contained in:
Ryuta Kamizono 2021-02-22 21:55:40 +09:00
parent effed76ec6
commit 349b4a9770
2 changed files with 9 additions and 1 deletions

View file

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

View file

@ -81,6 +81,14 @@ class EnumTest < ActiveRecord::TestCase
assert_equal books(:ddd), Book.where(last_read: "forgotten").first
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
assert_predicate Book.written.build, :written?
assert_not_predicate Book.written.build, :proposed?