Class: Tapioca::Compilers::Dsl::ActionControllerHelpers

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

Overview

‘Tapioca::Compilers::Dsl::ActionControllerHelpers` decorates RBI files for all subclasses of [`ActionController::Base`](api.rubyonrails.org/classes/ActionController/Helpers.html).

For example, with the following ‘MyHelper` module:

~~~rb module MyHelper

 def greet(user)
   # ...
 end

def localized_time
   # ...
 end

end ~~~

and the following controller:

~~~rb class UserController < ActionController::Base

helper MyHelper
helper { def age(user) "99" end }
helper_method :current_user_name

def current_user_name
  # ...
end

end ~~~

this generator will produce an RBI file ‘user_controller.rbi` with the following content:

~~~rbi # user_controller.rbi # typed: strong class UserController

module HelperMethods
  include MyHelper

  sig { params(user: T.untyped).returns(T.untyped) }
  def age(user); end

  sig { returns(T.untyped) }
  def current_user_name; end
end

class HelperProxy < ::ActionView::Base
  include HelperMethods
end

sig { returns(HelperProxy) }
def helpers; end

end ~~~

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/tapioca/compilers/dsl/action_controller_helpers.rb', line 78

def decorate(root, constant)
  helper_proxy_name = "HelperProxy"
  helper_methods_name = "HelperMethods"
  proxied_helper_methods = constant._helper_methods.map(&:to_s).map(&:to_sym)

  # Define the helpers method
  root.path(constant) do |controller|
    create_method(controller, 'helpers', return_type: helper_proxy_name)

    # Create helper method module
    controller.create_module(helper_methods_name) do |helper_methods|
      helpers_module = constant._helpers

      gather_includes(helpers_module).each do |ancestor|
        helper_methods.create_include(ancestor)
      end

      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

    # Create helper proxy class
    controller.create_class(helper_proxy_name, superclass: "::ActionView::Base") do |proxy|
      proxy.create_include(helper_methods_name)
    end
  end
end

#gather_constantsObject



113
114
115
# File 'lib/tapioca/compilers/dsl/action_controller_helpers.rb', line 113

def gather_constants
  ::ActionController::Base.descendants.reject(&:abstract?).select(&:name)
end