Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Orthoses::ActionMailer::Base middleware #17

Merged
merged 3 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
GIT
remote: https://github.com/ruby/rake.git
revision: b17e0a14d6a24022c92bed22fbdc7924f85478b0
revision: 191a9d770caef530f68fbcf1f6d090e8872d31cb
specs:
rake (13.0.6)

Expand Down Expand Up @@ -89,7 +89,7 @@ GEM
irb (>= 1.5.0)
reline (>= 0.3.1)
erubi (1.12.0)
ffi (1.15.5)
ffi (1.16.2)
fileutils (1.7.1)
globalid (1.2.1)
activesupport (>= 6.1)
Expand Down Expand Up @@ -174,7 +174,7 @@ GEM
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rbs (3.2.1)
rbs (3.2.2)
rbtree (0.4.6)
rdoc (6.5.0)
psych (>= 4.0.0)
Expand Down Expand Up @@ -209,11 +209,11 @@ GEM
timeout (0.4.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
unicode-display_width (2.5.0)
websocket-driver (0.7.6)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
zeitwerk (2.6.11)
zeitwerk (2.6.12)

PLATFORMS
ruby
Expand Down
5 changes: 1 addition & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ require "rgot/cli"
task :test do
require 'orthoses/rails'

# Create cache for performance
Orthoses::Utils.rbs_environment(collection: true)

exit Rgot::Cli.new(%w[-v --require active_record --require active_support/all lib]).run
exit Rgot::Cli.new(%w[-v --require active_record --require active_support/all --require action_mailer lib]).run
end

task default: :test
3 changes: 3 additions & 0 deletions lib/orthoses/action_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative 'action_mailer/base'
85 changes: 85 additions & 0 deletions lib/orthoses/action_mailer/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# frozen_string_literal: true

module Orthoses
module ActionMailer
class Base
def initialize(loader)
@loader = loader
end

def call
@loader.call.tap do |store|
::ActionMailer::Base.descendants.each do |mailer_class|
base_name = Utils.module_name(mailer_class) or next
content = store[base_name]
mailer_class.action_methods.each do |action_method|
method_object = mailer_class.instance_method(action_method)
parameters = method_parameters_to_rbs_method_type(method_object).to_s.gsub(' -> untyped', '')
content << "def self.#{action_method}: #{parameters} -> ::ActionMailer::MessageDelivery"
end
end
end
end

private

def method_parameters_to_rbs_method_type(method_object)
required_positionals = []
optional_positionals = []
rest_positionals = nil
trailing_positionals = []
required_keywords = {}
optional_keywords = {}
rest_keywords = nil
requireds = required_positionals
block = nil
untyped = RBS::Types::Bases::Any.new(location: nil)

method_object.parameters.each do |kind, name|
case kind
when :req
requireds << ::RBS::Types::Function::Param.new(name: name, type: untyped)
when :opt
requireds = trailing_positionals
optional_positionals << ::RBS::Types::Function::Param.new(name: name, type: untyped)
when :rest
requireds = trailing_positionals
name = nil if name == :*
rest_positionals = ::RBS::Types::Function::Param.new(name: name, type: untyped)
when :keyreq
required_keywords[name] = ::RBS::Types::Function::Param.new(name: nil, type: untyped)
when :key
optional_keywords[name] = ::RBS::Types::Function::Param.new(name: nil, type: untyped)
when :keyrest
rest_keywords = ::RBS::Types::Function::Param.new(name: name, type: untyped)
when :block
block = RBS::Types::Block.new(
type: RBS::Types::Function.empty(untyped).update(rest_positionals: RBS::Types::Function::Param.new(name: nil, type: untyped)),
required: true,
self_type: nil
)
else
raise "bug"
end
end

function = ::RBS::Types::Function.new(
required_positionals: required_positionals,
optional_positionals: optional_positionals,
rest_positionals: rest_positionals,
trailing_positionals: trailing_positionals,
required_keywords: required_keywords,
optional_keywords: optional_keywords,
rest_keywords: rest_keywords,
return_type: untyped,
)
::RBS::MethodType.new(
location: nil,
type_params: [],
type: function,
block: block
)
end
end
end
end
32 changes: 32 additions & 0 deletions lib/orthoses/action_mailer/base_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
begin
require 'test_helper'
rescue LoadError
end

module BaseTest
LOADER = ->{
class UserMailer < ::ActionMailer::Base
def foo(a, b = nil, *c, d:, e: nil, **f)
end

private

def bar
end
end
}
def test_action_methods(t)
store = Orthoses::ActionMailer::Base.new(
Orthoses::Store.new(LOADER)
).call
expect = <<~RBS
class BaseTest::UserMailer < ::ActionMailer::Base
def self.foo: (untyped a, ?untyped b, *untyped c, d: untyped, ?e: untyped, **untyped f) -> ::ActionMailer::MessageDelivery
end
RBS
actual = store['BaseTest::UserMailer'].to_rbs
unless expect == actual
t.error("expect=\n```rbs\n#{expect}```\n, but got \n```rbs\n#{actual}```\n")
end
end
end
1 change: 1 addition & 0 deletions lib/orthoses/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require 'orthoses'

require_relative 'action_mailer'
require_relative 'active_model'
require_relative 'active_record'
require_relative 'active_storage'
Expand Down
2 changes: 2 additions & 0 deletions lib/orthoses/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def initialize(loader)
def call
loader = @loader
Orthoses::Builder.new do
use Orthoses::ActionMailer::Base

use Orthoses::ActiveModel::Attributes
use Orthoses::ActiveModel::HasSecurePassword

Expand Down
14 changes: 7 additions & 7 deletions rbs_collection.lock.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
sources:
- type: git
name: ruby/gem_rbs_collection
revision: 267dd270bb5aabcc1e21c87f44360f0680a8501c
revision: c69c46f012a6a0263006eef0f84ca59ac0c5043e
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
path: ".gem_rbs_collection"
Expand All @@ -20,7 +20,7 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: 267dd270bb5aabcc1e21c87f44360f0680a8501c
revision: c69c46f012a6a0263006eef0f84ca59ac0c5043e
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: benchmark
Expand Down Expand Up @@ -88,7 +88,7 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: 267dd270bb5aabcc1e21c87f44360f0680a8501c
revision: c69c46f012a6a0263006eef0f84ca59ac0c5043e
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: io-console
Expand All @@ -112,7 +112,7 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: 267dd270bb5aabcc1e21c87f44360f0680a8501c
revision: c69c46f012a6a0263006eef0f84ca59ac0c5043e
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: minitest
Expand Down Expand Up @@ -140,7 +140,7 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: 267dd270bb5aabcc1e21c87f44360f0680a8501c
revision: c69c46f012a6a0263006eef0f84ca59ac0c5043e
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: objspace
Expand Down Expand Up @@ -180,11 +180,11 @@ gems:
source:
type: git
name: ruby/gem_rbs_collection
revision: 267dd270bb5aabcc1e21c87f44360f0680a8501c
revision: c69c46f012a6a0263006eef0f84ca59ac0c5043e
remote: https://github.com/ruby/gem_rbs_collection.git
repo_dir: gems
- name: rbs
version: 3.2.1
version: 3.2.2
source:
type: rubygems
- name: rdoc
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'orthoses-rails'
require 'rgot/cli'
require 'action_mailer'
require 'active_record'
require 'active_support/all'

Expand Down