Skip to content

Commit 17a2ca0

Browse files
callmesangiobyroot
andcommitted
Fix enum conflict message grammar.
Format the error message so that the correct article is used according to the method type (e.g. "an instance method", "a class method"). Fallback to omitting the method type altogether if unknown, producing the generic wording "a method". Fixes rails#55364 Co-Authored-By: Jean Boussier <[email protected]>
1 parent 7dcec6f commit 17a2ca0

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

activerecord/lib/active_record/enum.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -383,25 +383,25 @@ def assert_valid_enum_options(options)
383383

384384
ENUM_CONFLICT_MESSAGE = \
385385
"You tried to define an enum named \"%{enum}\" on the model \"%{klass}\", but " \
386-
"this will generate a %{type} method \"%{method}\", which is already defined " \
386+
"this will generate %{type} method \"%{method}\", which is already defined " \
387387
"by %{source}."
388388
private_constant :ENUM_CONFLICT_MESSAGE
389389

390390
def detect_enum_conflict!(enum_name, method_name, klass_method = false)
391391
if klass_method && dangerous_class_method?(method_name)
392-
raise_conflict_error(enum_name, method_name, type: "class")
392+
raise_conflict_error(enum_name, method_name, "a class")
393393
elsif klass_method && method_defined_within?(method_name, Relation)
394-
raise_conflict_error(enum_name, method_name, type: "class", source: Relation.name)
394+
raise_conflict_error(enum_name, method_name, "a class", source: Relation.name)
395395
elsif klass_method && method_name.to_sym == :id
396-
raise_conflict_error(enum_name, method_name)
396+
raise_conflict_error(enum_name, method_name, "an instance")
397397
elsif !klass_method && dangerous_attribute_method?(method_name)
398-
raise_conflict_error(enum_name, method_name)
398+
raise_conflict_error(enum_name, method_name, "an instance")
399399
elsif !klass_method && method_defined_within?(method_name, _enum_methods_module, Module)
400-
raise_conflict_error(enum_name, method_name, source: "another enum")
400+
raise_conflict_error(enum_name, method_name, "an instance", source: "another enum")
401401
end
402402
end
403403

404-
def raise_conflict_error(enum_name, method_name, type: "instance", source: "Active Record")
404+
def raise_conflict_error(enum_name, method_name, type, source: "Active Record")
405405
raise ArgumentError, ENUM_CONFLICT_MESSAGE % {
406406
enum: enum_name,
407407
klass: name,

activerecord/test/cases/enum_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,22 @@ def self.name; "Book"; end
584584
end
585585
end
586586

587+
test "conflict message grammar" do
588+
conflicts = {
589+
column: /will generate a class method/,
590+
logger: /will generate an instance method/,
591+
}
592+
593+
conflicts.each do |name, pattern|
594+
assert_raises(ArgumentError, match: pattern) do
595+
Class.new(ActiveRecord::Base) do
596+
self.table_name = "books"
597+
enum name, [:value]
598+
end
599+
end
600+
end
601+
end
602+
587603
test "overriding enum method should not raise" do
588604
assert_nothing_raised do
589605
Class.new(ActiveRecord::Base) do

0 commit comments

Comments
 (0)