Skip to content

Comments

Deprecate defining enums with keywords args#50987

Merged
rafaelfranca merged 1 commit intorails:mainfrom
skipkayhil:hm-deprecate-kw-enum
Feb 10, 2024
Merged

Deprecate defining enums with keywords args#50987
rafaelfranca merged 1 commit intorails:mainfrom
skipkayhil:hm-deprecate-kw-enum

Conversation

@skipkayhil
Copy link
Member

Motivation / Background

Enums have historically been defined using keyword arguments:

class Function > ApplicationRecord
  enum color: [:red, :blue],
       type: [:instance, :class],
       _scopes: false

This has the advantage of being able to define multiple enums at once with the same options. However, it also has a downside that enum options must be prefixed with an underscore to separate them from the enum definitions (to enable models to have enums with the same name as an option).

In Rails 7, a new syntax was introduced to instead define enums with positional arguments:

class Function > ApplicationRecord
  enum :color, [:red, :blue], scopes: false
  enum :type, [:instance, :class], scopes: false

This new syntax eliminates the need to prefix options with an underscore, and the docs were updated to recommend this new syntax.

However, both versions of the API have been supported since, and it has started to cause some problems:

The first issue is that the available options have drifted. In Rails 7.1, an option was added to make assigning an invalid enum value use validation errors instead of runtime errors. However, the equivalent underscored prefix option was not added for the original enum syntax

Articles have been created that describe the new option in Rails 7.1, but the examples in the articles use un-prefixed options with the old syntax. This confusion has also lead to issues opened asking why that incorrect syntax is not working.

Additionally, the presence of underscored options is just generally confusing because it tends to imply an option is for internal use.

Detail

This commit aims to fix all of these issues by deprecating the old enum syntax. With only one way to define enums, options cannot drift and there will be less confusion around how enums should be defined.

Additional information

Closes #50333

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Enums have historically been defined using keyword arguments:

```ruby
class Function > ApplicationRecord
  enum color: [:red, :blue],
       type: [:instance, :class],
       _scopes: false
```

This has the advantage of being able to define multiple enums at once
with the same options. However, it also has a downside that enum options
must be prefixed with an underscore to separate them from the enum
definitions (to enable models to have enums with the same name as an
option).

In Rails 7, a new syntax was [introduced][1] to instead define enums with
positional arguments:

```ruby
class Function > ApplicationRecord
  enum :color, [:red, :blue], scopes: false
  enum :type, [:instance, :class], scopes: false
```

This new syntax eliminates the need to prefix options with an underscore,
and the docs were updated to recommend this new syntax.

However, both versions of the API have been supported since, and it has
started to cause some problems:

The first issue is that the available options have drifted. In Rails
7.1, an option was added to make assigning an invalid enum value use
validation errors instead of runtime errors. However, the equivalent
underscored prefix option was not added for the original enum syntax

Articles have been created that describe the new option in Rails 7.1,
but the examples in the articles use un-prefixed options with the old
syntax. This confusion has also lead to issues opened asking why that
incorrect syntax is not working.

Additionally, the presence of underscored options is just generally
confusing because it tends to imply an option is for internal use.

This commit aims to fix all of these issues by deprecating the old enum
syntax. With only one way to define enums, options cannot drift and
there will be less confusion around how enums should be defined.

[1]: 0618d2d
@rafaelfranca rafaelfranca merged commit 1a92af2 into rails:main Feb 10, 2024
@skipkayhil skipkayhil deleted the hm-deprecate-kw-enum branch February 12, 2024 15:57
yahonda added a commit to yahonda/rails that referenced this pull request Feb 14, 2024
…lbox

This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed`
warning in Action Mailbox.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails/actionmailbox
bundle install
bin/test test/unit/router_test.rb
```

* Without this tommit
```
$ bin/test test/unit/router_test.rb
... snip ..
DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 7.3. Positional arguments should be used instead:

enum :status, [:pending, :processing, :delivered, :failed, :bounced]
 (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31)
Run options: --seed 65254

...............

Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s.
15 runs, 25 assertions, 0 failures, 0 errors, 0 skips
$
```

Follow up rails#50987
Refer to rails#51037
yahonda added a commit to yahonda/rails that referenced this pull request Feb 14, 2024
…lbox

This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed`
warning in Action Mailbox.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails/actionmailbox
bundle install
bin/test test/unit/router_test.rb
```

