From e3e1c1d3e29740c77df723e602da608409c58ba7 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 3 Feb 2019 19:43:36 +0900 Subject: [PATCH 1/7] Add AnnotateRoutes::HeaderGenerator#markdown? --- lib/annotate/annotate_routes/header_generator.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 941f9bb99..f4ee3f7b0 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -51,13 +51,13 @@ def generate out << comment(options[:wrapper_open]) if options[:wrapper_open] - out << comment(options[:format_markdown] ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') + out << comment(markdown? ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') out << comment return out if contents_without_magic_comments.size.zero? maxs = [HEADER_ROW.map(&:size)] + contents_without_magic_comments[1..-1].map { |line| line.split.map(&:size) } - if options[:format_markdown] + if markdown? max = maxs.map(&:max).compact.max out << comment(content(HEADER_ROW, maxs)) @@ -66,7 +66,7 @@ def generate out << comment(content(contents_without_magic_comments[0], maxs)) end - out += contents_without_magic_comments[1..-1].map { |line| comment(content(options[:format_markdown] ? line.split(' ') : line, maxs)) } + out += contents_without_magic_comments[1..-1].map { |line| comment(content(markdown? ? line.split(' ') : line, maxs)) } out << comment(options[:wrapper_close]) if options[:wrapper_close] out @@ -85,7 +85,7 @@ def comment(row = '') end def content(line, maxs) - return line.rstrip unless options[:format_markdown] + return line.rstrip unless markdown? line.each_with_index.map do |elem, index| min_length = maxs.map { |arr| arr[index] }.max || 0 @@ -93,5 +93,9 @@ def content(line, maxs) format("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) end.join(' | ') end + + def markdown? + options[:format_markdown] + end end end From da830582ac271d41c8b5d8ef3f79d8c762572f21 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sat, 4 Jan 2020 05:54:29 +0900 Subject: [PATCH 2/7] Add AnnotateRoutes::HeaderGenerator#timestamp_if_required --- lib/annotate/annotate_routes/header_generator.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index f4ee3f7b0..8708b25f9 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -51,7 +51,7 @@ def generate out << comment(options[:wrapper_open]) if options[:wrapper_open] - out << comment(markdown? ? PREFIX_MD : PREFIX) + (options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '') + out << comment(markdown? ? PREFIX_MD : PREFIX) + timestamp_if_required out << comment return out if contents_without_magic_comments.size.zero? @@ -97,5 +97,9 @@ def content(line, maxs) def markdown? options[:format_markdown] end + + def timestamp_if_required + options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '' + end end end From 4392231d5e9ac6661c1082f264ea12e9256dd90e Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 16:21:06 +0900 Subject: [PATCH 3/7] Add AnnotateRoutes::HeaderGenerator#format_line_element --- lib/annotate/annotate_routes/header_generator.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 8708b25f9..6a53e4a90 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -87,11 +87,12 @@ def comment(row = '') def content(line, maxs) return line.rstrip unless markdown? - line.each_with_index.map do |elem, index| - min_length = maxs.map { |arr| arr[index] }.max || 0 + line.each_with_index.map { |elem, index| format_line_element(elem, maxs, index) }.join(' | ') + end - format("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) - end.join(' | ') + def format_line_element(elem, maxs, index) + min_length = maxs.map { |arr| arr[index] }.max || 0 + format("%-#{min_length}.#{min_length}s", elem.tr('|', '-')) end def markdown? From a51cc045dd422d425733ac55e1bba0a9459f6831 Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 16:23:11 +0900 Subject: [PATCH 4/7] Refactor AnnotateRoutes::HeaderGenerator.app_routes_map --- lib/annotate/annotate_routes/header_generator.rb | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 6a53e4a90..89fe1aecc 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -17,20 +17,23 @@ def generate(options = {}) private def app_routes_map(options) - routes_map = `rake routes`.chomp("\n").split(/\n/, -1) + result = `rake routes`.chomp("\n").split(/\n/, -1) # In old versions of Rake, the first line of output was the cwd. Not so # much in newer ones. We ditch that line if it exists, and if not, we # keep the line around. - routes_map.shift if routes_map.first =~ %r{^\(in \/} + result.shift if result.first =~ %r{^\(in \/} + + ignore_routes = options[:ignore_routes] + regexp_for_ignoring_routes = ignore_routes ? /#{ignore_routes}/ : nil # Skip routes which match given regex # Note: it matches the complete line (route_name, path, controller/action) - if options[:ignore_routes] - routes_map.reject! { |line| line =~ /#{options[:ignore_routes]}/ } + if regexp_for_ignoring_routes + result.reject { |line| line =~ regexp_for_ignoring_routes } + else + result end - - routes_map end end From fca5b36a476c503659508194d66e71a37eb8c89f Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 16:24:55 +0900 Subject: [PATCH 5/7] Refactor AnnotateRoutes::HeaderGenerator#time_stamp_if_required --- lib/annotate/annotate_routes/header_generator.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 89fe1aecc..03a11ce9d 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -102,8 +102,13 @@ def markdown? options[:format_markdown] end - def timestamp_if_required - options[:timestamp] ? " (Updated #{Time.now.strftime('%Y-%m-%d %H:%M')})" : '' + def timestamp_if_required(time = Time.now) + if options[:timestamp] + time_formatted = time.strftime('%Y-%m-%d %H:%M') + " (Updated #{time_formatted})" + else + '' + end end end end From 3f54f5738747179d88a165b2bbd2b029e5e79dfe Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Thu, 9 Jan 2020 16:33:19 +0900 Subject: [PATCH 6/7] Rename AnnotateRoutes::HeaderGenerator.app_routes_map to .routes_map --- lib/annotate/annotate_routes/header_generator.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/annotate/annotate_routes/header_generator.rb b/lib/annotate/annotate_routes/header_generator.rb index 03a11ce9d..b1c93acf7 100644 --- a/lib/annotate/annotate_routes/header_generator.rb +++ b/lib/annotate/annotate_routes/header_generator.rb @@ -8,15 +8,14 @@ class HeaderGenerator class << self def generate(options = {}) - routes_map = app_routes_map(options) - new(options, routes_map).generate + new(options, routes_map(options)).generate end private :new private - def app_routes_map(options) + def routes_map(options) result = `rake routes`.chomp("\n").split(/\n/, -1) # In old versions of Rake, the first line of output was the cwd. Not so From 32d50e573df2e2d9bdf2ae37e8c4d997b74cf44d Mon Sep 17 00:00:00 2001 From: Shu Fujita Date: Sun, 5 Apr 2020 20:40:37 +0900 Subject: [PATCH 7/7] Execute `rubocop --auto-gen-config` --- .rubocop_todo.yml | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 4d802d81c..5863ac72e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2020-04-03 00:51:53 +0900 using RuboCop version 0.68.1. +# on 2020-04-05 20:42:06 +0900 using RuboCop version 0.68.1. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -179,11 +179,11 @@ Lint/ShadowingOuterLocalVariable: Exclude: - 'Rakefile' -# Offense count: 21 +# Offense count: 22 Metrics/AbcSize: - Max: 145 + Max: 103 -# Offense count: 8 +# Offense count: 7 # Configuration parameters: CountComments, ExcludedMethods. # ExcludedMethods: refine Metrics/BlockLength: @@ -194,18 +194,18 @@ Metrics/BlockLength: Metrics/BlockNesting: Max: 4 -# Offense count: 11 +# Offense count: 12 Metrics/CyclomaticComplexity: - Max: 37 + Max: 25 -# Offense count: 29 +# Offense count: 30 # Configuration parameters: CountComments, ExcludedMethods. Metrics/MethodLength: - Max: 71 + Max: 40 -# Offense count: 8 +# Offense count: 9 Metrics/PerceivedComplexity: - Max: 42 + Max: 28 # Offense count: 1 Naming/AccessorMethodName: @@ -331,14 +331,13 @@ Style/HashSyntax: - 'lib/tasks/annotate_routes.rake' - 'spec/lib/annotate/annotate_models_spec.rb' -# Offense count: 8 +# Offense count: 7 # Cop supports --auto-correct. Style/IfUnlessModifier: Exclude: - 'Rakefile' - 'bin/annotate' - 'lib/annotate/annotate_models.rb' - - 'lib/annotate/annotate_routes/header_generator.rb' # Offense count: 1 # Cop supports --auto-correct. @@ -520,7 +519,7 @@ Style/UnneededPercentQ: Exclude: - 'annotate.gemspec' -# Offense count: 375 +# Offense count: 377 # Cop supports --auto-correct. # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns. # URISchemes: http, https