rails_sorbet_enum

A gem that adds support for using typed Sorbet T::Enums as Rails ActiveRecord enum columns.

Installation

Install the gem and add to the application's Gemfile by executing:

bundle add rails_sorbet_enum

Usage

Declare the enum by including RailsSorbetEnum::Concern and calling sorbet_enum.

# app/models/my_model.rb
class MyModel < ApplicationModel
  include RailsSorbetEnum::Concern

  class Status < T::Enum
    enums do
      Pending = new('pending')
      Done = new('done')
    end
  end

  # :status is the column name with `string` type
  sorbet_enum(:status, Status)
end

The status field will be of the enum type, rather than a string.

my_model.status                          #=> MyModel::Status
my_model.status = MyModel::Status::Done

# If the enum field is set to a non-enum value, the assignment will raise
# rather than failing validation.
my_model.status = 'done'                 #=> raises ActiveRecord::SerializationTypeMismatch

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.