Posted over 5 years ago. Visible to the public.

Make "rake notes" learn about Haml, Sass, CoffeeScript, and other file types

Rails comes with a Rake task notes that shows code comments that start with "TODO", "FIXME", or "OPTIMIZE".

While it's generally not good practice to leave them in your code (your work is not done until it's done), in larger projects you will occasionally have to use them as other parts of the application that you depend upon are not yet available.
To keep track of them, run rake notes. Its output looks something like this:

Copy
$ rake notes app/controllers/frontend/media_documents_controller.rb: * [ 6] [TODO] should be part of a publication workflow app/helpers/frontend/slider_helper.rb: * [48] [TODO] this is only temporary till frontend is done

However, with Rails' default configuration this task will only look through default file extensions, like .rb, .erb, .css, .js.

When using different markup like Haml, Sass, or CoffeeScript, you need to make Rails aware of its extensions and how comments work in each of those languages. Put the following into config/initializers/ and you are good to go:

Copy
Rails.application.config.annotations.tap do |notes| notes.register_extensions('scss', 'sass') { |annotation| %r(//\s*(#{annotation}):?\s*(.*?)$) } notes.register_extensions('haml') { |annotation| %r(#\s*(#{annotation}):?\s*(.*?)$) } notes.register_extensions('coffee') { |annotation| %r(#\s*(#{annotation}):?\s*(.*?)$) } end

The block receives annotation which is a string containing a regular expression fragment ("OPTIMIZE|FIXME|TODO") and needs to return a regular expression that matches comments in the given file type. If you have other file types that you want to register, define accordingly.

Next time you run rake notes, comments in such files should show up:

Copy
$ rake notes app/assets/stylesheets/frontend/blocks/slider.sass: * [ 9] [TODO] Not sure if this is correct app/controllers/frontend/media_documents_controller.rb: * [ 6] [TODO] should be part of a publication workflow app/helpers/frontend/slider_helper.rb: * [48] [TODO] this is only temporary till frontend is done
Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Arne Hartherz
Last edit:
over 5 years ago
by Arne Hartherz
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more