1
0
Fork 0
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:
Rafael França 2018-11-23 23:35:29 -05:00 committed by GitHub
commit 171e32fc77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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.
`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) }
end
end
enum_values.freeze
end
end

View file

@ -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"