Skip to content

Comments

Allow new syntax for enum to avoid leading _ from reserved options#41328

Merged
kamipo merged 1 commit intorails:mainfrom
kamipo:avoid-leading-_-from-reserved-options
Feb 19, 2021
Merged

Allow new syntax for enum to avoid leading _ from reserved options#41328
kamipo merged 1 commit intorails:mainfrom
kamipo:avoid-leading-_-from-reserved-options

Conversation

@kamipo
Copy link
Member

@kamipo kamipo commented Feb 4, 2021

Unlike other features built on Attribute API, reserved options for
enum has leading _.

That is due to enum takes one hash argument only, which contains both
enum definitions and reserved options.

I propose new syntax for enum to avoid leading _ from reserved
options, by allowing enum(attr_name, ..., **options) more Attribute
API like syntax.

Before:

class Book < ActiveRecord::Base
  enum status: [ :proposed, :written ], _prefix: true, _scopes: false
  enum cover: [ :hard, :soft ], _suffix: true, _default: :hard
end

After:

class Book < ActiveRecord::Base
  enum :status, [ :proposed, :written ], prefix: true, scopes: false
  enum :cover, [ :hard, :soft ], suffix: true, default: :hard
end

@kamipo kamipo force-pushed the avoid-leading-_-from-reserved-options branch from f46743a to 02ad7ba Compare February 5, 2021 03:15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we deprecate all those options and the old syntax? We can probably include a Cop to do the migration automatically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good idea to me if happening the migration is ok for us.

I thought since it is confusing that the old syntax options has leading _ but the new syntax options doesn't have that, I'd also like to deprecate defining enum on the attribute names (:prefix, :suffix, :scopes, and :default) in the old syntax to reserve the names to make it as options in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to deprecate in this release, but if we can easily autocorrect the deprecation with a cop, I think we should.

Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more question, but ultimately 👍

Unlike other features built on Attribute API, reserved options for
`enum` has leading `_`.

* `_prefix`/`_suffix`: rails#19813, rails#20999
* `_scopes`: rails#34605
* `_default`: rails#39820

That is due to `enum` takes one hash argument only, which contains both
enum definitions and reserved options.

I propose new syntax for `enum` to avoid leading `_` from reserved
options, by allowing `enum(attr_name, ..., **options)` more Attribute
API like syntax.

Before:

```ruby
class Book < ActiveRecord::Base
  enum status: [ :proposed, :written ], _prefix: true, _scopes: false
  enum cover: [ :hard, :soft ], _suffix: true, _default: :hard
end
```

After:

```ruby
class Book < ActiveRecord::Base
  enum :status, [ :proposed, :written ], prefix: true, scopes: false
  enum :cover, [ :hard, :soft ], suffix: true, default: :hard
end
```
@kamipo kamipo force-pushed the avoid-leading-_-from-reserved-options branch from 3e8bf3c to 0618d2d Compare February 19, 2021 05:48
@kamipo kamipo merged commit ee7cf8c into rails:main Feb 19, 2021
@kamipo kamipo deleted the avoid-leading-_-from-reserved-options branch February 19, 2021 06:01
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
`
tobyprivett added a commit to ministryofjustice/hmpps-book-secure-move-api that referenced this pull request Oct 10, 2024
tobyprivett added a commit to ministryofjustice/hmpps-book-secure-move-api that referenced this pull request Nov 11, 2024
tobyprivett added a commit to ministryofjustice/hmpps-book-secure-move-api that referenced this pull request Nov 15, 2024
tobyprivett added a commit to ministryofjustice/hmpps-book-secure-move-api that referenced this pull request Nov 18, 2024
* Rails 8 beta

* Migrate to new enum

rails/rails#41328

* Resolve: DEPRECATION WARNING: `to_time` will always preserve the full timezone

* Update spec with new error message

* Rails 8 prefers flipper-active_record from HEAD

* Update Rails to 8.0.0

* Run `rails app:update`

* Load Rails 8.0 defaults

* Run `bundle update`

* Remove `to_time_preserves_timezone` as it is a Rails 8 default

* Remove flipper

- The gem is outdated and the feature flag was last used once, 5 years ago

* Pin rubocop to a commit to avoid continual dependabot bumps

- Hopefully GDS will tag a new release and we won't have to do this anymore
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