aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorebyrds <emmanuel_pajaro@hotmail.com>2018-11-23 23:55:31 +0000
committerebyrds <emmanuel_pajaro@hotmail.com>2018-11-23 23:55:31 +0000
commit4c8c31774a7c44080099f4a9c7e21d76fa097e09 (patch)
tree033ed02ede94d51f56b225f2dd9b8060c1c3a287 /activerecord
parentf0330b620282b6c999679674ec54f7b2b9f83324 (diff)
downloadrails-4c8c31774a7c44080099f4a9c7e21d76fa097e09.tar.gz
rails-4c8c31774a7c44080099f4a9c7e21d76fa097e09.tar.bz2
rails-4c8c31774a7c44080099f4a9c7e21d76fa097e09.zip
Raises error when attempting to modify enum values
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/enum.rb1
-rw-r--r--activerecord/test/cases/enum_test.rb14
3 files changed, 19 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 24a64e9deb..95f9cf6d14 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -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.
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb
index 3a600835e1..d62cd3f92a 100644
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ -199,6 +199,7 @@ module ActiveRecord
klass.scope value_method_name, -> { where(attr => value) }
end
end
+ enum_values.freeze
end
end
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index 867ce7082b..fef06e6edd 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -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"