Created
September 12, 2012 21:34
-
-
Save rwjblue/3710089 to your computer and use it in GitHub Desktop.
Rename a current route, and update all path helpers to the new name.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'pathname' | |
old_controller_name = ARGV[0] | |
new_controller_name = ARGV[1] | |
old_routes = `rake routes | grep #{old_controller_name}`.split("\n").map{|str| str.strip.split.first}.uniq.compact - ['GET','PUT','POST','DELETE','root'] | |
routes_file_contents = File.read('config/routes.rb') | |
File.open('config/routes.rb', 'w'){|io| io.write routes_file_contents.gsub(":#{old_controller_name}", ":#{new_controller_name}") } | |
new_routes = `rake routes | grep #{new_controller_name}`.split("\n").map{|str| str.strip.split.first}.uniq.compact - ['GET','PUT','POST','DELETE','root'] | |
modified_files = {} | |
Pathname.glob('app/**/*').each do |file| | |
next unless file.extname =~ /rb\z/ | |
next if file.directory? | |
puts file.to_s | |
begin | |
file_contents = file.read | |
old_routes.each_with_index do |old_route, index| | |
file_contents.gsub!(/(\W)#{old_route + '_path'}/, "\\1#{new_routes[index] + '_path'}") | |
file_contents.gsub!(/(\W)#{old_route + '_url'}/, "\\1#{new_routes[index] + '_url'}") | |
modified_files[file] = file_contents | |
end | |
rescue => ex | |
puts [filename, ex.message, *ex.backtrace] | |
end | |
end | |
modified_files.each{|file,contents| file.open('w'){|io| io.write contents}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment