Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow id as an enum value in Active Record (v2) #48536

Merged
merged 1 commit into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.to_sym == :id
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)
Expand Down
13 changes: 12 additions & 1 deletion activerecord/test/cases/enum_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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,
Expand Down