From f1eb014ff622c493f0cfdd56de7a762b4adc4d44 Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Fri, 16 Aug 2024 20:34:52 +0100 Subject: [PATCH] Clear the model's association cache before resolving its association --- lib/ruby_lsp/ruby_lsp_rails/server.rb | 2 ++ sorbet/rbi/shims/user.rbi | 14 ++++++++++++++ test/ruby_lsp_rails/server_test.rb | 20 ++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 sorbet/rbi/shims/user.rbi diff --git a/lib/ruby_lsp/ruby_lsp_rails/server.rb b/lib/ruby_lsp/ruby_lsp_rails/server.rb index 9ce05c0f..b002d7a2 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/server.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/server.rb @@ -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) diff --git a/sorbet/rbi/shims/user.rbi b/sorbet/rbi/shims/user.rbi new file mode 100644 index 00000000..3303f42e --- /dev/null +++ b/sorbet/rbi/shims/user.rbi @@ -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 diff --git a/test/ruby_lsp_rails/server_test.rb b/test/ruby_lsp_rails/server_test.rb index b3bcf7d9..478a57d0 100644 --- a/test/ruby_lsp_rails/server_test.rb +++ b/test/ruby_lsp_rails/server_test.rb @@ -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",