Skip to content

Commit

Permalink
Clear the model's association cache before resolving its association
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Aug 16, 2024
1 parent 24b30be commit 3c4e9cf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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
association_klass = const.reflect_on_association(params[:association_name].intern).klass

source_location = Object.const_source_location(association_klass.to_s)
Expand Down
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

0 comments on commit 3c4e9cf

Please sign in to comment.