How to use annotate gem | Annotate model structure | Inkoop Blog

Annotate gem provides some much needed documentation to your Ruby on Rails Application. We talk about how to set it upa and use it in some important parts of Ruby on Rails.

Posted by Yogesh on 01 Oct 2018

Annotate gem will add the table structure like field name, index, foreign_keys as comments at the top or bottom of the model, it can annotate tests, fixtures, factories, serializers, routes also.

It has option to annotate whenever we run migration rake db:migrate.

Installation

Add this into your Gemfile in development group

group :development do
  gem 'annotate'
end
bundle

Configuration

rails g annotate:install # will generate the rake file

The generated rakefile present under lib/tasks/autoannotatemodels.rake, this file has the default annotate configuration you can override any of those.

Usage

Add schema information as comments to model and fixture files

rake annotate_models

After execute the command

Sample 1

app/models/user.rb

# == Schema Information
#
# Table name: users
#
#  id                       :integer          not null, primary key
#  email                    :string(255)      default(""), not null
#  gender                   :string(1)
#  first_name               :string(255)
#  last_name                :string(255)
#  encrypted_password       :string(255)      default(""), not null
#  last_sign_in_at          :datetime
#  last_sign_in_ip          :string(255)
#  reset_password_sent_at   :datetime
#  reset_password_token     :string(255)
#  sign_in_count            :integer          default(0), not null
#  created_at               :datetime
#  updated_at               :datetime
#  school_id                :integer
#  state_id                 :integer
#  country_id               :integer
#
# Indexes
#
#  index_users_on_school_id               (school_id)
#  index_users_on_confirmation_token      (confirmation_token) UNIQUE
#  index_users_on_email                   (email)
#  index_users_on_reset_password_token    (reset_password_token) UNIQUE
#
# Foreign Keys
#
#  fk_rails_...  (school_id => schools.id)
#

class User < ActiveRecord::Base
  .....
end

Sample 2

Add the route map to config/routes.rb

rake annotate_routes

After executing the command

# == Route Map
#
#               Prefix Verb     URI Pattern                        Controller#Action
#                 root GET      /                                  home#index
#        user_password POST     /account/password(.:format)        users/passwords#create
#    new_user_password GET      /account/password/new(.:format)    users/passwords#new
#   edit_user_password GET      /account/password/edit(.:format)   users/passwords#edit
#    user_registration POST     /sign-up(.:format)                 users/registrations#create
#         user_session POST     /sign-in(.:format)                 users/sessions#create
# destroy_user_session DELETE   /sign-out(.:format)                users/sessions#destroy
#                 user GET      /account(.:format)                 users#index
Test::Application.routes.draw do
  root to: 'home#index'
  .....

- Yogesh Kumar


Have a Project in mind?