Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear the model's association cache before resolving its association #435

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ commands:
optional: args...
open:
app: http://localhost:3000

env:
'RUBY_DEBUG_IRB_CONSOLE': '1'
2 changes: 2 additions & 0 deletions lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ def resolve_association_target(params)
}
end

# Clear the cache so that we get the latest associations.
const.clear_reflections_cache
Copy link
Member

Choose a reason for hiding this comment

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

This approach would mean that we essentially never have reflections cached. Do we know how expensive that could be?

Would it be worth trying to only clear the cache based on file modifications, similar to how we handle reloading on schema changes?

association_klass = const.reflect_on_association(params[:association_name].intern).klass

source_location = Object.const_source_location(association_klass.to_s)
Expand Down
14 changes: 14 additions & 0 deletions sorbet/rbi/shims/user.rbi
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# typed: strict
# frozen_string_literal: true

class User
class << self
sig { params(association_name: Symbol).void }
def has_many(association_name)
end

sig { returns(T::Hash[String, T.untyped]) }
def reflections
end
end
end
20 changes: 20 additions & 0 deletions test/ruby_lsp_rails/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ def <(other)
assert_nil(response.fetch(:result))
end

test "resolve association reflect the latest associations" do
response = @server.execute(
"association_target_location",
{ model_name: "User", association_name: :memberships },
)
assert_nil(response.fetch(:result))

User.has_many(:memberships)

response = @server.execute(
"association_target_location",
{ model_name: "User", association_name: :memberships },
)

location = response[:result][:location]
assert_match(%r{test/dummy/app/models/membership.rb:3$}, location)
ensure
User.reflections.delete("memberships")
end

test "resolve association handles class_name option" do
response = @server.execute(
"association_target_location",
Expand Down
Loading