Permalink
Browse files

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

…definition-values

Enum raises on invalid definition values
  • Loading branch information...
eileencodes committed Oct 10, 2018
2 parents 9413ef5 + df11558 commit 58999af99fa485f748ab33d4aa4254a4b4a50991
Showing with 33 additions and 0 deletions.
  1. +11 −0 activerecord/CHANGELOG.md
  2. +11 −0 activerecord/lib/active_record/enum.rb
  3. +11 −0 activerecord/test/cases/enum_test.rb
View
@@ -1,3 +1,14 @@
* 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"

0 comments on commit 58999af

Please sign in to comment.