Skip to content

Handle apps without ActiveRecord #263

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

Merged
merged 1 commit into from
Feb 20, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/ruby_lsp/ruby_lsp_rails/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Server
sig { params(model_name: String).returns(T.nilable(T::Hash[Symbol, T.untyped])) }
def resolve_database_info_from_model(model_name)
const = ActiveSupport::Inflector.safe_constantize(model_name)
unless const && const < ActiveRecord::Base && !const.abstract_class?
unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class?
Copy link
Member

Choose a reason for hiding this comment

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

We might be able to support other backends (like Mongoid) if we shift this to check for features rather than inheritance. Would something like this work?

Suggested change
unless const && defined?(ActiveRecord) && const < ActiveRecord::Base && !const.abstract_class?
unless const && const.respond_to?(:columns) && !const.abstract_class?

Copy link
Contributor Author

@andyw8 andyw8 Feb 20, 2024

Choose a reason for hiding this comment

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

It looks like we can't assume the existence of those methods:

irb(main):003* class Post
irb(main):004*   include Mongoid::Document
irb(main):005*   field :title, type: String
irb(main):006*   field :body, type: String
irb(main):007*   has_many :comments
irb(main):008> end
=> 
#<Mongoid::Association::Referenced::HasMany:0x0000000116e39c18
 @extension=nil,
 @module_path="",
 @name=:comments,
 @options={},
 @owner_class=Post,
 @polymorphic=false,
 @validate=true>
irb(main):009> Post.columns
(irb):9:in `<main>': undefined method `columns' for Post:Class (NoMethodError)

Post.columns
    ^^^^^^^^
irb(main):010> Post.abstract_class?
(irb):10:in `<main>': undefined method `abstract_class?' for Post:Class (NoMethodError)

Post.abstract_class?
    ^^^^^^^^^^^^^^^^

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The closest match would be fields:

irb(main):011> Post.fields
=> 
{"_id"=>
  #<Mongoid::Fields::Standard:0x000000011783f820
   @default_name="___id_default__",
   @default_val=#<Proc:0x0000000116fbe160 /Users/andyw8/.gem/ruby/3.2.1/gems/mongoid-8.1.4/lib/mongoid/fields.rb:137 (lambda)>,
   @label=nil,
   @name="_id",
   @options=
    {:default=>#<Proc:0x0000000116fbe160 /Users/andyw8/.gem/ruby/3.2.1/gems/mongoid-8.1.4/lib/mongoid/fields.rb:137 (lambda)>,
     :pre_processed=>true,
     :type=>BSON::ObjectId,
     :klass=>Post},
   @pre_processed=true>,
 "title"=>#<Mongoid::Fields::Standard:0x0000000117838fc0 @default_val=nil, @label=nil, @name="title", @options={:type=>String, :klass=>Post}>,
 "body"=>#<Mongoid::Fields::Standard:0x00000001178387a0 @default_val=nil, @label=nil, @name="body", @options={:type=>String, :klass=>Post}>}

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's too bad. I thought there'd be a common interface for these. Thanks for looking into it.

return {
result: nil,
}
Expand Down