You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we can see many positive reactions in rails/rails#49100, many people seem to love the feature. So, I think it's worth adding a new cop to suggest using the validate option for enums.
Describe the solution you'd like
The new cop would suggest validate as below if enabled:
classUser < ActiveRecord::Base# badenum:status,[:enabled,:disabled]# ^^^^ Prefer the `:validate` option over `ArgumentError`.# goodenum:activity,[:active,:inactive],validate: trueend
But first, I believe this cop should be disabled by default because the code's behavior would change. We could enable the cop by default when many people are familiar with this feature (I hope this cop can help that 😁).
Describe alternatives you've considered
I have no alternatives for now.
Additional context
Here is a reproduction of this feature, although it's a bit too long: ⬇️
Reproduction code
require"bundler/inline"gemfile(true)dosource"https://rubygems.org"gem"rails","~> 7.1"gem"sqlite3","~> 1.7"endrequire"active_record"require"minitest/autorun"require"logger"ActiveRecord::Base.establish_connection(adapter: "sqlite3",database: ":memory:")ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:users,force: truedo |t|
t.column:status,:integert.column:activity,:integerendendclassUser < ActiveRecord::Base# bad (< Rails 7.1)enum:status,[:enabled,:disabled]# good (>= Rails 7.1)enum:activity,[:active,:inactive],validate: trueendclassUserTest < Minitest::Testdefsetup@user=User.newenddeftest_statusassert_raises(ArgumentError,"'foo' is not a valid status")do@user.status=:fooendenddeftest_activity@user.activity=:foorefute@user.valid?assert_equal["Activity is not included in the list"],@user.errors.full_messages@user.activity=:activeassert@user.valid?endend
If you have Docker, you can save this code to test.rb in any directory and run it easily:
Is your feature request related to a problem? Please describe.
Rails 7.1 has added the support Enum validation implemented by PR rails/rails#49100. For example:
As we can see many positive reactions in rails/rails#49100, many people seem to love the feature. So, I think it's worth adding a new cop to suggest using the
validate
option for enums.Describe the solution you'd like
The new cop would suggest
validate
as below if enabled:But first, I believe this cop should be disabled by default because the code's behavior would change. We could enable the cop by default when many people are familiar with this feature (I hope this cop can help that 😁).
Describe alternatives you've considered
I have no alternatives for now.
Additional context
Here is a reproduction of this feature, although it's a bit too long: ⬇️
Reproduction code
If you have Docker, you can save this code to
test.rb
in any directory and run it easily:docker run --rm -it -v ${PWD}:/work -w /work ruby:3.3 ruby test.rb
Thanks for reading.
The text was updated successfully, but these errors were encountered: