08 Dec, 2007

Published at 08:00PM

Tagged with rails, rake, and tips

This post has 0 comments

Renaming views for Rails 2.0

Now that Rails 2.0 has officially been released, I’ve been porting a few choice applications to the new version. I’ve been maintaining a rake file with various tasks to assist me in the process. Here’s a particularly useful one that will rename your views to the new action.format.renderer format:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# $> rake upgrade:views:rename

namespace :upgrade do
  namespace :views do
    desc 'Renaming all views to new Rails 2.0 format: action.format.renderer'
    task :rename do
      Dir.glob('app/views/**/[^_]*.rhtml').each do |file|
        puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.html.erb')}`
      end

      Dir.glob('app/views/**/[^_]*.rxml').each do |file|
        puts `svn mv #{file} #{file.gsub(/\.rxml$/, '.xml.builder')}`
      end

      Dir.glob('app/views/**/[^_]*.rjs').each do |file|
        puts `svn mv #{file} #{file.gsub(/\.rjs$/, '.js.rjs')}`
      end

      Dir.glob('app/views/**/[^_]*.haml').each do |file|
        puts `svn mv #{file} #{file.gsub(/\.haml$/, '.html.haml')}`
      end
    end
  end
end

The existing .rhtml templates will still work in Rails 2.0, so this isn’t mandatory or anything. But it’s a good idea to get used to the new format, as it’s a nice convention for dealing with what your actions can respond_to.

Comments

Do you have something to say about this post?
Retype the image to the right Spam Hint: Are You Human? Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch