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])

Constants included from Reflection

Reflection::ANCESTORS_METHOD, Reflection::CLASS_METHOD, Reflection::CONSTANTS_METHOD, Reflection::EQUAL_METHOD, Reflection::METHOD_METHOD, Reflection::NAME_METHOD, Reflection::OBJECT_ID_METHOD, Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Reflection::SINGLETON_CLASS_METHOD, Reflection::SUPERCLASS_METHOD

Instance Attribute Summary

Attributes inherited from Base

#errors, #processable_constants

Instance Method Summary collapse

Methods inherited from Base

#add_error, #handles?, #initialize

Methods included from Reflection

#ancestors_of, #are_equal?, #class_of, #constants_of, #descendants_of, #inherited_ancestors_of, #method_of, #name_of, #name_of_type, #object_id_of, #private_instance_methods_of, #protected_instance_methods_of, #public_instance_methods_of, #qualified_name_of, #signature_of, #singleton_class_of, #superclass_of

Constructor Details

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

Instance Method Details

#decorate(root, constant) ⇒ Object


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

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

#gather_constantsObject


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

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)

  constants = all_modules.select do |mod|
    next unless name_of(mod)

    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