Skip to content

Commit 1a92af2

Browse files
authored
Merge pull request rails#50987 from skipkayhil/hm-deprecate-kw-enum
Deprecate defining enums with keywords args
2 parents f0adde9 + 8c54251 commit 1a92af2

File tree

12 files changed

+198
-122
lines changed

12 files changed

+198
-122
lines changed

activerecord/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
* Deprecate defining an `enum` with keyword arguments.
2+
3+
```ruby
4+
class Function > ApplicationRecord
5+
# BAD
6+
enum color: [:red, :blue],
7+
type: [:instance, :class]
8+
9+
# GOOD
10+
enum :color, [:red, :blue]
11+
enum :type, [:instance, :class]
12+
end
13+
```
14+
15+
*Hartley McGuire*
16+
117
* Add `active_record.config.validate_migration_timestamps` option for validating migration timestamps.
218

319
When set, validates that the timestamp prefix for a migration is no more than a day ahead of

activerecord/lib/active_record/enum.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ def enum(name = nil, values = nil, **options)
223223
options.transform_keys! { |key| :"#{key[1..-1]}" }
224224

225225
definitions.each { |name, values| _enum(name, values, **options) }
226+
227+
ActiveRecord.deprecator.warn(<<~MSG)
228+
Defining enums with keyword arguments is deprecated and will be removed
229+
in Rails 7.3. Positional arguments should be used instead:
230+
231+
#{definitions.map { |name, values| "enum :#{name}, #{values}" }.join("\n")}
232+
MSG
226233
end
227234

228235
private

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ class SpecialBook < ActiveRecord::Base
486486
self.table_name = "books"
487487

488488
belongs_to :author
489-
enum last_read: { unread: 0, reading: 2, read: 3, forgotten: nil }
489+
enum :last_read, { unread: 0, reading: 2, read: 3, forgotten: nil }
490490
end
491491

492492
def test_association_enum_works_properly

activerecord/test/cases/associations/has_one_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ class SpecialBook < ActiveRecord::Base
807807
belongs_to :author, class_name: "SpecialAuthor"
808808
has_one :subscription, class_name: "SpecialSubscription", foreign_key: "subscriber_id"
809809

810-
enum status: [:proposed, :written, :published]
810+
enum :status, [:proposed, :written, :published]
811811
end
812812

813813
class SpecialAuthor < ActiveRecord::Base

activerecord/test/cases/attribute_methods_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,8 @@ class ChildWithDeprecatedBehaviorResolved < ClassWithDeprecatedAliasAttributeBeh
13811381
self.table_name = "books"
13821382

13831383
attribute :status, :string
1384-
enum status: {
1384+
1385+
enum :status, {
13851386
pending: "0",
13861387
completed: "1",
13871388
}

0 commit comments

Comments
 (0)