Annotate is a very useful utility for annotating schema information in Rails models. The gem prepends (or appends, as per your configuration) a summarized information about the table schema in each model file as comments) - saving multiple roundtrips to the schema.rb
file during coding.
For Example:
# == Schema Information
#
# Table name: roles
#
# id :integer not null, primary key
# name :string
# resource_id :integer
# resource_type :string
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_roles_on_name (name)
# index_roles_on_name_and_resource_type_and_resource_id (name,resource_type,resource_id)
#
However while working with admin interfaces powered by ActiveAdmin I have often found it convenient to have the model schema annotations available right in the ActiveAdmin resource files. There is a very simple way to achieve that:
The rake task generated by Annotate (lib/tasks/auto_annotate_models.rake
) has option to specify the model directory. This is primarily intended for projects with non standard directory configuration. As it so happens we can specify multiple directories separated by commas.
So taking advantage of the fact that our ActiveAdmin resource files are conventionally named same as the models, we can just add the admin directory to the list of model directories:
if Rails.env.development?
task :set_annotation_options do
# You can override any of these by setting an environment variable of the
# same name.
Annotate.set_defaults(
'model_dir' => 'app/models,app/admin',
# ... other configuration options
)
end
end
Next time annotate is run, either manually invoked or through migration hooks, our ActiveAdmin resource files with annotated along with our model files.
If you are not using the generated rake file, this approach of course works with the --model-dir
command line option as well.