Skip to content

Instantly share code, notes, and snippets.

@codealchemy
Created March 22, 2018 01:36
Show Gist options
  • Select an option

  • Save codealchemy/27cea3c46b9cc7f949d58e855086bd41 to your computer and use it in GitHub Desktop.

Select an option

Save codealchemy/27cea3c46b9cc7f949d58e855086bd41 to your computer and use it in GitHub Desktop.
Replace all '_url' route helpers in templates across a Rails project with '_path' helpers
def update_nested_templates(path)
if File.directory?(path)
Dir.foreach(path) do |filename|
next if %w(. ..).include?(filename)
update_nested_templates(File.absolute_path(filename, path))
end
else
update_template!(path)
end
end
def update_template!(template_path)
template_content = File.read(template_path)
Rails.application.routes.url_helpers.public_methods.grep(/_url\z/).each do |url_helper|
path_helper = url_helper.to_s.gsub("_url", "_path")
template_content.gsub!(" #{url_helper}", " #{path_helper}")
end
File.open(template_path, "w") do |file|
file.write(template_content)
end
$stdout.puts "Updated #{template_path}"
end
Dir.glob(Rails.root.join("app", "views", "*")).each do |template_path|
update_nested_templates(template_path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment