rubocop / rails-style-guide

A community-driven Ruby on Rails style guide
http://rails.rubystyle.guide
6.48k stars 1.06k forks source link

Enum: better styling #361

Open pirj opened 1 month ago

pirj commented 1 month ago

We suggest the kwargs-style enum definition:

  enum type: {
    credit: 0,
    debit: 1
  }

This is problematic in two ways.

  1. Passing options like prefix/suffix/scopes/default is complicated (needs to be prefixed with an underscore) and is not documented
  2. Results in weird unrelated errors (mentions the very first defined enum instead of the one that has options)

Ok to change this to use positional args for name and values?

  enum :type, {
    credit: 0,
    debit: 1
  }

With options:

  enum :type, {
    credit: 0,
    debit: 1
  }, suffix: true
Earlopain commented 2 weeks ago

Not sure if you're aware, but there's a cop for this now: https://github.com/rubocop/rubocop-rails/pull/1336 It is only is for Ruby >= 3.0 because on 2.7 it can cause keyword argument warnings (https://github.com/rubocop/rubocop-rails/pull/1363). The new syntax itself was introduced with Rails 7.0

pirj commented 2 weeks ago

@koic @andyw8 wdyt?

pirj commented 2 weeks ago

Off-topic: Rails 6.1 is EOL a month ago, is it a good idea to scrub the guide from legacy content, and leave a note that pre-7 guidelines are only kept in the GitHub repo (in a branch).?