Profile Picture


Tip: Annotate ActiveAdmin resources along with models

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.
      'model_dir'               => 'app/models,app/admin',
      # ... other configuration options

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.

comments powered by Disqus
Separator line
Separator line

Full stack web developer and polyglot programmer with strong interest in dynamic languages, web application development and user experience design.

Strong believer in agile methodologies, behaviour driven development and efficacy of open source technologies.

© 2013 - 2015 Gaurab Paul

Code licensed under the The MIT License. Content and Artwork licensed under CC BY-NC-SA.

The opinions expressed herein are my personal viewpoints and may not be taken as professional recommendations from any of my previous or current employers.

Site is powered by Jekyll and graciously hosted by Github