Class: Tapioca::Compilers::Dsl::UrlHelpers
- 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 (see 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
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
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Tapioca::Compilers::Dsl::Base
Instance Method Details
#decorate(root, constant) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tapioca/compilers/dsl/url_helpers.rb', line 89 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_constants ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/tapioca/compilers/dsl/url_helpers.rb', line 107 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 |