From c6d18df4f96d9674cbe48d061c698d00d72a3700 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 00:23:58 +0900 Subject: [PATCH 1/2] Refactor AnnotateRoutes.rewrite_contents_with_header --- lib/annotate/annotate_routes.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index b91f46417..dfc24ae2d 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -66,14 +66,7 @@ def rewrite_contents_with_header(existing_text, header, options = {}) content, header_position = strip_annotations(existing_text) new_content = annotate_routes(header, content, header_position, options) new_text = new_content.join("\n") - - if existing_text == new_text - puts "#{routes_file} unchanged." - false - else - File.open(routes_file, 'wb') { |f| f.puts(new_text) } - true - end + rewrite_contents(existing_text, new_text) end def header(options = {}) From 3fbd026835f4eda6f615e8d719e7e30356cc68c0 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 00:53:55 +0900 Subject: [PATCH 2/2] Remove AnnotateRoute.rewrite_contents_with_header --- lib/annotate/annotate_routes.rb | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/annotate/annotate_routes.rb b/lib/annotate/annotate_routes.rb index dfc24ae2d..732645d48 100644 --- a/lib/annotate/annotate_routes.rb +++ b/lib/annotate/annotate_routes.rb @@ -30,7 +30,11 @@ class << self def do_annotations(options = {}) if routes_file_exist? existing_text = File.read(routes_file) - if rewrite_contents_with_header(existing_text, header(options), options) + content, header_position = strip_annotations(existing_text) + new_content = annotate_routes(header(options), content, header_position, options) + new_text = new_content.join("\n") + + if rewrite_contents(existing_text, new_text) puts "#{routes_file} annotated." end else @@ -62,13 +66,6 @@ def routes_file @routes_rb ||= File.join('config', 'routes.rb') end - def rewrite_contents_with_header(existing_text, header, options = {}) - content, header_position = strip_annotations(existing_text) - new_content = annotate_routes(header, content, header_position, options) - new_text = new_content.join("\n") - rewrite_contents(existing_text, new_text) - end - def header(options = {}) routes_map = app_routes_map(options)