Search code examples
ruby-on-railsrubysorbet

How to Generate .RBI file for an ActiveModel Class


I have a PORO that I'm using as a value object. I'm adding ActiveModel via include ActiveModel::Model for all of the niceties that that brings me. The problem is that I cannot figure out how to generate RBI files for it so that it will pass srb tc

I'm currently using sorbet-rails and since this is a PORO and does not inherit from ActiveRecord, bundle exec rails_rbi:all or bundle exec rails_rbi:models will not generate .rbi files for this even if I place it in the models dir (which it should not live in).

I could write the .rbi files by hand, but I really don't want to do that. Is there a way that I can auto generate them?

Here is a parred down example of the module I need an RBI file for

module MyModule
  class MyClass
    include ActiveModel::Model
    extend T::Sig

    VALID_VARIANTS = T.let(['Enum1', 'Enum2'], T::Array[String])

    sig { returns(String) }
    attr_accessor :variant

    validates :variant, presence: true, inclusion: { in: VALID_VARIANTS }
    validate :enum_1_is_valid, if: Proc.new { |a| a.variant == 'Enum1' }

    private

    sig { void }
    def enum_1_is_valid
      # ...
    end
  end
end

Solution

  • The issue is that sorbet-typed does not include ActiveModel::Model which is just a wrapper around ActiveModel::Validations and a few others. By switching to only using ActiveModel::Validations, the code now passes typed: strict.

    The main downside to this is we don't get the nice automatic attribute assignment and still need to use an initializer. Not a big deal, but not perfect.