为什么 Tapioca 生成的 rbi 不包含 Devise current_user 方法?

Vin*_*cha 5 ruby ruby-on-rails sorbet

我正在尝试将 Devise 添加到使用 Sorbet 的 Rails 项目中。添加 Devise 后,我运行bundle exec tapioca dslbundle exec tapioca gem. 现在,在我的控制器上,类型检查抱怨它找不到current_user帮助器方法。

我注意到生成的文件中sorbet/rbi/gems/devise@4.9.2.rbi包含以下描述:

  class << self
    # Define authentication filters and accessor helpers based on mappings.
    # These filters should be used inside the controllers as before_actions,
    # so you can control the scope of the user who should be signed in to
    # access that specific controller/action.
    # Example:
    #
    #   Roles:
    #     User
    #     Admin
    #
    #   Generated methods:
    #     authenticate_user!  # Signs user in or redirect
    #     authenticate_admin! # Signs admin in or redirect
    #     user_signed_in?     # Checks whether there is a user signed in or not
    #     admin_signed_in?    # Checks whether there is an admin signed in or not
    #     current_user        # Current signed in user
    #     current_admin       # Current signed in admin
    #     user_session        # Session data available only to the user scope
    #     admin_session       # Session data available only to the admin scope
    #
    #   Use:
    #     before_action :authenticate_user!  # Tell devise to use :user map
    #     before_action :authenticate_admin! # Tell devise to use :admin map
    #
    # source://devise//lib/devise/controllers/helpers.rb#112
    def define_helpers(mapping); end
  end
Run Code Online (Sandbox Code Playgroud)

我是否缺少某些东西来正确识别这些方法bundle exec srb tc

在我的ApplicationController课堂上,我尝试这样做include Devise::Controllers::Helpers,但这没有帮助。

Vin*_*cha 1

我在木薯存储库上找到了我在那里创建的这个GitHub 问题的答案。

我引用Kaan Ozkan的回答:

这些方法是在调用define_helpers 时定义的。因此,define_helpers 需要在 RBI 生成期间触发,以便它们在运行时可见并由 Tapioca 生成。

我认为最简单的解决方案是手动定义所有生成的方法sorbet/rbi/shims/gems/devise.rbi

module Devise::Controllers::Helpers::ClassMethods
  def current_user; end
end
Run Code Online (Sandbox Code Playgroud)