-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathrack_app.rb
59 lines (48 loc) · 1.57 KB
/
rack_app.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
# typed: strict
# frozen_string_literal: true
module RubyLsp
module Rails
class RackApp
extend T::Sig
BASE_PATH = "/ruby_lsp_rails/"
sig { params(env: T::Hash[T.untyped, T.untyped]).returns(T::Array[T.untyped]) }
def call(env)
request = ActionDispatch::Request.new(env)
path = request.path
route, argument = path.delete_prefix(BASE_PATH).split("/")
case route
when "activate"
[200, { "Content-Type" => "application/json" }, []]
when "models"
resolve_database_info_from_model(argument)
else
not_found
end
end
private
sig { params(model_name: String).returns(T::Array[T.untyped]) }
def resolve_database_info_from_model(model_name)
const = ActiveSupport::Inflector.safe_constantize(model_name)
if const && const < ActiveRecord::Base
begin
schema_file = ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(const.connection.pool.db_config)
rescue => e
warn("Could not locate schema: #{e.message}")
end
body = JSON.dump({
columns: const.columns.map { |column| [column.name, column.type] },
schema_file: schema_file,
schema_table: const.table_name,
})
[200, { "Content-Type" => "application/json" }, [body]]
else
not_found
end
end
sig { returns(T::Array[T.untyped]) }
def not_found
[404, { "Content-Type" => "text/plain" }, ["Not Found"]]
end
end
end
end