AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Rails annotate gem2/12/2024 ![]() Original article writen by Rick DeNatale and published on Talk Like A Duck | direct link to this article | If you are reading this article elsewhere than Talk Like A Duck, it has been illegally reproduced and without proper authorization. Ruby on Rails added support for enums in Rails 4.1. For example, we could define an enum for the status attribute, where the possible values are pending, active, or archived. This version only requires the annotate_models gem code for the migration tasks, and also only in a non-production environment. In Ruby on Rails, an enum is an attribute where the values map to integers in the database and can be queried by name. Note that the strings in the require statements should both be "annotate_models", for some reason the code formatter in typo is changing the underscores to spaces, no time right now to debug that. I just checked and my current lib/tasks/annotations.rake looks like the above. I think the right way to do this would be to fork annotate on github, and change it to register the rake task using a railtie, and then issue a pull request, but I guess I'm being lazy. It's pragmatic, it works, although I guess I'm a bit exposed to changes in the annotate gem. Require " annotate/annotate_models " AnnotateModels.do_annotations( :position_in_class => ' before ', :position_in_fixture => ' before ') Then I added the following file which I called 'annotations.rake' to lib/tasks in my Rails 3 project: ![]() First I added the annotate gem to the development group in my Gemfile. I wanted to install the gem with bundler, after which it took me a bit of experimentation to get automatic annotation.Īfter playing around with it for awhile I came up with this. The readme claims that installing it as a plugin also modifies the the db:migrate rake tasks to automatically run schema annotation to keep in sync with what your migrations are doing to the schema. Installing the gem provides an annotate gem binary with which you can manually produce these annotations. It also provides a separate command/rake task for annotating your config/routes.rb with the output from rake routes. It now annotates things like model specs, fixtures (if you are still using them) and various fixture replacement files like machinist blueprints. Google alerted me to what seems to be a popular, modernized fork, maintained by Cuong Tran, who gemified it and expanded the function. But I got the yen the other day to start using it again. I'd fallen out of using it, just falling into the habit of opening up schema.rb and searching for the model name. For those few of you who might not have run across it, it adds a comment block at the top of a rails active record model class with the relevant section from db/schema.rb. One of the oldest Rails plugins is annotate_models which was originally written by Prag Dave Thomas.
0 Comments
Read More
Leave a Reply. |