Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Annotate ActiveRecord models as a gem

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.rdoc

Annotate (aka AnnotateModels)

IMPORTANT: If you're upgrading from a previous version, including 2.5.0.beta3, re-run the generator task!

Add 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_girl factories, i.e. the (spec|test)/factories/<model>_factory.rb files

  • routes.rb file (for Rails projects)

The schema comment looks like this:

# == Schema Info
#
# Table name: line_items
#
#  id                  :integer(11)    not null, primary key
#  quantity            :integer(11)    not null
#  product_id          :integer(11)    not null
#  unit_price          :float
#  order_id            :integer(11)
#

 class LineItem < ActiveRecord::Base
   belongs_to :product
  . . .

It also annotates geometrical columns, geom type and srid, when using `SpatialAdapter` or `PostgisAdapter`:

# == Schema Info
#
# Table name: trips
#
#  local           :geometry        point, 4326
#  path            :geometry        line_string, 4326

Also, if you pass the -r option, it'll annotate routes.rb with the output of `rake routes`.

Install

Into Gemfile from Github:

gem 'annotate', :git => 'git://github.com/MrJoy/annotate_models.git'

Into environment gems from rubygems.org:

gem install annotate

Into environment gems from Github checkout:

git clone git://github.com/MrJoy/annotate_models.git annotate_models
cd annotate_models
rake build
gem install pkg/annotate-*.gem

Commands

(If you used the Gemfile install, prefix the below commands with `bundle exec`.)

It's important to note that there are three binaries provided by this gem:

  • `annotate`: Can annotate models (and related artifacts), or `routes.rb`.

  • `annotate_models`: Can annotate models (and related artifacts).

  • `annotate_routes`: Can annotate `routes.rb`.

The reason for this is that `annotate` is also used by ImageMagick, and so can be a source of conflicts. The name `annotate_models` is misleading given the additional functionality of annotating `routes.rb` so two binaries are provided, one for each task.

Usage

Usage in Rails

To annotate all your models, tests, fixtures, and factories:

cd /path/to/app
annotate

Or:

cd /path/to/app
annotate_models

To annotate just your models, tests, and factories:

annotate --exclude fixtures

To annotate just your models:

annotate --exclude tests,fixtures,factories

To annotate routes.rb:

annotate --routes

Or:

annotate_routes

To remove model/test/fixture/factory annotations:

annotate --delete

To remove routes.rb annotations:

annotate --routes --delete

Or:

annotate_routes --delete

Usage Outside of Rails

Everything above applies, except that –routes is not meaningful, and you will probably need to explicitly set one or more `–require` option(s), and/or one or more `–model-dir` options to inform annotate about the structure of your project and help it bootstrap and load the relevant code.

Configuration

If you want to always skip annotations on a particular model, add this string anywhere in the file:

# -*- SkipSchemaAnnotations

To generate a configuration file (in the form of a `.rake` file), to set default options:

rails g annotate:install

Edit this file to control things like output format, where annotations are added (top or bottom of file), and in which artifacts.

Configuration in Rails

To generate a configuration file (in the form of a `.rake` file), to set default options:

rails g annotate:install

Edit this file to control things like output format, where annotations are added (top or bottom of file), and in which artifacts.

Rails Integration

By default, once you've generated a configuration file, annotate will be executed whenever you run `rake db:migrate` (but only in development mode). If you want to disable this behavior permanently, edit the `.rake` file and change:

'skip_on_db_migrate'   => "false",

To:

'skip_on_db_migrate'   => "try",

If you want to run `rake db:migrate` as a one-off without running annotate, you can do so with a simple environment variable, instead of editing the `.rake` file:

skip_on_db_migrate=1 rake db:migrate

Options

Usage: annotate [options] [ModelName]*
    -m, --show-migration             Include the migration version number in the annotation
    -i, --show-indexes               List the table's database indexes in the annotation
    -s, --simple-indexes             Concat the column's related indexes in the annotation
    -D, --model-dir dir              Annotate model files stored in dir rather than app/models, may be used multiple times
    -I, --ignore-model-subdirs       Ignore subdirectories of the models directory
    -S, --sort                       Sort columns in creation order rather than alphabetically
    -e [tests,fixtures,factories],   Do not annotate fixtures, test files, and/or factories
        --exclude
    -f [bare|rdoc|markdown],         Render Schema Infomation as plain/RDoc/Markdown
        --format
    -F, --force                      Force new annotations even if there are no changes.
    -t, --trace                      If unable to annotate a file, print the full stack trace, not just the exception message.
    -r, --routes                     Annotate the Routes.rb file instead of annotating other items like the model/test/fixture/factory.
    -p, --position [before|after]    Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)
        --pc, --position-in-class [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of the model file
        --pf, --position-in-factory [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any factory files
        --px, --position-in-fixture [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any fixture files
        --pt, --position-in-test [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any test files
        --pr, --position-in-routes [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any test files
    -R, --require path               Additional file to require before loading models, may be used multiple times
    -d, --delete                     Remove annotations from all relevant files
    -v, --version                    Show the current version of this gem

Usage: annotate_models [options] [ModelName]*
    -m, --show-migration             Include the migration version number in the annotation
    -i, --show-indexes               List the table's database indexes in the annotation
    -s, --simple-indexes             Concat the column's related indexes in the annotation
    -D, --model-dir dir              Annotate model files stored in dir rather than app/models, may be used multiple times
    -I, --ignore-model-subdirs       Ignore subdirectories of the models directory
    -S, --sort                       Sort columns in creation order rather than alphabetically
    -e [tests,fixtures,factories],   Do not annotate fixtures, test files, and/or factories
        --exclude
    -f [bare|rdoc|markdown],         Render Schema Infomation as plain/RDoc/Markdown
        --format
    -F, --force                      Force new annotations even if there are no changes.
    -t, --trace                      If unable to annotate a file, print the full stack trace, not just the exception message.
    -p, --position [before|after]    Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)
        --pc, --position-in-class [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of the model file
        --pf, --position-in-factory [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any factory files
        --px, --position-in-fixture [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any fixture files
        --pt, --position-in-test [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any test files
    -R, --require path               Additional file to require before loading models, may be used multiple times
    -d, --delete                     Remove annotations from all relevant files
    -v, --version                    Show the current version of this gem

Usage: annotate_routes [options]
    -p, --position [before|after]    Place the annotations at the top (before) or the bottom (after) of the model/test/fixture/factory/routes file(s)
        --pr, --position-in-routes [before|after]
                                     Place the annotations at the top (before) or the bottom (after) of any test files
    -R, --require path               Additional file to require before loading models, may be used multiple times
    -d, --delete                     Remove annotations from all relevant files
    -v, --version                    Show the current version of this gem

Sorting

By default, columns will be sorted in database order (i.e. the order in which migrations were run).

If you prefer to sort alphabetically so that the results of annotation are consistent regardless of what order migrations are executed in, use –sort.

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:

--markup markdown
--markup-provider kramdown

Be sure to add this to your `Gemfile` as well:

gem 'kramdown', :groups => [:development], :require => false

WARNING

Note that this code will blow away the initial/final comment block in your models if it looks like it was previously added by this gem, so you don't want to add additional text to an automatically created comment block.

BACK UP YOUR MODELS BEFORE USING THIS TOOL!

Links

License

Released under the same license as Ruby. No Support. No Warranty.

Authors

With help from:

and many others that I may have missed to add.

Something went wrong with that request. Please try again.