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

Raises error when attempting to modify enum values

This commit is contained in:
ebyrds 2018-11-23 23:55:31 +00:00
parent f0330b6202
commit 4c8c31774a
3 changed files with 19 additions and 0 deletions

View file

@ -1,3 +1,7 @@
* Values of enum are frozen, raising an error when attempting to modify them.
*Emmanuel Byrd*
* Move `ActiveRecord::StatementInvalid` SQL to error property and include binds as separate error property. * Move `ActiveRecord::StatementInvalid` SQL to error property and include binds as separate error property.
`ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception_class` now requires `binds` to be passed as the last argument. `ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception_class` now requires `binds` to be passed as the last argument.

View file

@ -199,6 +199,7 @@ module ActiveRecord
klass.scope value_method_name, -> { where(attr => value) } klass.scope value_method_name, -> { where(attr => value) }
end end
end end
enum_values.freeze
end end
end end

View file

@ -438,6 +438,20 @@ class EnumTest < ActiveRecord::TestCase
assert_equal ["drafted", "uploaded"], book2.status_change assert_equal ["drafted", "uploaded"], book2.status_change
end 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 test "declare multiple enums at a time" do
klass = Class.new(ActiveRecord::Base) do klass = Class.new(ActiveRecord::Base) do
self.table_name = "books" self.table_name = "books"