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 to add helper methods (see 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

sig { returns(UserController::HelperProxy) }
def helpers; end

end

module UserController::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 UserController::HelperProxy < ::ActionView::Base

include UserController::HelperMethods

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



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
111
# File 'lib/tapioca/compilers/dsl/action_controller_helpers.rb', line 79

def decorate(root, constant)
  helper_proxy_name = "#{constant}::HelperProxy"
  helper_methods_name = "#{constant}::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

    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
  root.create_class(helper_proxy_name, superclass: "::ActionView::Base") do |proxy|
    proxy.create_include(helper_methods_name)
  end

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

#gather_constantsObject



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

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