Tuesday, December 09th, 2008 | Author: viking

In one of my Rails projects, I have some views that are rendered through ERB and others through HAML. This makes it annoying to jump to a view’s spec via rails.vim, since it currently doesn’t handle custom Rcommand’s that accept multiple suffixes.

When I asked tpope today about this, he brought up a good point. Why are view specs associated with an engine type (such as ERB or HAML) by default? You should be able to convert an ERB view to a HAML view and use the same tests.

Per his tip, I renamed my views from names like new.html.haml_spec.rb to just new.html_spec.rb. Unfortunately, autotest stopped recognizing the correlation between my views and my view specs. Putting this in my .autotest file fixed the issue:

Autotest.add_hook :initialize do |at|
  at.remove_mapping(%r%^app/views/(.*)$%)
  at.add_mapping(%r%^app/views/(.*)$%) do |f, md|
    # strip off engine name
    path = md[1].sub(/\.(erb|haml)$/, "")
    at.files_matching(%r{spec/views/#{path}_spec.rb})
  end
end

I hope that helps someone. :)

Category: rails
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
Leave a Reply » Log in