mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #34517 from EByrdS/immutable-enum
Pluralized enum raises error when attempting to modify
This commit is contained in:
commit
171e32fc77
3 changed files with 19 additions and 0 deletions
|
@ -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.
|
||||
|
||||
`ActiveRecord::ConnectionAdapters::AbstractAdapter#translate_exception_class` now requires `binds` to be passed as the last argument.
|
||||
|
|
|
@ -199,6 +199,7 @@ module ActiveRecord
|
|||
klass.scope value_method_name, -> { where(attr => value) }
|
||||
end
|
||||
end
|
||||
enum_values.freeze
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -438,6 +438,20 @@ class EnumTest < ActiveRecord::TestCase
|
|||
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"
|
||||
|
|
Loading…
Reference in a new issue