未验证 提交 6ffc8131 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #39902 from tgxworld/fix_enum_respects_attribute_override

Fix enum losing attribute type cast.
......@@ -182,7 +182,13 @@ def enum(definitions)
detect_enum_conflict!(name, "#{name}=")
attr = attribute_alias?(name) ? attribute_alias(name) : name
type, options = attributes_to_define_after_schema_loads[attr]
attribute(attr, **default) do |subtype|
if type && !type.is_a?(Proc)
subtype = _lookup_cast_type(attr, type, options)
end
EnumType.new(attr, enum_values, subtype)
end
......
......@@ -4,14 +4,24 @@
require "support/schema_dumping_helper"
class Mysql2EnumTest < ActiveRecord::Mysql2TestCase
self.use_transactional_tests = false
include SchemaDumpingHelper
class EnumTest < ActiveRecord::Base
attribute :state, :integer
enum state: {
start: 0,
middle: 1,
finish: 2
}
end
def setup
EnumTest.connection.create_table :enum_tests, id: false, force: true do |t|
t.column :enum_column, "enum('text','blob','tiny','medium','long','unsigned','bigint')"
t.column :state, "TINYINT(1)"
end
end
......@@ -29,4 +39,9 @@ def test_schema_dumping
schema = dump_table_schema "enum_tests"
assert_match %r{t\.column "enum_column", "enum\('text','blob','tiny','medium','long','unsigned','bigint'\)"$}, schema
end
def test_enum_with_attribute
enum_test = EnumTest.create!(state: :middle)
assert_equal "middle", enum_test.state
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册