Merge pull request #48527 from ghiculescu/active-record-enum-id

Disallow `id` as an enum value in Active Record
This commit is contained in:
Guillermo Iguaran 2023-06-19 19:36:42 -07:00 committed by GitHub
commit 8b36095881
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

@ -322,6 +322,8 @@ def detect_enum_conflict!(enum_name, method_name, klass_method = false)
raise_conflict_error(enum_name, method_name, type: "class")
elsif klass_method && method_defined_within?(method_name, Relation)
raise_conflict_error(enum_name, method_name, type: "class", source: Relation.name)
elsif klass_method && method_name == primary_key
raise_conflict_error(enum_name, method_name)
elsif !klass_method && dangerous_attribute_method?(method_name)
raise_conflict_error(enum_name, method_name)
elsif !klass_method && method_defined_within?(method_name, _enum_methods_module, Module)

@ -497,7 +497,8 @@ class EnumTest < ActiveRecord::TestCase
:save, # generates #save!, which conflicts with an AR method
:proposed, # same value as an existing enum
:public, :private, :protected, # some important methods on Module and Class
:name, :parent, :superclass
:name, :parent, :superclass,
:id # conflicts with AR querying
]
conflicts.each_with_index do |value, i|
@ -508,6 +509,16 @@ class EnumTest < ActiveRecord::TestCase
end
end
test "can use id as a value with a prefix or suffix" do
assert_nothing_raised do
Class.new(ActiveRecord::Base) do
self.table_name = "books"
enum status_1: [:id], _prefix: true
enum status_2: [:id], _suffix: true
end
end
end
test "reserved enum values for relation" do
relation_method_samples = [
:records,