Last Updated: February 25, 2016
·
1.02K
· ahacop

Add ActiveRecord scopes to Ctags

Ctags is a great tool for improving the navigability of a project within a text editor. Unfortunately, it doesn’t create tags for Rails-specific features. Luckily, we can pretty easily add custom tags ourselves!

To create a custom tag for ActiveRecord scopes just use the —regex-<LANG> command line option. The option requires a regex replacement pattern containing: (1) the pattern matching the language feature in our source code, and (2) the tag entry that should be created for it. In our case, we want to look for the scope definition, and we want to create a tag with the name of the scope.

--regex-ruby=/^[ \t]*scope[ \t]*:([a-zA-Z0-9_]+)/\1/

Now, if we see an ActiveRecord scope in our text editor we can quickly jump to the definition in the model!

The option will be applied by default when added to the .ctags configuration file.

echo '--regex-ruby=/^[ \t]*scope[ \t]*:([a-zA-Z0-9_]+)/\1/' >> ~/.ctags

See the Ctags documentation for more info.