Skip to content

Commit 640be1d

Browse files
committed
Generate types for path helpers when routes.rb is created or changed
1 parent 41d3a85 commit 640be1d

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/ruby_lsp/rbs_rails/addon.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ def generate_signature(uri) #: void
101101
case path.to_s
102102
when "db/schema.rb"
103103
generate_all_model_signatures
104+
when %r{^config/(routes\.rb|routes/.*\.rb)$}
105+
generate_path_helpers_signature
104106
else
105107
klass = constantize(path)
106108
return unless klass
@@ -118,6 +120,15 @@ def generate_all_model_signatures #: void
118120
end
119121
end
120122

123+
def generate_path_helpers_signature #: void
124+
rbs_path = config.signature_root_dir / "path_helpers.rbs"
125+
rbs_path.dirname.mkpath
126+
127+
sig = ::RbsRails::PathHelpers.generate
128+
rbs_path.write sig
129+
logger.info("Updated RBS signature: #{rbs_path}")
130+
end
131+
121132
# @rbs klass: Class
122133
def generate_signature0(klass) #: void
123134
return unless klass < ::ActiveRecord::Base

sig/ruby_lsp/rbs_rails/addon.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ module RubyLsp
4444

4545
def generate_all_model_signatures: () -> void
4646

47+
def generate_path_helpers_signature: () -> void
48+
4749
# @rbs klass: Class
4850
def generate_signature0: (Class klass) -> void
4951

spec/rbs_rails/addon_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,32 @@
116116
end
117117
end
118118

119+
context "when config/routes.rb is created" do
120+
let(:filename) { "config/routes.rb" }
121+
let(:rbs_path) { Pathname.new("#{workspace_path}/sig/rbs_rails/path_helpers.rbs") }
122+
123+
it "generates path_helpers.rbs" do
124+
subject
125+
126+
expect(rbs_path).to exist
127+
content = rbs_path.read
128+
expect(content).to include("interface ::_RbsRailsPathHelpers")
129+
end
130+
end
131+
132+
context "when config/routes/*.rb is created" do
133+
let(:filename) { "config/routes/api.rb" }
134+
let(:rbs_path) { Pathname.new("#{workspace_path}/sig/rbs_rails/path_helpers.rbs") }
135+
136+
it "generates path_helpers.rbs" do
137+
subject
138+
139+
expect(rbs_path).to exist
140+
content = rbs_path.read
141+
expect(content).to include("interface ::_RbsRailsPathHelpers")
142+
end
143+
end
144+
119145
context "when any other file is created" do
120146
let(:filename) { "app/controllers/users_controller.rb" }
121147

0 commit comments

Comments
 (0)