未验证 提交 58999af9 编写于 作者: E Eileen M. Uchitelle 提交者: GitHub

Merge pull request #34110 from albertoalmagro/enum-raises-on-invalid-definition-values

Enum raises on invalid definition values
* Enum raises on invalid definition values
When defining a Hash enum it can be easy to use [] instead of {}. This
commit checks that only valid definition values are provided, those can
be a Hash, an array of Symbols or an array of Strings. Otherwise it
raises an ArgumentError.
Fixes #33961
*Alberto Almagro*
* Reloading associations now clears the Query Cache like `Persistence#reload` does.
```
......
......@@ -150,6 +150,7 @@ def enum(definitions)
enum_prefix = definitions.delete(:_prefix)
enum_suffix = definitions.delete(:_suffix)
definitions.each do |name, values|
assert_valid_enum_definition_values(values)
# statuses = { }
enum_values = ActiveSupport::HashWithIndifferentAccess.new
name = name.to_s
......@@ -210,10 +211,20 @@ def _enum_methods_module
end
end
def assert_valid_enum_definition_values(values)
unless values.is_a?(Hash) || values.all? { |v| v.is_a?(Symbol) } || values.all? { |v| v.is_a?(String) }
error_message = <<~MSG
Enum values #{values} must be either a hash, an array of symbols, or an array of strings.
MSG
raise ArgumentError, error_message
end
end
ENUM_CONFLICT_MESSAGE = \
"You tried to define an enum named \"%{enum}\" on the model \"%{klass}\", but " \
"this will generate a %{type} method \"%{method}\", which is already defined " \
"by %{source}."
private_constant :ENUM_CONFLICT_MESSAGE
def detect_enum_conflict!(enum_name, method_name, klass_method = false)
if klass_method && dangerous_class_method?(method_name)
......
......@@ -265,6 +265,17 @@ class EnumTest < ActiveRecord::TestCase
assert_equal "published", @book.status
end
test "invalid definition values raise an ArgumentError" do
e = assert_raises(ArgumentError) do
Class.new(ActiveRecord::Base) do
self.table_name = "books"
enum status: [proposed: 1, written: 2, published: 3]
end
end
assert_match(/must be either a hash, an array of symbols, or an array of strings./, e.message)
end
test "reserved enum names" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "books"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册