class Tapioca::Dsl::Compilers::ActionControllerHelpers
~~~
end
def helpers; end
sig { returns(HelperProxy) }
end
include HelperMethods
class HelperProxy < ::ActionView::Base
end
def current_user_name; end
sig { returns(T.untyped) }
def age(user); end
sig { params(user: T.untyped).returns(T.untyped) }
include MyHelper
module HelperMethods
class UserController
# typed: strong
# user_controller.rbi
~~~rbi
this compiler will produce an RBI file ‘user_controller.rbi` with the following content:
~~~
end
end
# …
def current_user_name
helper_method :current_user_name
helper { def age(user) “99” end }
helper MyHelper
class UserController < ActionController::Base
~~~rb
and the following controller:
~~~
end
end
# …
def localized_time
end
# …
def greet(user)
module MyHelper
~~~rb
For example, with the following `MyHelper` module:
subclasses of [`ActionController::Base`](api.rubyonrails.org/classes/ActionController/Helpers.html).
`Tapioca::Dsl::Compilers::ActionControllerHelpers` decorates RBI files for all
def create_unknown_proxy_method(helper_methods, method_name)
def create_unknown_proxy_method(helper_methods, method_name) helper_methods.create_method( method_name.to_s, parameters: [ create_rest_param("args", type: "T.untyped"), create_kw_rest_param("kwargs", type: "T.untyped"), create_block_param("blk", type: "T.untyped"), ], return_type: "T.untyped", ) end
def decorate
def decorate helpers_module = constant._helpers proxied_helper_methods = constant._helper_methods.map(&:to_s).map(&:to_sym) helper_proxy_name = "HelperProxy" helper_methods_name = "HelperMethods" # Define the helpers method root.create_path(constant) do |controller| controller.create_method("helpers", return_type: helper_proxy_name) # Create helper method module controller.create_module(helper_methods_name) do |helper_methods| # If the controller has no helper defined, then it just inherits # the Action Controller base helper methods module, so we should # just add that as an include and stop doing more processing. if helpers_module.name == "ActionController::Base::HelperMethods" next helper_methods.create_include(T.must(qualified_name_of(helpers_module))) end # Find all the included helper modules and generate an include # for each of those helper modules gather_includes(helpers_module).each do |ancestor| helper_methods.create_include(ancestor) end # Generate a method definition in the helper module for each # helper method defined via the `helper_method` call in the controller. helpers_module.instance_methods(false).each do |method_name| method = if proxied_helper_methods.include?(method_name) helper_method_proxy_target(method_name) else helpers_module.instance_method(method_name) end if method create_method_from_def(helper_methods, method) else create_unknown_proxy_method(helper_methods, method_name) end end end # Create helper proxy class controller.create_class(helper_proxy_name, superclass_name: "::ActionView::Base") do |proxy| proxy.create_include(helper_methods_name) end end end
def gather_constants
def gather_constants descendants_of(::ActionController::Base).select(&:name).select do |klass| klass.const_defined?(:HelperMethods, false) end end
def gather_includes(mod)
def gather_includes(mod) mod.ancestors .reject { |ancestor| ancestor.is_a?(Class) || ancestor == mod || name_of(ancestor).nil? } .map { |ancestor| T.must(qualified_name_of(ancestor)) } .reverse end
def helper_method_proxy_target(method_name)
def helper_method_proxy_target(method_name) # Lookup the proxy target method only if it is defined as a public/protected or private method. if constant.method_defined?(method_name) || constant.private_method_defined?(method_name) constant.instance_method(method_name) end end