Created
March 22, 2018 01:36
-
-
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
This file contains hidden or 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
| 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