Skip to content

Commit

Permalink
Update README introduction
Browse files Browse the repository at this point in the history
  • Loading branch information
grncdr committed May 20, 2023
1 parent 45456df commit c6b9fa7
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
# Solargraph::Rails - Help solargraph with Rails

## Models
Given a typical Rails model like this:
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
# == Schema Information
#
# Table name: my_books
#
# id :integer not null, primary key
# author :string
# name :string
# created_at :datetime not null
# updated_at :datetime not null
#
class MyBook < ApplicationRecord
def my_method
"hello"
class Author < ApplicationRecord
has_many :books

def sortable_name
"#{lastname}, #{firstnames}"
end
end

...
class Book < ApplicationRecord
belongs_to :book

def label
[author.sortable_name, title, isbn].join("\n")
end
end
```

The various Ruby intellisense tools are ok at knowing that there is a `MyBook` constant, and some (including Solargraph) are aware that objects like `MyBook.new` have a method `.my_method`. But what about those magical dynamic attributes that ActiveRecord creates when Rails starts up? You can see these listed at the top of the file under `# == Schema Information`, the comments helpfully added by the Annotate gem.
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, static analysis 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.
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 parses the database schema and YARD docs of various gems to give Solargraph some extra hints. For instance database attributes:

Expand Down

0 comments on commit c6b9fa7

Please sign in to comment.