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

Better handling of negative elements in enum #40679

Merged
merged 1 commit into from Dec 9, 2020

Conversation

ghiculescu
Copy link
Member

@ghiculescu ghiculescu commented Nov 24, 2020

Resolves #39065; fixes a warning added in #36380 that fires a bit too often. Adds a more descriptive warning, and logs it only when there's actually a conflict.

An example of where this is annoying is in our app:

class MobileDeviceToken < ApplicationRecord
  enum inactive_reason: { :not_applicable => 0, :user_logged_out => 1, :fcm_error => 2 }
end

Results in this warning every time the app boots:

An enum element in MobileDeviceToken uses the prefix 'not_'. This will cause a conflict with auto generated negative scopes.

Even though there isn't actually anything we need to change.

@ghiculescu ghiculescu changed the title Better handling of negative words in enum Better handling of negative elements in enum Nov 24, 2020
activerecord/lib/active_record/enum.rb Outdated Show resolved Hide resolved
"Book"
end
silence_warnings do
enum status: [:not_sent, :sent]
Copy link
Member

Choose a reason for hiding this comment

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

Missing a test with _scopes: false since we should not show the warning if that option is passed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good pickup. Added a new test + this fix.

Fixes rails#39065. Adds a more descriptive warning, and logs it less often.
@rafaelfranca rafaelfranca merged commit 354202e into rails:master Dec 9, 2020
rafaelfranca added a commit that referenced this pull request Dec 9, 2020
Better handling of negative elements in enum
@ghiculescu ghiculescu deleted the negative-enum-warning branch December 9, 2020 17:23
rafaelfranca added a commit that referenced this pull request Dec 9, 2020
Better handling of negative elements in enum
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.

Better handling of negative words in enum
2 participants