Skip to content

Commit

Permalink
Delegate serializable? to subtype on Enum
Browse files Browse the repository at this point in the history
Since #41516, values on Enum are not always serializable.
  • Loading branch information
kamipo committed Mar 9, 2021
1 parent 603eb24 commit 7f3ea21
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 2 additions & 4 deletions activerecord/lib/active_record/enum.rb
Expand Up @@ -115,7 +115,7 @@ def inherited(base) # :nodoc:
end

class EnumType < Type::Value # :nodoc:
delegate :type, to: :subtype
delegate :type, :serializable?, to: :subtype

def initialize(name, mapping, subtype)
@name = name
Expand All @@ -128,10 +128,8 @@ def cast(value)
value.to_s
elsif mapping.has_value?(value)
mapping.key(value)
elsif value.blank?
nil
else
assert_valid_value(value)
value.presence
end
end

Expand Down
4 changes: 4 additions & 0 deletions activerecord/test/cases/enum_test.rb
Expand Up @@ -94,6 +94,10 @@ class EnumTest < ActiveRecord::TestCase
assert_nil Book.where(status: "prohibited").first
end

test "find via where with large number" do
assert_equal @book, Book.where(status: [2, 9223372036854775808]).first
end

test "find via where should be type casted" do
book = Book.enabled.create!
assert_predicate book, :enabled?
Expand Down

0 comments on commit 7f3ea21

Please sign in to comment.