-
Notifications
You must be signed in to change notification settings - Fork 111
/
action_mailer.rb
75 lines (67 loc) · 2.03 KB
/
action_mailer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# typed: strict
# frozen_string_literal: true
begin
require "action_mailer"
rescue LoadError
return
end
module Tapioca
module Dsl
module Compilers
# `Tapioca::Dsl::Compilers::ActionMailer` generates RBI files for subclasses of
# [`ActionMailer::Base`](https://api.rubyonrails.org/classes/ActionMailer/Base.html).
#
# For example, with the following `ActionMailer` subclass:
#
# ~~~rb
# class NotifierMailer < ActionMailer::Base
# def notify_customer(customer_id)
# # ...
# end
# end
# ~~~
#
# this compiler will produce the RBI file `notifier_mailer.rbi` with the following content:
#
# ~~~rbi
# # notifier_mailer.rbi
# # typed: true
# class NotifierMailer
# sig { params(customer_id: T.untyped).returns(::ActionMailer::MessageDelivery) }
# def self.notify_customer(customer_id); end
# end
# ~~~
class ActionMailer < Compiler
extend T::Sig
ConstantType = type_member { { fixed: T.class_of(::ActionMailer::Base) } }
sig { override.void }
def decorate
root.create_path(constant) do |mailer|
action_methods_for_constant.each do |mailer_method|
method_def = constant.instance_method(mailer_method)
parameters = compile_method_parameters_to_rbi(method_def)
mailer.create_method(
mailer_method,
parameters: parameters,
return_type: "::ActionMailer::MessageDelivery",
class_method: true,
)
end
end
end
class << self
extend T::Sig
sig { override.returns(T::Enumerable[Module]) }
def gather_constants
descendants_of(::ActionMailer::Base).reject(&:abstract?)
end
end
private
sig { returns(T::Array[String]) }
def action_methods_for_constant
constant.action_methods.to_a
end
end
end
end
end