Class: Tapioca::Compilers::Dsl::UrlHelpers

Inherits:
Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/compilers/dsl/url_helpers.rb

Overview

‘Tapioca::Compilers::Dsl::UrlHelpers` generates RBI files for classes that include or extend [`Rails.application.routes.url_helpers`](api.rubyonrails.org/v5.1.7/classes/ActionDispatch/Routing/UrlFor.html#module-ActionDispatch::Routing::UrlFor-label-URL+generation+for+named+routes).

For example, with the following setup:

~~~rb # config/application.rb class Application < Rails::Application

routes.draw do
  resource :index
end

end ~~~

~~~rb 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
# generator 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 generator will produce the following RBI files:

~~~rbi # 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 ~~~

~~~rbi # 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 ~~~

~~~rbi # post.rbi # typed: true class Post

include GeneratedPathHelpersModule
include GeneratedUrlHelpersModule

end ~~~

Constant Summary collapse

NON_DISCOVERABLE_INCLUDERS =
T.let([
  ActionDispatch::IntegrationTest,
  ActionView::Helpers,
], T::Array[Module])

Instance Attribute Summary

Attributes inherited from Base

#processable_constants

Instance Method Summary collapse

Methods inherited from Base

#handles?, #initialize

Constructor Details

This class inherits a constructor from Tapioca::Compilers::Dsl::Base

Instance Method Details

#decorate(root, constant) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/tapioca/compilers/dsl/url_helpers.rb', line 93

def decorate(root, constant)
  case constant
  when GeneratedPathHelpersModule.singleton_class, GeneratedUrlHelpersModule.singleton_class
    generate_module_for(root, constant)
  else
    root.path(constant) do |mod|
      create_mixins_for(mod, constant, GeneratedUrlHelpersModule)
      create_mixins_for(mod, constant, GeneratedPathHelpersModule)
    end
  end
end

#gather_constantsObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tapioca/compilers/dsl/url_helpers.rb', line 111

def gather_constants
  Object.const_set(:GeneratedUrlHelpersModule, Rails.application.routes.named_routes.url_helpers_module)
  Object.const_set(:GeneratedPathHelpersModule, Rails.application.routes.named_routes.path_helpers_module)

  module_enumerator = T.cast(ObjectSpace.each_object(Module), T::Enumerator[Module])
  constants = module_enumerator.select do |mod|
    next unless Module.instance_method(:name).bind(mod).call

    includes_helper?(mod, GeneratedUrlHelpersModule) ||
      includes_helper?(mod, GeneratedPathHelpersModule) ||
      includes_helper?(mod.singleton_class, GeneratedUrlHelpersModule) ||
      includes_helper?(mod.singleton_class, GeneratedPathHelpersModule)
  end

  constants.concat(NON_DISCOVERABLE_INCLUDERS)
end