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

@alfie-max alfie-max commented Dec 3, 2018

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 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 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 alfie-max force-pushed the disable_enum_scopes branch from a7de8af to 2e5e5f5 Dec 3, 2018
@alfie-max
Copy link
Contributor Author

@alfie-max alfie-max commented Dec 3, 2018

@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
@alfie-max alfie-max force-pushed the disable_enum_scopes branch from 2e5e5f5 to fa92fa1 Dec 3, 2018
Copy link
Member

@gmcgibbon gmcgibbon left a comment

👍 LGTM. Please squash your commits

@alfie-max
Copy link
Contributor Author

@alfie-max alfie-max commented Dec 3, 2018

Sure.. just a min

@alfie-max alfie-max force-pushed the disable_enum_scopes branch from fa92fa1 to 3362877 Dec 3, 2018
@alfie-max
Copy link
Contributor Author

@alfie-max alfie-max commented Dec 3, 2018

should i add this to the CHANGELOG?

@alfie-max alfie-max force-pushed the disable_enum_scopes branch from 3362877 to 3670d60 Dec 3, 2018
@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Dec 3, 2018

👍 for a changelog entry

@alfie-max alfie-max force-pushed the disable_enum_scopes branch from 3670d60 to f8abaf6 Dec 3, 2018
@alfie-max alfie-max force-pushed the disable_enum_scopes branch from f8abaf6 to 6267ee9 Dec 4, 2018
@alfie-max alfie-max force-pushed the disable_enum_scopes branch from 6267ee9 to c4d157a Dec 4, 2018
@rafaelfranca rafaelfranca merged commit e517226 into rails:master Dec 4, 2018
2 checks passed
@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Dec 4, 2018

Thank you so much!

@alfie-max alfie-max deleted the disable_enum_scopes branch Dec 4, 2018
@SamSaffron
Copy link
Contributor

@SamSaffron SamSaffron commented Dec 4, 2018

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

@alfie-max
Copy link
Contributor Author

@alfie-max alfie-max commented Dec 5, 2018

thanks @SamSaffron 😄

suketa added a commit to suketa/rails_sandbox that referenced this issue 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 issue 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 issue Feb 4, 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
```
kamipo added a commit to kamipo/rails that referenced this issue Feb 5, 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
```
kamipo added a commit to kamipo/rails that referenced this issue 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
```
brendon added a commit to brendon/rails that referenced this issue Jun 17, 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
```
wbotelhos added a commit to wbotelhos/rails that referenced this issue Jul 18, 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
Linked issues

Successfully merging this pull request may close these issues.

4 participants