Skip to content

Commit

Permalink
Add class name to enum validation exception message
Browse files Browse the repository at this point in the history
Unify enum without column exception message

Update enum_test for new exception message
  • Loading branch information
amkisko authored and rafaelfranca committed Feb 9, 2024
1 parent 1352bd7 commit b55b558
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/enum.rb
Expand Up @@ -249,7 +249,7 @@ def _enum(name, values, prefix: nil, suffix: nil, scopes: true, instance_methods

decorate_attributes([name]) do |_name, subtype|
if subtype == ActiveModel::Type.default_value
raise "Undeclared attribute type for enum '#{name}'. Enums must be" \
raise "Undeclared attribute type for enum '#{name}' in #{self.name}. Enums must be" \
" backed by a database column or declared with an explicit type" \
" via `attribute`."
end
Expand Down
3 changes: 2 additions & 1 deletion activerecord/test/cases/enum_test.rb
Expand Up @@ -1057,13 +1057,14 @@ def self.name

test "raises for attributes with undeclared type" do
klass = Class.new(Book) do
def self.name; "Book"; end
enum typeless_genre: [:adventure, :comic]
end

error = assert_raises(RuntimeError) do
klass.type_for_attribute(:typeless_genre)
end
assert_match "Undeclared attribute type for enum 'typeless_genre'", error.message
assert_match "Undeclared attribute type for enum 'typeless_genre' in Book", error.message
end

test "supports attributes declared with a explicit type" do
Expand Down

0 comments on commit b55b558

Please sign in to comment.