Enum lookup table

My team recently found some utility from adding a small rake task that generates an active_record_enums lookup table with table, enum, value, and value_description as columns. We have a data team that runs reports against a follower db that were having to do this manually on their end.

After railsconf we were trying to think of ways we could start contributing to rails and wondered if this little script might be useful to others.

So then this is generated from all the enums across all models? As you mention about reporting – sounds pretty useful for this.

Yes. Here’s the whole thing as of right now:

  desc "update active_record_enums table"
  task update_enums: :environment do
    class ActiveRecordEnum < ApplicationRecord; end

    Rails.application.eager_load!
    enum_map = ActiveRecord::Base.descendants.sort{ |a, b| a.name <=> b.name }.map { |k| [k.table_name, k.defined_enums] }.to_h
    enum_map.each_pair do |table, enums|
      next if enums.blank?

      puts table
      enums.each_pair do |enum, values|
        puts '-' * 30
        puts "#{enum}:"
        values.each_pair do |description, value|
          record = ActiveRecordEnum.find_or_initialize_by(table: table, column: enum, value: value)
          record.update!(value_description: description) unless record.value_description.presence == description
          puts "created/updated data enum record #{value}:#{description}"
        end
      end
      puts '_' * 30
      puts "\n"
    end
  end
1 Like

I suppose one thing this is missing is the ability to deal with removed values. I wonder if an ‘active’ or inverse ‘deprecated/decommissioned’ column would be useful. Then another pass could run through ActiveRecordEnum.all and compare to the enum_map to see if the enum still exists in the code.

Maybe this would be useful to you in this case: GitHub - pedrorolo/active_module: Modules and Classes as first-class active record values · GitHub

That’s a neat gem, but I’m not sure how I would use it to enhance this process. Could you say more about where you think it would be useful.

For anyone who’s interested in this here’s the table from our schema.rb

  create_table "active_record_enums", force: :cascade do |t|
    t.string "table", null: false
    t.string "column", null: false
    t.string "value", null: false
    t.string "value_description", null: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["table", "column", "value"], name: "index_active_record_enums_on_table_and_column_and_value", unique: true
  end

The use case is for data reporting being run outside of our app that is ingesting tables via something like airbyte and gathering up raw enum values. For a while our data team kept their own maps of things like “status” column with value “0” means “draft” etc. This table and the rake task above gives them an easier way to get that label and avoids communication breakdowns when developers add a new option.

Well… this gem allows you to turn the enum values into objects (Module or class objects).

Thus, you can define on these objects whatever methods you need them to have e.g. description:

module EnumValue1
  def description="description1"
end

module EnumValue2
  def description="description2"
end

class ARClass < ActiveRecord::Base
  attribute :enum_field, :active_module, possible_modules: [EnumValue1, EnumValue2]
  active_module_enum :enum_field
end

ARClass.enum_fields.keys.map(&:description) #=> ["description1", "description2"]

ar_object = ArClass.new enum_field: :enum_value1

ar_object.enum_field.description #=> "description1"

I suspect that’s more than I need for this particular use case, but I can see the utility for more complex use cases.