Skip to content

Latest commit

 

History

History
74 lines (60 loc) · 1.91 KB

compiler_urlhelpers.md

File metadata and controls

74 lines (60 loc) · 1.91 KB

UrlHelpers

Tapioca::Dsl::Compilers::UrlHelpers generates RBI files for classes that include or extend Rails.application.routes.url_helpers.

For example, with the following setup:

# config/application.rb
class Application < Rails::Application
  routes.draw do
    resource :index
  end
end
app/models/post.rb
class Post
  # Use `T.unsafe` so that Sorbet does not complain about a dynamic
  # module being included. This allows the `include` to happen properly
  # at runtime but Sorbet won't see the include. However, since this
  # compiler will generate the proper RBI files for the include,
  # static type checking will work as expected.
  T.unsafe(self).include Rails.application.routes.url_helpers
end

this compiler will produce the following RBI files:

# generated_path_helpers_module.rbi
# typed: true
module GeneratedPathHelpersModule
  include ActionDispatch::Routing::PolymorphicRoutes
  include ActionDispatch::Routing::UrlFor

  sig { params(args: T.untyped).returns(String) }
  def edit_index_path(*args); end

  sig { params(args: T.untyped).returns(String) }
  def index_path(*args); end

  sig { params(args: T.untyped).returns(String) }
  def new_index_path(*args); end
end
# generated_url_helpers_module.rbi
# typed: true
module GeneratedUrlHelpersModule
  include ActionDispatch::Routing::PolymorphicRoutes
  include ActionDispatch::Routing::UrlFor

  sig { params(args: T.untyped).returns(String) }
  def edit_index_url(*args); end

  sig { params(args: T.untyped).returns(String) }
  def index_url(*args); end

  sig { params(args: T.untyped).returns(String) }
  def new_index_url(*args); end
end
# post.rbi
# typed: true
class Post
  include GeneratedPathHelpersModule
  include GeneratedUrlHelpersModule
end