Skip to content

Commit 8bebbd3

Browse files
authored
Merge pull request #90 from Shopify/andyw8/rack-app
Use a Rack app instead of Rails middleware
2 parents c7a3dbe + 2634ffa commit 8bebbd3

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,24 @@ Ruby LSP Rails is a [Ruby LSP](https://github.com/Shopify/ruby-lsp) extension fo
1010
To install, add the following line to your application's Gemfile:
1111

1212
```ruby
13+
# Gemfile
1314
group :development do
1415
gem "ruby-lsp-rails"
1516
end
1617
```
18+
Some features rely on server introspection, and use a Rack server which is automatically mounted by using a Railtie.
19+
20+
For applications with specialized routing requirements, such as custom sharding, this may not be compatible. It can
21+
be disabled with:
22+
23+
```ruby
24+
# config/environments/development.rb
25+
Rails.application.configure do
26+
# ...
27+
config.ruby_lsp_rails.server = false
28+
# ...
29+
end
30+
```
1731

1832
## Usage
1933

@@ -41,7 +55,7 @@ cause the test runner to hang.
4155

4256
This gem consists of two components that enable enhanced Rails functionality in the editor:
4357

44-
1. A Rack middleware that automatically exposes APIs when Rails server is running
58+
1. A Rack app that automatically exposes APIs when Rails server is running
4559
1. A Ruby LSP extension that connects to the exposed APIs to fetch runtime information from the Rails server
4660

4761
This is why the Rails server needs to be running for some features to work.

lib/ruby_lsp_rails/middleware.rb renamed to lib/ruby_lsp_rails/rack_app.rb

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,26 @@
33

44
module RubyLsp
55
module Rails
6-
class Middleware
6+
class RackApp
77
extend T::Sig
88

99
BASE_PATH = "/ruby_lsp_rails/"
1010

11-
sig { params(app: T.untyped).void }
12-
def initialize(app)
13-
@app = app
14-
end
15-
1611
sig { params(env: T::Hash[T.untyped, T.untyped]).returns(T::Array[T.untyped]) }
1712
def call(env)
1813
request = ActionDispatch::Request.new(env)
1914
path = request.path
20-
return @app.call(env) unless path.start_with?(BASE_PATH)
2115

2216
route, argument = path.delete_prefix(BASE_PATH).split("/")
2317

2418
case route
19+
when "activate"
20+
[200, { "Content-Type" => "application/json" }, []]
2521
when "models"
2622
resolve_database_info_from_model(argument)
2723
else
28-
[200, { "Content-Type" => "text/plain" }, []]
24+
not_found
2925
end
30-
rescue
31-
@app.call(env)
3226
end
3327

3428
private

lib/ruby_lsp_rails/railtie.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22
# frozen_string_literal: true
33

44
require "rails/railtie"
5-
require "ruby_lsp_rails/middleware"
5+
require "ruby_lsp_rails/rack_app"
66

77
module RubyLsp
88
module Rails
99
class Railtie < ::Rails::Railtie
10-
initializer "ruby_lsp_rails.setup" do |app|
11-
app.config.middleware.insert_after(ActionDispatch::ShowExceptions, RubyLsp::Rails::Middleware)
10+
config.ruby_lsp_rails = ActiveSupport::OrderedOptions.new
11+
config.ruby_lsp_rails.server = true
1212

13-
config.after_initialize do |_app|
13+
initializer "ruby_lsp_rails.setup" do |_app|
14+
config.after_initialize do |app|
15+
unless config.ruby_lsp_rails.server == false
16+
app.routes.prepend do
17+
T.bind(self, ActionDispatch::Routing::Mapper)
18+
mount(RackApp.new => RackApp::BASE_PATH)
19+
end
20+
end
21+
22+
# If we start the app with `bin/rails console` then `Rails::Server` is not defined.
1423
if defined?(::Rails::Server)
1524
ssl_enable, host, port = ::Rails::Server::Options.new.parse!(ARGV).values_at(:SSLEnable, :Host, :Port)
1625
app_uri = "#{ssl_enable ? "https" : "http"}://#{host}:#{port}"

test/ruby_lsp_rails/middleware_test.rb renamed to test/ruby_lsp_rails/rack_app_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
module RubyLsp
77
module Rails
8-
class ModelsControllerTest < ActionDispatch::IntegrationTest
8+
class RackAppTest < ActionDispatch::IntegrationTest
99
test "GET show returns column information for existing models" do
1010
get "/ruby_lsp_rails/models/User"
1111
assert_response(:success)

0 commit comments

Comments
 (0)