Proposal: Add human_enum to transform the current enum into a more human format, using I18n

When defining an enum, such as

class Conversation < ActiveRecord::Base
  enum status: [ :active, :archived ]
end

There are some methods defined, like statuses to get the list of statuses. It would useful to also have a human_status, that returns the status in a human manner, including localizations.

Active Model .human method uses I18n to translate the model name by default, and there seems to be a translation pattern that fits enums perfectly using the <model>/<enum> format:

en:
  activerecord:
    attributes:
      conversation/status:
        active: "Active"
        archived: "Archived"

My proposal is that the enum should also define a human_#{enum} method, like human_status in this case

conversation.human_status # => "Active"

It would follow the same principle as the existing .human method, where there is a standard way to get a readable format along with translations.

I have a rough idea on where to implement this - avoiding further work to confirm that this is something that would be useful. Never did a Rails proposal, feedback is most welcome :slight_smile:

3 Likes