Annotate

Episode #11 by Teacher's Avatar David Kimura

Summary

With strong params replacing attr_accessible, a difficulty has arisen with referencing to the structure of your models. With annotate, we can generate the structure of our models in the comments of our models.
rails model documentation 2:56

Resources

Summary

# Gemfile
gem 'annotate'

To annotate just your models:

# bash
annotate --exclude tests,fixtures,factories,serializers

Which will generate the below comments

# app/models/user.rb
    # == Schema Information
    #
    # Table name: users
    #
    #  id         :integer          not null, primary key
    #  first_name :string
    #  last_name  :string
    #  active     :boolean
    #  created_at :datetime         not null
    #  updated_at :datetime         not null
    #

    class User < ActiveRecord::Base
      # attr_accessible :first_name, :last_name, :active
    end

To annotate routes.rb:

# bash
    annotate --routes

Which will generate the below comments

# config/routes.rb
    # == Route Map
    #
    #    Prefix Verb   URI Pattern               Controller#Action
    #     users GET    /users(.:format)          users#index
    #           POST   /users(.:format)          users#create
    #  new_user GET    /users/new(.:format)      users#new
    # edit_user GET    /users/:id/edit(.:format) users#edit
    #      user GET    /users/:id(.:format)      users#show
    #           PATCH  /users/:id(.:format)      users#update
    #           PUT    /users/:id(.:format)      users#update
    #           DELETE /users/:id(.:format)      users#destroy
    #      root GET    /                         visitors#index
    #

    Rails.application.routes.draw do
      resources :users
      root to: 'visitors#index'
    end

To remove model/test/fixture/factory/serializer annotations:

# bash
    annotate --delete

To remove routes.rb annotations:

# bash
    annotate --routes --delete

Generating the migration task  

# bash
    rails g annotate:install