lib/tapioca/compilers/dsl/action_controller_helpers.rb in tapioca-0.4.10 vs lib/tapioca/compilers/dsl/action_controller_helpers.rb in tapioca-0.4.11

- old
+ new

@@ -11,12 +11,11 @@ module Tapioca module Compilers module Dsl # `Tapioca::Compilers::Dsl::ActionControllerHelpers` decorates RBI files for all - # subclasses of `::ActionController::Base` - # to add helper methods (see https://api.rubyonrails.org/classes/ActionController/Helpers.html). + # subclasses of [`ActionController::Base`](https://api.rubyonrails.org/classes/ActionController/Helpers.html). # # For example, with the following `MyHelper` module: # # ~~~rb # module MyHelper @@ -48,26 +47,26 @@ # # ~~~rbi # # user_controller.rbi # # typed: strong # class UserController - # sig { returns(UserController::HelperProxy) } - # def helpers; end - # end + # module HelperMethods + # include MyHelper # - # module UserController::HelperMethods - # include MyHelper + # sig { params(user: T.untyped).returns(T.untyped) } + # def age(user); end # - # sig { params(user: T.untyped).returns(T.untyped) } - # def age(user); end + # sig { returns(T.untyped) } + # def current_user_name; end + # end # - # sig { returns(T.untyped) } - # def current_user_name; end - # end + # class HelperProxy < ::ActionView::Base + # include HelperMethods + # end # - # class UserController::HelperProxy < ::ActionView::Base - # include UserController::HelperMethods + # sig { returns(HelperProxy) } + # def helpers; end # end # ~~~ class ActionControllerHelpers < Base extend T::Sig @@ -75,54 +74,54 @@ override .params(root: Parlour::RbiGenerator::Namespace, constant: T.class_of(::ActionController::Base)) .void end def decorate(root, constant) - helper_proxy_name = "#{constant}::HelperProxy" - helper_methods_name = "#{constant}::HelperMethods" + helper_proxy_name = "HelperProxy" + helper_methods_name = "HelperMethods" proxied_helper_methods = constant._helper_methods.map(&:to_s).map(&:to_sym) - # Create helper method module - root.create_module(helper_methods_name) do |helper_methods| - helpers_module = constant._helpers + # Define the helpers method + root.path(constant) do |controller| + create_method(controller, 'helpers', return_type: helper_proxy_name) - gather_includes(helpers_module).each do |ancestor| - helper_methods.create_include(ancestor) - end + # Create helper method module + controller.create_module(helper_methods_name) do |helper_methods| + helpers_module = constant._helpers - helpers_module.instance_methods(false).each do |method_name| - method = if proxied_helper_methods.include?(method_name) - constant.instance_method(method_name) - else - helpers_module.instance_method(method_name) + gather_includes(helpers_module).each do |ancestor| + helper_methods.create_include(ancestor) end - create_method_from_def(helper_methods, method) + + helpers_module.instance_methods(false).each do |method_name| + method = if proxied_helper_methods.include?(method_name) + constant.instance_method(method_name) + else + helpers_module.instance_method(method_name) + end + create_method_from_def(helper_methods, method) + end end - end - # Create helper proxy class - root.create_class(helper_proxy_name, superclass: "::ActionView::Base") do |proxy| - proxy.create_include(helper_methods_name) + # Create helper proxy class + controller.create_class(helper_proxy_name, superclass: "::ActionView::Base") do |proxy| + proxy.create_include(helper_methods_name) + end end - - # Define the helpers method - root.path(constant) do |controller| - create_method(controller, 'helpers', return_type: helper_proxy_name) - end end sig { override.returns(T::Enumerable[Module]) } def gather_constants - ::ActionController::Base.descendants.reject(&:abstract?) + ::ActionController::Base.descendants.reject(&:abstract?).select(&:name) end private sig { params(mod: Module).returns(T::Array[String]) } def gather_includes(mod) mod.ancestors .reject { |ancestor| ancestor.is_a?(Class) || ancestor == mod || ancestor.name.nil? } - .map { |ancestor| T.must(ancestor.name) } + .map { |ancestor| "::#{ancestor.name}" } .reverse end end end end