aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorChristophe Maximin <christophe.maximin@gmail.com>2018-11-07 22:38:41 +0000
committerChristophe Maximin <christophe.maximin@gmail.com>2018-11-07 23:36:39 +0000
commit25f1cbdecd4f506b46a6c476f18074dafdb581f2 (patch)
tree322a0212b12eb6de351076ecf40c635d036bf1de /activerecord
parentfc2684c9c012b95ce003cce22b378d5ea9ab56d3 (diff)
downloadrails-25f1cbdecd4f506b46a6c476f18074dafdb581f2.tar.gz
rails-25f1cbdecd4f506b46a6c476f18074dafdb581f2.tar.bz2
rails-25f1cbdecd4f506b46a6c476f18074dafdb581f2.zip
Guard Enums against definitions with blank label names
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/enum.rb4
-rw-r--r--activerecord/test/cases/enum_test.rb18
3 files changed, 27 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d743076eb4..f57ca1854f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,4 +1,8 @@
-Adds support for multiple databases to `rails db:schema:cache:dump` and `rails db:schema:cache:clear`.
+* Defining an Enum as a Hash with blank key, or as an Array with a blank value, now raises an `ArgumentError`.
+
+ *Christophe Maximin*
+
+* Adds support for multiple databases to `rails db:schema:cache:dump` and `rails db:schema:cache:clear`.
*Gannon McGibbon*
diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb
index 3d97e4e513..3a600835e1 100644
--- a/activerecord/lib/active_record/enum.rb
+++ b/activerecord/lib/active_record/enum.rb
@@ -218,6 +218,10 @@ module ActiveRecord
MSG
raise ArgumentError, error_message
end
+
+ if values.is_a?(Hash) && values.keys.any?(&:blank?) || values.is_a?(Array) && values.any?(&:blank?)
+ raise ArgumentError, "Enum label name must not be blank."
+ end
end
ENUM_CONFLICT_MESSAGE = \
diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb
index b4593ccdf2..867ce7082b 100644
--- a/activerecord/test/cases/enum_test.rb
+++ b/activerecord/test/cases/enum_test.rb
@@ -274,6 +274,24 @@ class EnumTest < ActiveRecord::TestCase
end
assert_match(/must be either a hash, an array of symbols, or an array of strings./, e.message)
+
+ e = assert_raises(ArgumentError) do
+ Class.new(ActiveRecord::Base) do
+ self.table_name = "books"
+ enum status: { "" => 1, "active" => 2 }
+ end
+ end
+
+ assert_match(/Enum label name must not be blank/, e.message)
+
+ e = assert_raises(ArgumentError) do
+ Class.new(ActiveRecord::Base) do
+ self.table_name = "books"
+ enum status: ["active", ""]
+ end
+ end
+
+ assert_match(/Enum label name must not be blank/, e.message)
end
test "reserved enum names" do