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 29, 2024
1 parent 62ce47d commit c56547e
Show file tree
Hide file tree
Showing 3 changed files with 36 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
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

0 comments on commit c56547e

Please sign in to comment.