提交 fd635412 编写于 作者: R Rafael França 提交者: Rafael Mendonça França

Merge pull request #34517 from EByrdS/immutable-enum

Pluralized enum raises error when attempting to modify
上级 6238156b
* Values of enum are frozen, raising an error when attempting to modify them.
*Emmanuel Byrd*
* `update_columns` now correctly raises `ActiveModel::MissingAttributeError`
if the attribute does not exist.
......
......@@ -201,6 +201,7 @@ def enum(definitions)
klass.scope value_method_name, -> { where(attr => value) }
end
end
enum_values.freeze
end
end
......
......@@ -409,6 +409,20 @@ def self.name; "Book"; end
assert_equal ["drafted", "uploaded"], book2.status_change
end
test "attempting to modify enum raises error" do
e = assert_raises(RuntimeError) do
Book.statuses["bad_enum"] = 40
end
assert_match(/can't modify frozen/, e.message)
e = assert_raises(RuntimeError) do
Book.statuses.delete("published")
end
assert_match(/can't modify frozen/, e.message)
end
test "declare multiple enums at a time" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "books"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册