Skip to content
Jared Smith edited this page Sep 13, 2018 · 10 revisions

Tip: Automatically annotate models after each run of rake db:migrate and rake db:rollback

Version A

In Rails 3 using as gem - also see the discussion on this issue

# lib/tasks/auto_annotate_models# Annotate models on each run of rake db:migrateDir["#{Gem::Specification.find_by_name("annotate").full_gem_path}/**/tasks/**/*.rake"].each{|ext| loadext}ifRails.env.development?

Version B

This way was tested with the current master version of annotate (2.6.0.beta1), as the one propose in Version A) did not work properly.

# Gemfile# Annotate models in the current master version from githubgem'annotate',github: 'ctran/annotate_models'# This syntax works with Bundler 1.2+
# lib/tasks/auto_annotate_models.rakeifRails.env.development?task:set_annotation_optionsdo# Just some example settings from annotate 2.6.0.beta1Annotate.set_defaults('routes'=>'false','position_in_routes'=>'before','position_in_class'=>'before','position_in_test'=>'before','position_in_fixture'=>'before','position_in_factory'=>'before','position_in_serializer'=>'before','show_foreign_keys'=>'true','show_complete_foreign_keys'=>'false','show_indexes'=>'true','simple_indexes'=>'false','model_dir'=>'app/models','root_dir'=>'','include_version'=>'false','require'=>'','exclude_tests'=>'false','exclude_fixtures'=>'false','exclude_factories'=>'false','exclude_serializers'=>'false','exclude_scaffolds'=>'true','exclude_controllers'=>'true','exclude_helpers'=>'true','exclude_sti_subclasses'=>'false','ignore_model_sub_dir'=>'false','ignore_columns'=>nil,'ignore_routes'=>nil,'ignore_unknown_models'=>'false','hide_limit_column_types'=>'integer,boolean','hide_default_column_types'=>'json,jsonb,hstore','skip_on_db_migrate'=>'false','format_bare'=>'true','format_rdoc'=>'false','format_markdown'=>'false','sort'=>'false','force'=>'false','classified_sort'=>'false','trace'=>'false','wrapper_open'=>nil,'wrapper_close'=>nil,'with_comment'=>true)end# Comes with the current master when running `rails g annotate:install`# But somehow won't annotate my models correctly (only one)# Thus commented out# Annotate.load_tasks# Annotate modelstask:annotatedoputs'Annotating models...'system'bundle exec annotate'end# Run annotate task after db:migrate# and db:rollback tasksRake::Task['db:migrate'].enhancedoRake::Task['annotate'].invokeendRake::Task['db:rollback'].enhancedoRake::Task['annotate'].invokeendend