iftheshoefritz / solargraph-rails

Solargraph plugin to add awareness of Rails-specific code
MIT License
309 stars 30 forks source link

Definitions are not loaded according to inheritance chain #53

Open ali-p-q opened 1 year ago

ali-p-q commented 1 year ago

Describe the bug Modules from ActiveModel that are automagically included by Rails in classes that inherit from ApplicationRecord (which, in turn, inherits from ActiveRecord::Base) are not detected by solargraph-rails, unless included explicitly in the definitions file for all classes in the inheritance chain. Solargraph is not able to follow the inheritance chain otherwise and the modules will not get loaded (no definitions, no autocomplete).

To Reproduce Steps to reproduce the behavior:

  1. Go to 'definitions.rb' (or whatever the name you gave this file, which should be in the config folder)
  2. Add the line include ActiveModel::SecurePassword::ClassMethods under class ActiveRecord::Base in the definitions.rb file
  3. Open a Rails project model class (let's say User) that inherits from ApplicationRecord (which, in turn, inherits from ActiveRecord::Base)

Expected behavior From inside the User class (or any other class that inherits from ApplicationRecord) one should be able to have autocomplete and docs for the method has_secure_password

Actual behavior No definitions or docs are loaded.

Workaround Definitions are loaded only if the included module (and all other extended/included classes/modules) are explicitly included for all classes in the inheritance chain. In the given example, the line include ActiveModel::SecurePassword::ClassMethods must be added under class User and under class ApplicationRecord, as well as under class ActiveRecord::Base, in which case the autocomplete and docs for the method has_secure_password will show in the User class.

Edit: It also works if include ActiveModel::Model is added to both User and ApplicationRecord, in which case one needs only add all other modules from ActiveModel to ActiveRecord::Base in order to load them automagically in your class. See screenshot below.

Screenshots include ActiveModel::SecurePassword::ClassMethods added under class ActiveRecord::Base image

has_secure_password is not loaded in the User class image

Duplicate all entries for all classes involved in the inheritance chain image

has_secure_password is loaded in the User class image

Alternative solution, adding include ActiveModel::Model to both User and ApplicationRecord and adding all modules from ActiveModel individually to ActiveRecord::Base. image

Debug log Output of debug log below.

ruby -r'solargraph-rails' -e 'Solargraph::Rails::Debug.run()'

[INFO] Indexing workspace files in ./ [DEBUG] [Rails][Schema] added ["name", "email", "created_at", "updated_at", "password_digest", "remember_digest"] to User [DEBUG] [Rails][RailsApi] added ["ActiveRecord::ConnectionAdapters::SchemaStatements", "ActiveRecord::ConnectionAdapters::SchemaStatements"] to CreateUsers [DEBUG] [Rails][RailsApi] added ["ActiveRecord::ConnectionAdapters::SchemaStatements", "ActiveRecord::ConnectionAdapters::SchemaStatements"] to AddIndexToUsersEmail [DEBUG] [Rails][RailsApi] added ["ActiveRecord::ConnectionAdapters::SchemaStatements", "ActiveRecord::ConnectionAdapters::SchemaStatements"] to AddPasswordDigestToUsers [DEBUG] [Rails][RailsApi] added ["ActiveRecord::ConnectionAdapters::SchemaStatements", "ActiveRecord::ConnectionAdapters::SchemaStatements"] to AddRememberDigestToUsers [INFO] Loading gems for bundler/require [DEBUG] [Rails][Rails] found 33 pins in annotations [INFO] Loading rails 7.0.4 from /home/ali/.rvm/gems/ruby-3.1.3/doc/rails-7.0.4/.yardoc [INFO] Loading bundler 2.3.26 from /home/ali/.rvm/rubies/ruby-3.1.3/lib/ruby/gems/3.1.0/doc/bundler-2.3.26/.yardoc [INFO] Loading bootsnap 1.15.0 from cache [INFO] Loading msgpack 1.6.0 from cache [INFO] Loading rake 13.0.6 from cache [INFO] Loading concurrent-ruby 1.1.10 from cache [INFO] Loading i18n 1.12.0 from cache [INFO] Loading minitest 5.11.3 from cache [INFO] Loading tzinfo 2.0.5 from cache [INFO] Using cached documentation for activesupport at /home/ali/.solargraph/cache/gems/activesupport-7.0.4/yardoc [INFO] Loading activesupport 7.0.4 from cache [INFO] Loading builder 3.2.4 from cache [INFO] Loading erubi 1.12.0 from cache [INFO] Loading racc 1.6.2 from cache [INFO] Loading nokogiri 1.13.10 from cache [INFO] Loading rails-dom-testing 2.0.3 from cache [INFO] Loading crass 1.0.6 from cache [INFO] Loading loofah 2.19.1 from cache [INFO] Loading rails-html-sanitizer 1.4.4 from cache [INFO] Using cached documentation for actionview at /home/ali/.solargraph/cache/gems/actionview-7.0.4/yardoc [INFO] Loading actionview 7.0.4 from cache [INFO] Loading rack 2.2.5 from cache [INFO] Loading rack-test 2.0.2 from cache [INFO] Using cached documentation for actionpack at /home/ali/.solargraph/cache/gems/actionpack-7.0.4/yardoc [INFO] Loading actionpack 7.0.4 from cache [INFO] Loading nio4r 2.5.8 from cache [INFO] Loading websocket-extensions 0.1.5 from cache [INFO] Loading websocket-driver 0.7.5 from cache [INFO] Using cached documentation for actioncable at /home/ali/.solargraph/cache/gems/actioncable-7.0.4/yardoc [INFO] Loading actioncable 7.0.4 from cache [INFO] Loading globalid 1.0.0 from cache [INFO] Using cached documentation for activejob at /home/ali/.solargraph/cache/gems/activejob-7.0.4/yardoc [INFO] Loading activejob 7.0.4 from cache [INFO] Using cached documentation for activemodel at /home/ali/.solargraph/cache/gems/activemodel-7.0.4/yardoc [INFO] Loading activemodel 7.0.4 from cache [INFO] Using cached documentation for activerecord at /home/ali/.solargraph/cache/gems/activerecord-7.0.4/yardoc [INFO] Loading activerecord 7.0.4 from cache [INFO] Loading marcel 1.0.2 from cache [INFO] Loading mini_mime 1.1.2 from cache [INFO] Using cached documentation for activestorage at /home/ali/.solargraph/cache/gems/activestorage-7.0.4/yardoc [INFO] Loading activestorage 7.0.4 from cache [INFO] Loading date 3.3.3 from cache [INFO] Loading timeout 0.3.1 from cache [INFO] Loading net-protocol 0.2.1 from cache [INFO] Loading net-imap 0.3.4 from cache [INFO] Loading net-pop 0.1.2 from cache [INFO] Loading net-smtp 0.3.3 from cache [INFO] Loading mail 2.8.0 from cache [INFO] Using cached documentation for actionmailbox at /home/ali/.solargraph/cache/gems/actionmailbox-7.0.4/yardoc [INFO] Loading actionmailbox 7.0.4 from cache [INFO] Using cached documentation for actionmailer at /home/ali/.solargraph/cache/gems/actionmailer-7.0.4/yardoc [INFO] Loading actionmailer 7.0.4 from cache [INFO] Using cached documentation for actiontext at /home/ali/.solargraph/cache/gems/actiontext-7.0.4/yardoc [INFO] Loading actiontext 7.0.4 from cache [INFO] Loading method_source 1.0.0 from cache [INFO] Loading thor 1.2.1 from cache [INFO] Loading zeitwerk 2.6.6 from cache [INFO] Using cached documentation for railties at /home/ali/.solargraph/cache/gems/railties-7.0.4/yardoc [INFO] Loading railties 7.0.4 from cache [INFO] Loading active_storage_validations 0.8.9 from cache [INFO] Loading execjs 2.8.1 from cache [INFO] Loading autoprefixer-rails 10.4.7.0 from cache [INFO] Loading bcrypt 3.1.18 from cache [INFO] Loading ffi 1.15.5 from cache [INFO] Loading sassc 2.4.0 from cache [INFO] Loading bootstrap-sass 3.4.1 from cache [INFO] Loading will_paginate 3.1.8 from cache [INFO] Loading bootstrap-will_paginate 1.0.0 from cache [INFO] Loading faker 2.1.2 from cache [INFO] Loading mini_magick 4.9.5 from cache [INFO] Loading ruby-vips 2.1.4 from cache [INFO] Loading image_processing 1.9.3 from cache [INFO] Loading importmap-rails 1.1.5 from cache [INFO] Loading jbuilder 2.11.5 from cache [INFO] Loading uri 0.12.0 from cache [INFO] Loading net-http 0.3.2 from cache [INFO] Loading puma 5.6.5 from cache [INFO] Loading sprockets 4.2.0 from cache [INFO] Loading sprockets-rails 3.4.2 from cache [INFO] Loading stimulus-rails 1.2.1 from cache [INFO] Loading turbo-rails 1.3.2 from cache Ruby version: 3.1.3 Solargraph version: 0.48.0 Solargraph Rails version: 1.1.0

ali-p-q commented 1 year ago

PS: I am willing to learn more about the project to help implement this feature. Send me a message if you are looking for people to help maintain the project.