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

option to disable all scopes that ActiveRecord.enum generates #34605

Merged
merged 1 commit into from
Dec 4, 2018

Conversation

alfie-max
Copy link
Contributor

Summary

Fixes #34599

By default ActiveRecord.enum generates several default scopes based on the values. This PR adds an option to disable those scopes which could possibly cause conflicts with existing methods for the model.

Other Information

@alfie-max
Copy link
Contributor Author

alfie-max commented Dec 3, 2018

i just gave this a try.. @rafaelfranca kindly check this out and maybe i can work on making some changes if necessary

@SamSaffron
Copy link
Contributor

SamSaffron commented Dec 3, 2018

This looks correct to me but we should have a test I think, that way we can confirm it will not regress, params with the name _enum etc. end up being prime candidates long term for removal cause they do not feel quite right starting with _ is usually reserved for ignored vars.

@rafaelfranca rafaelfranca added this to the 6.0.0 milestone Dec 3, 2018
@alfie-max
Copy link
Contributor Author

@SamSaffron added a test.. Not sure if that would suffice. Actually caught a case which i missed which would be the default case while adding the test.

activerecord/lib/active_record/enum.rb Outdated Show resolved Hide resolved
activerecord/lib/active_record/enum.rb Outdated Show resolved Hide resolved
activerecord/test/cases/enum_test.rb Outdated Show resolved Hide resolved
Copy link
Member

@gmcgibbon gmcgibbon left a comment

Choose a reason for hiding this comment

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

👍 LGTM. Please squash your commits

@alfie-max
Copy link
Contributor Author

Sure.. just a min

@alfie-max
Copy link
Contributor Author

should i add this to the CHANGELOG?

@rafaelfranca
Copy link
Member

👍 for a changelog entry

@rafaelfranca rafaelfranca merged commit e517226 into rails:master Dec 4, 2018
@rafaelfranca
Copy link
Member

Thank you so much!

@alfie-max alfie-max deleted the disable_enum_scopes branch December 4, 2018 17:05
@SamSaffron
Copy link
Contributor

congrats @alfie-max on getting your first Rails PR done! Thanks

@alfie-max
Copy link
Contributor Author

thanks @SamSaffron 😄

suketa added a commit to suketa/rails_sandbox that referenced this pull request Aug 4, 2019
option to disable all scopes that `ActiveRecord.enum`
generates
rails/rails#34605
suketa added a commit to suketa/rails_sandbox that referenced this pull request Aug 4, 2019
option to disable all scopes that `ActiveRecord.enum`
generates
rails/rails#34605
kamipo added a commit to kamipo/rails that referenced this pull request Feb 19, 2021
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
```
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.

Add option to disable scopes in ActiveRecord.enum
4 participants