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

wnrlj8wa  于 2023-11-18  发布在  Ruby
关注(0)|答案(1)|浏览(134)

我试图将Devise添加到使用Sorbet的Rails项目中。添加Devise后,我运行了bundle exec tapioca dslbundle exec tapioca gem。现在,在我的控制器上,类型检查报告无法找到current_user辅助方法。
我注意到,在生成的文件sorbet/rbi/gems/ [[email protected]](https://stackoverflow.com/cdn-cgi/l/email-protection)上,它包括以下描述:

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

字符串
我是否遗漏了一些东西,以便用bundle exec srb tc正确识别这些方法?
在我的ApplicationController类中,我尝试了include Devise::Controllers::Helpers,但没有帮助。

2cmtqfgy

2cmtqfgy1#

我在tapioca repository上得到了答案,就在我创建的这个GitHub issue上。
我引用Kaan Ozkan的回答:
这些方法是在调用define_helpers时定义的。因此,define_helpers需要在RBI生成期间触发,以便它们在运行时可见并由tapioca生成。
我认为最简单的解决方案是在sorbet/rbi/shims/gems/devise.rbi中手动定义所有生成的方法:

module Devise::Controllers::Helpers::ClassMethods
  def current_user; end
end

字符串

相关问题