https://github.com/iftheshoefritz/solargraph-rails
Solargraph plugin to add awareness of Rails-specific code
https://github.com/iftheshoefritz/solargraph-rails
Last synced: 4 months ago
JSON representation
Solargraph plugin to add awareness of Rails-specific code
- Host: GitHub
- URL: https://github.com/iftheshoefritz/solargraph-rails
- Owner: iftheshoefritz
- License: mit
- Created: 2020-11-03T04:52:16.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-06-23T19:33:15.000Z (over 1 year ago)
- Last Synced: 2024-11-17T14:14:15.300Z (about 1 year ago)
- Language: Ruby
- Size: 7.74 MB
- Stars: 322
- Watchers: 10
- Forks: 30
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
- stars - iftheshoefritz/solargraph-rails - Solargraph plugin to add awareness of Rails-specific code (Ruby)
README
# Solargraph::Rails - Help solargraph with Rails
## Models
Consider pair of typical Rails models like this:
```sh
rails g model Author lastname:string firstnames:string
rails g model Book title:string isbn:string author:belongs_to
```
```ruby
class Author < ApplicationRecord
has_many :books
def sortable_name
"#{lastname}, #{firstnames}"
end
end
class Book < ApplicationRecord
belongs_to :author
def label
[author.sortable_name, title, isbn].join("\n")
end
end
```
The various Ruby intellisense tools are ok at knowing that there are `Book` and `Author` constants, and some (including Solargraph) are aware that objects like `Book.new` have a `.label` method. But what about those "magical" dynamic methods that ActiveRecord creates like `.title`, or `.author`?
Since these attributes are only created at runtime, a simple static analysis of the `Book` class alone can't identify them. Your editor has no idea that these attributes exist, but they're amongst the most common things that you will work with in any Rails app.
That's where this plugin for Solargraph comes in: it understands db/schema.rb and any comments from the annotate\_models gem for models, and also supplies key annotations and Rails-specific context on top of what Solargraph pulls via YARD and RBS. As a result, you have access to database attributes:

... and ActiveRecord finders:

... and associations:

... and routes file syntax:

and more!
## Installation
### Install `solargraph` and `solargraph-rails`
If you add them to your Gemfile, you'll have to tell your IDE plugin to use bundler to load the right version of solargraph.
### Import Rails RBS types
Use [gem\_rbs\_collection](https://github.com/ruby/gem_rbs_collection)
to install RBS types for Rails:
```sh
rbs collection init
rbs collection install
```
### Add `solargraph-rails` to your `.solargraph.yml`
(if you don't have a `.solargraph.yml` in your project root, you can run `solargraph config` to add one)
```
plugins:
- solargraph-rails
```
### Build YARD docs
In the project root, run `yard gems`.
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/iftheshoefritz/solargraph_rails.
1. create fork and clone the repo
2. install gem deps `bundle install`
3. install dummy rails app deps:
```
cd spec/rails7 && bundle install && rbs collection init && rbs collection install && cd ../../
cd spec/rails8 && bundle install && rbs collection init && rbs collection install && cd ../../
```
4. now tests should pass locally and you can try different changes
5. submit PR
See [DEVELOPMENT.md](./DEVELOPMENT.md) for more information
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).