File tree Expand file tree Collapse file tree 3 files changed +48
-15
lines changed
lib/ruby_lsp/ruby_lsp_rails Expand file tree Collapse file tree 3 files changed +48
-15
lines changed Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ class Server
26
26
27
27
extend T ::Sig
28
28
29
- sig { params ( model_name : String ) . returns ( T . nilable ( T ::Hash [ Symbol , T . untyped ] ) ) }
29
+ sig { params ( model_name : String ) . returns ( T ::Hash [ Symbol , T . untyped ] ) }
30
30
def resolve_database_info_from_model ( model_name )
31
31
const = ActiveSupport ::Inflector . safe_constantize ( model_name )
32
32
unless const && defined? ( ActiveRecord ) && const < ActiveRecord ::Base && !const . abstract_class?
@@ -35,14 +35,18 @@ def resolve_database_info_from_model(model_name)
35
35
}
36
36
end
37
37
38
- schema_file = ActiveRecord ::Tasks ::DatabaseTasks . schema_dump_path ( const . connection . pool . db_config )
39
-
40
- {
38
+ info = {
41
39
result : {
42
40
columns : const . columns . map { |column | [ column . name , column . type ] } ,
43
- schema_file : ::Rails . root + schema_file ,
44
41
} ,
45
42
}
43
+
44
+ if ActiveRecord ::Tasks ::DatabaseTasks . respond_to? ( :schema_dump_path )
45
+ info [ :result ] [ :schema_file ] =
46
+ ActiveRecord ::Tasks ::DatabaseTasks . schema_dump_path ( const . connection . pool . db_config )
47
+
48
+ end
49
+ info
46
50
rescue => e
47
51
{
48
52
error : e . message ,
Original file line number Diff line number Diff line change @@ -16,6 +16,8 @@ class RunnerClientTest < ActiveSupport::TestCase
16
16
assert_predicate @client , :stopped?
17
17
end
18
18
19
+ # These are integration tests which start the server. For the more fine-grained tests, see `server_test.rb`.
20
+
19
21
test "#model returns information for the requested model" do
20
22
# These columns are from the schema in the dummy app: test/dummy/db/schema.rb
21
23
columns = [
@@ -31,16 +33,8 @@ class RunnerClientTest < ActiveSupport::TestCase
31
33
assert_match ( %r{db/schema\. rb$} , response . fetch ( :schema_file ) )
32
34
end
33
35
34
- test "returns nil if model doesn't exist" do
35
- assert_nil @client . model ( "Foo" )
36
- end
37
-
38
- test "returns nil if class is not a model" do
39
- assert_nil @client . model ( "Time" )
40
- end
41
-
42
- test "returns nil if class is an abstract model" do
43
- assert_nil @client . model ( "ApplicationRecord" )
36
+ test "returns nil if the request returns a nil response" do
37
+ assert_nil @client . model ( "ApplicationRecord" ) # ApplicationRecord is abstract
44
38
end
45
39
end
46
40
end
Original file line number Diff line number Diff line change
1
+ # typed: true
2
+ # frozen_string_literal: true
3
+
4
+ require "test_helper"
5
+ require "ruby_lsp/ruby_lsp_rails/server"
6
+
7
+ class ServerTest < ActiveSupport ::TestCase
8
+ setup do
9
+ @server = RubyLsp ::Rails ::Server . new
10
+ end
11
+
12
+ test "returns nil if model doesn't exist" do
13
+ response = @server . resolve_database_info_from_model ( "Foo" )
14
+ assert_nil ( response . fetch ( :result ) )
15
+ end
16
+
17
+ test "returns nil if class is not a model" do
18
+ response = @server . resolve_database_info_from_model ( "Time" )
19
+ assert_nil ( response . fetch ( :result ) )
20
+ end
21
+
22
+ test "returns nil if class is an abstract model" do
23
+ response = @server . resolve_database_info_from_model ( "ApplicationRecord" )
24
+ assert_nil ( response . fetch ( :result ) )
25
+ end
26
+
27
+ test "handles older Rails version which don't have `schema_dump_path`" do
28
+ ActiveRecord ::Tasks ::DatabaseTasks . send ( :alias_method , :old_schema_dump_path , :schema_dump_path )
29
+ ActiveRecord ::Tasks ::DatabaseTasks . undef_method ( :schema_dump_path )
30
+ response = @server . resolve_database_info_from_model ( "User" )
31
+ assert_nil ( response . fetch ( :result ) [ :schema_file ] )
32
+ ensure
33
+ ActiveRecord ::Tasks ::DatabaseTasks . send ( :alias_method , :schema_dump_path , :old_schema_dump_path )
34
+ end
35
+ end
You can’t perform that action at this time.
0 commit comments