rails-style-guide icon indicating copy to clipboard operation
rails-style-guide copied to clipboard

Enum: better styling

Open pirj opened this issue 1 year ago • 3 comments

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

pirj avatar Oct 30 '24 10:10 pirj

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

Earlopain avatar Nov 19 '24 12:11 Earlopain

@koic @andyw8 wdyt?

pirj avatar Nov 19 '24 19:11 pirj

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).?

pirj avatar Nov 19 '24 19:11 pirj