Last Updated: February 25, 2016
·
1.694K
· voxdolo

Custom navigation commands in Rails.vim

Time was, all a (wo)man needed to make a custom navigation command in vim for Rails (e.g. Rdecorator) was something like this:

autocmd User Rails Rnavcommand decorator app/decorators -suffix=_decorator.rb -default=model()

Nowadays, that gets you a whole lot of this:

:Rnavcommand has been removed.

For better or worse, those days are gone if you're keeping up with the latest developments in the most important plugin for vim users writing Rails code. Mapping the same command now looks like this:

let g:rails_projections = {
      \ "app/decorators/*_decorator.rb": {
      \   "command": "decorator",
      \   "template":
      \     "class %SDecorator < SimpleDelegator\nend",
      \   "test": [
      \     "test/unit/%s_decorator_test.rb",
      \     "spec/decorators/%s_decorator_spec.rb"
      \   ],
      \  "affinity": "model"
      \ }}

Is it more verbose? Yes. Is it more customizable? Yes. Is the cognitive load higher? Again: Yes. I leave it to you to decide if the change is for the good, but either way that's where we're at. Knowing The Popinator personally, I can tell you he does nothing in his OSS work without reason and I'm guessing the reason here is a win in configurability with the addition of these interpolation variables:

%s: original
%p: pluralized
%i: singularized
%S: camelized
%h: humanized

The addition of those variables makes it possible for the first time for you to make templates for new file generation for your projection without writing a plugin and diving into the internals of Rails.vim (which is a massive codebase, all in one file). Additionally, the manner of specifying the location of alternate and related files for your projection type is much more precise.

For more information on projections in vim for Rails, see the always awesome docs from Mr. Pope:

:help rails-projections

1 Response
Add your response

It's worth noting that that leading "\" in the rails projection declaration is a vim line-continuation character for use in :source and Ex command scripts. Dictionaries look crazy all on one line, so spacing the key-value pairs out with a newline and a line-continuation character makes it a bit more readable.

For more info:

:help line-continuation
over 1 year ago ·