* Without this commit
```
$ bin/test test/unit/router_test.rb
... snip ..
DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 7.3. Positional arguments should be used instead:

enum :status, [:pending, :processing, :delivered, :failed, :bounced]
 (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31)
Run options: --seed 65254

...............

Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s.
15 runs, 25 assertions, 0 failures, 0 errors, 0 skips
$
```

Follow up rails#50987
Refer to rails#51037
hendrixfan added a commit to hendrixfan/mastodon that referenced this pull request Feb 15, 2024
As of Rails 7.0, the enum syntax has been updated [1] to positional arguments without the prefix `_`.
The old syntax is still supported [2], but it is recommended to use the new one.

[1] rails/rails#41328
[2] rails/rails#50987
`
hendrixfan added a commit to hendrixfan/mastodon that referenced this pull request Feb 15, 2024
As of Rails 7.0, the enum syntax has been updated [1] to positional arguments without the prefix `_`.
The old syntax is still supported [2], but it is recommended to use the new one.

[1] rails/rails#41328
[2] rails/rails#50987
`
hendrixfan added a commit to hendrixfan/mastodon that referenced this pull request Feb 15, 2024
As of Rails 7.0, the enum syntax has been updated [1] to positional arguments without the prefix `_`.
The old syntax is still supported [2], but it is recommended to use the new one.

[1] rails/rails#41328
[2] rails/rails#50987
`
ghiculescu added a commit to ghiculescu/audits1984 that referenced this pull request Feb 19, 2024
Ridhwana pushed a commit to Ridhwana/rails that referenced this pull request Mar 7, 2024
…lbox

This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed`
warning in Action Mailbox.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails/actionmailbox
bundle install
bin/test test/unit/router_test.rb
```

* Without this commit
```
$ bin/test test/unit/router_test.rb
... snip ..
DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 7.3. Positional arguments should be used instead:

enum :status, [:pending, :processing, :delivered, :failed, :bounced]
 (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31)
Run options: --seed 65254

...............

Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s.
15 runs, 25 assertions, 0 failures, 0 errors, 0 skips
$
```

Follow up rails#50987
Refer to rails#51037
rosa pushed a commit to basecamp/audits1984 that referenced this pull request Mar 21, 2024
viralpraxis pushed a commit to viralpraxis/rails that referenced this pull request Mar 24, 2024
…lbox

This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed`
warning in Action Mailbox.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails/actionmailbox
bundle install
bin/test test/unit/router_test.rb
```

* Without this commit
```
$ bin/test test/unit/router_test.rb
... snip ..
DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 7.3. Positional arguments should be used instead:

enum :status, [:pending, :processing, :delivered, :failed, :bounced]
 (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31)
Run options: --seed 65254

...............

Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s.
15 runs, 25 assertions, 0 failures, 0 errors, 0 skips
$
```

Follow up rails#50987
Refer to rails#51037
fractaledmind pushed a commit to fractaledmind/rails that referenced this pull request May 13, 2024
…lbox

This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed`
warning in Action Mailbox.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails/actionmailbox
bundle install
bin/test test/unit/router_test.rb
```

* Without this commit
```
$ bin/test test/unit/router_test.rb
... snip ..
DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 7.3. Positional arguments should be used instead:

enum :status, [:pending, :processing, :delivered, :failed, :bounced]
 (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31)
Run options: --seed 65254

...............

Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s.
15 runs, 25 assertions, 0 failures, 0 errors, 0 skips
$
```

Follow up rails#50987
Refer to rails#51037
yoones pushed a commit to yoones/rails that referenced this pull request Mar 6, 2025
…lbox

This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed`
warning in Action Mailbox.

* Steps to reproduce
```ruby
git clone https://github.com/rails/rails
cd rails/actionmailbox
bundle install
bin/test test/unit/router_test.rb
```

* Without this commit
```
$ bin/test test/unit/router_test.rb
... snip ..
DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed
in Rails 7.3. Positional arguments should be used instead:

enum :status, [:pending, :processing, :delivered, :failed, :bounced]
 (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31)
Run options: --seed 65254

...............

Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s.
15 runs, 25 assertions, 0 failures, 0 errors, 0 skips
$
```

Follow up rails#50987
Refer to rails#51037
sandbergja added a commit to pulibrary/bibdata that referenced this pull request Mar 21, 2025
* enum has a slightly different API (see rails/rails#50987)
* you now should specify a coder for serialized columns
  (see rails/rails#47463).  Use YAML for backwards compatibility.
* we weren't actually using one of the serialized columns, and it doesn't
  even exist in the database.  Remove it from the model.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants