From 9be01d0c201dbdb9b6c743a271527365f8543089 Mon Sep 17 00:00:00 2001 From: Andy Waite Date: Thu, 13 Jun 2024 16:03:57 -0400 Subject: [PATCH] Prevent invalid ActiveRecord check against constant (#401) --- lib/ruby_lsp/ruby_lsp_rails/server.rb | 1 + test/ruby_lsp_rails/server_test.rb | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/lib/ruby_lsp/ruby_lsp_rails/server.rb b/lib/ruby_lsp/ruby_lsp_rails/server.rb index 2c9bdd13..5467d030 100644 --- a/lib/ruby_lsp/ruby_lsp_rails/server.rb +++ b/lib/ruby_lsp/ruby_lsp_rails/server.rb @@ -143,6 +143,7 @@ def active_record_model?(const) !!( const && defined?(ActiveRecord) && + const.is_a?(Class) && ActiveRecord::Base > const && # We do this 'backwards' in case the class overwrites `<` !const.abstract_class? ) diff --git a/test/ruby_lsp_rails/server_test.rb b/test/ruby_lsp_rails/server_test.rb index feb399f8..9d85fb39 100644 --- a/test/ruby_lsp_rails/server_test.rb +++ b/test/ruby_lsp_rails/server_test.rb @@ -24,6 +24,11 @@ class ServerTest < ActiveSupport::TestCase assert_nil(response.fetch(:result)) end + test "returns nil if constant is not a class" do + response = @server.execute("model", { name: "RUBY_VERSION" }) + assert_nil(response.fetch(:result)) + end + test "doesn't fail if the class overrides `<`" do class TestClassWithOverwrittenLessThan class << self