[Awesome Ruby Gem] Use annotate gem to annotate Rails classes with schema and routes info
Annotate (aka AnnotateModels)
The Annotate (aka AnnotateModels) gem annotating Rails classes with schema and routes info by adding a comment summarizing the current schema to the top or bottom of each of your…
-
ActiveRecord models
-
Fixture files
-
Tests and Specs
-
Object Daddy exemplars
-
Machinist blueprints
-
Fabrication fabricators
-
Thoughtbot’s factory_bot factories, i.e. the (spec|test)/factories/
_factory.rb files -
routes.rb file (for Rails projects)
The schema comment looks like this:
1 | # == Schema Info |
Installation
You can install it as a gem:
1 | gem install annotate |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usage
(If you used the Gemfile install, prefix the below commands with bundle exec.)
Usage in Rails
To annotate all your models, tests, fixtures, and factories:
1 | cd /path/to/app |
Configuration
If you want to always skip annotations on a particular model, add this string anywhere in the file:
1 | # -*- SkipSchemaAnnotations |
Configuration in Rails
To generate a configuration file (in the form of a .rake file), to set default options:
1 | rails g annotate:install |
Edit this file lib/tasks/auto_annotate_models.rake
to control things like output format, where annotations are added (top or bottom of file), and in which artifacts.
1 | # cat lib/tasks/auto_annotate_models.rake |
Markdown
The format produced is actually MultiMarkdown, making use of the syntax extension for tables. It’s recommended you use kramdown
as your parser if you want to use this format. If you’re using yard to generate documentation, specify a format of markdown with kramdown as the provider by adding this to your .yardopts file:
1 | # cat .yardopts |
Be sure to add this to your Gemfile as well:
1 | gem 'kramdown', groups => [:development], require => false |
WARNING
Don’t add text after an automatically-created comment block. This tool will blow away the initial/final comment block in your models if it looks like it was previously added by this gem.