Skip to content

Commit b5fe9ec

Browse files
committed
Revert "Merge pull request rails#49769 from jonathanhefner/active_record-enum-non-column-backed"
This reverts commit 121e0ad, reversing changes made to d68e439. Fixes rails#52607. But this reintroduce rails#49717. Since enum not backed by columns are less common, we can live with that for now.
1 parent 4330bbe commit b5fe9ec

File tree

4 files changed

+19
-30
lines changed

4 files changed

+19
-30
lines changed

activerecord/lib/active_record/enum.rb

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ def self.extended(base) # :nodoc:
167167
base.class_attribute(:defined_enums, instance_writer: false, default: {})
168168
end
169169

170+
def load_schema! # :nodoc:
171+
defined_enums.each_key do |name|
172+
unless columns_hash.key?(resolve_attribute_name(name))
173+
raise "Unknown enum attribute '#{name}' for #{self.name}. Enums must be" \
174+
" backed by a database column."
175+
end
176+
end
177+
end
178+
170179
class EnumType < Type::Value # :nodoc:
171180
delegate :type, to: :subtype
172181

@@ -255,13 +264,7 @@ def _enum(name, values, prefix: nil, suffix: nil, scopes: true, instance_methods
255264

256265
attribute(name, **options)
257266

258-
decorate_attributes([name]) do |_name, subtype|
259-
if subtype == ActiveModel::Type.default_value
260-
raise "Undeclared attribute type for enum '#{name}' in #{self.name}. Enums must be" \
261-
" backed by a database column or declared with an explicit type" \
262-
" via `attribute`."
263-
end
264-
267+
decorate_attributes([name]) do |name, subtype|
265268
subtype = subtype.subtype if EnumType === subtype
266269
EnumType.new(name, enum_values, subtype, raise_on_invalid_values: !validate)
267270
end

activerecord/lib/active_record/model_schema.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ def load_schema!
594594
columns_hash = schema_cache.columns_hash(table_name)
595595
columns_hash = columns_hash.except(*ignored_columns) unless ignored_columns.empty?
596596
@columns_hash = columns_hash.freeze
597+
598+
super
597599
end
598600

599601
# Guesses the table name, but does not decorate it with prefix and suffix information.

activerecord/test/cases/enum_test.rb

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,28 +1157,15 @@ def self.name
11571157
end
11581158

11591159
test "raises for attributes with undeclared type" do
1160-
klass = assert_deprecated(ActiveRecord.deprecator) do
1161-
Class.new(Book) do
1162-
def self.name; "Book"; end
1163-
enum typeless_genre: [:adventure, :comic]
1164-
end
1160+
klass = Class.new(Book) do
1161+
def self.name; "Book"; end
1162+
enum :typeless_genre, [:adventure, :comic]
11651163
end
11661164

11671165
error = assert_raises(RuntimeError) do
1168-
klass.type_for_attribute(:typeless_genre)
1169-
end
1170-
assert_match "Undeclared attribute type for enum 'typeless_genre' in Book", error.message
1171-
end
1172-
1173-
test "supports attributes declared with a explicit type" do
1174-
klass = assert_deprecated(ActiveRecord.deprecator) do
1175-
Class.new(Book) do
1176-
attribute :my_genre, :integer
1177-
enum my_genre: [:adventure, :comic]
1178-
end
1166+
klass.type_for_attribute(:typeless_genre) # load schema
11791167
end
1180-
1181-
assert_equal :integer, klass.type_for_attribute(:my_genre).type
1168+
assert_match "Unknown enum attribute 'typeless_genre'", error.message
11821169
end
11831170

11841171
test "default methods can be disabled by :_instance_methods" do

railties/test/application/test_test.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,14 +335,11 @@ class UserTest < ActiveSupport::TestCase
335335

336336
app_file "app/models/user.rb", <<-RUBY
337337
class User < ApplicationRecord
338-
def self.load_schema!
339-
super
340-
raise "SCHEMA LOADED!"
341-
end
338+
enum :type, [:admin, :user]
342339
end
343340
RUBY
344341

345-
assert_unsuccessful_run "models/user_test.rb", "SCHEMA LOADED!"
342+
assert_unsuccessful_run "models/user_test.rb", "Unknown enum attribute 'type' for User"
346343
end
347344

348345
private

0 commit comments

Comments
 (0)