Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/ruby_lsp/ruby_lsp_rails/hover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def handle_association(node)

return unless result

generate_column_content(result[:name])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this as is here works, but it means that any hover on associations needs two round trips to the server to pull information.

Instead of doing that, could we include the required information as part of the association_target response and then extract what's needed to generate the hover?

That way we get all of the data in a single request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thats a good point! I'll look into that and let you know, thank you

generate_hover(result[:name])
end

Expand Down
87 changes: 79 additions & 8 deletions test/ruby_lsp_rails/hover_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,22 @@ class User < ApplicationRecord
end

test "returns has_many association information" do
expected_response = {
association_response = {
location: "#{dummy_root}/app/models/membership.rb:5",
name: "Bar",
}
RunnerClient.any_instance.stubs(association_target: expected_response)
model_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer", nil, false],
["foo_id", "integer", nil, false],
["name", "string", nil, true],
],
primary_keys: ["id"],
foreign_keys: ["foo_id"],
indexes: [],
}
RunnerClient.any_instance.stubs(association_target: association_response, model: model_response)

response = hover_on_source(<<~RUBY, { line: 1, character: 11 })
class Foo < ApplicationRecord
Expand All @@ -241,15 +252,34 @@ class Bar < ApplicationRecord
```

**Definitions**: [fake.rb](file:///fake.rb#L5,1-7,4)

[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})

### Columns
- **id**: integer (PK)

- **foo_id**: integer (FK) - not null

- **name**: string
CONTENT
end

test "returns belongs_to association information" do
expected_response = {
association_response = {
location: "#{dummy_root}/app/models/membership.rb:1",
name: "Foo",
}
RunnerClient.any_instance.stubs(association_target: expected_response)
model_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer", nil, false],
["name", "string", nil, true],
],
primary_keys: ["id"],
foreign_keys: [],
indexes: [],
}
RunnerClient.any_instance.stubs(association_target: association_response, model: model_response)

response = hover_on_source(<<~RUBY, { line: 5, character: 14 })
class Foo < ApplicationRecord
Expand All @@ -267,15 +297,32 @@ class Bar < ApplicationRecord
```

**Definitions**: [fake.rb](file:///fake.rb#L1,1-3,4)

[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})

### Columns
- **id**: integer (PK)

- **name**: string
CONTENT
end

test "returns has_one association information" do
expected_response = {
association_response = {
location: "#{dummy_root}/app/models/membership.rb:5",
name: "Bar",
}
RunnerClient.any_instance.stubs(association_target: expected_response)
model_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer", nil, false],
["foo_id", "integer", nil, false],
],
primary_keys: ["id"],
foreign_keys: ["foo_id"],
indexes: [],
}
RunnerClient.any_instance.stubs(association_target: association_response, model: model_response)

response = hover_on_source(<<~RUBY, { line: 1, character: 10 })
class Foo < ApplicationRecord
Expand All @@ -292,15 +339,32 @@ class Bar < ApplicationRecord
```

**Definitions**: [fake.rb](file:///fake.rb#L5,1-6,4)

[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})

### Columns
- **id**: integer (PK)

- **foo_id**: integer (FK) - not null
CONTENT
end

test "returns has_and_belongs_to association information" do
expected_response = {
association_response = {
location: "#{dummy_root}/app/models/membership.rb:5",
name: "Bar",
}
RunnerClient.any_instance.stubs(association_target: expected_response)
model_response = {
schema_file: "#{dummy_root}/db/schema.rb",
columns: [
["id", "integer", nil, false],
["name", "string", nil, true],
],
primary_keys: ["id"],
foreign_keys: [],
indexes: [],
}
RunnerClient.any_instance.stubs(association_target: association_response, model: model_response)

response = hover_on_source(<<~RUBY, { line: 1, character: 26 })
class Foo < ApplicationRecord
Expand All @@ -318,6 +382,13 @@ class Bar < ApplicationRecord
```

**Definitions**: [fake.rb](file:///fake.rb#L5,1-7,4)

[Schema](#{URI::Generic.from_path(path: dummy_root + "/db/schema.rb")})

### Columns
- **id**: integer (PK)

- **name**: string
CONTENT
end

Expand Down
Loading