Skip to content

Instantly share code, notes, and snippets.

@tomily1
Created June 12, 2018 11:10
Show Gist options
  • Save tomily1/e3ce162cfd69a8dedb77e8bf208b7628 to your computer and use it in GitHub Desktop.
Save tomily1/e3ce162cfd69a8dedb77e8bf208b7628 to your computer and use it in GitHub Desktop.
Markdown Formatter for rake taks
module ActionDispatch
module Routing
class MarkdownFormatter
def initialize
@buffer = []
end
def result
@buffer.join("\n")
end
def section_title(title)
@buffer << "\n#{title}:"
end
def section(routes)
@buffer << draw_section(routes)
end
def header(routes)
@buffer << draw_header(routes)
end
def no_routes
@buffer << <<-MESSAGE.strip_heredoc
You don't have any routes defined!
Please add some routes in config/routes.rb.
For more information about routes, see the Rails guide: http://guides.rubyonrails.org/routing.html.
MESSAGE
end
private
def draw_section(routes)
header_lengths = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action' ].map(&:length)
name_width, verb_width, path_width, reqs_width = widths(routes).zip(header_lengths).map(&:max)
routes.map do |r|
"| #{r[:name].rjust(name_width)} | #{r[:verb].ljust(verb_width)} | #{r[:path].ljust(path_width)} | #{r[:reqs].ljust(reqs_width)} |"
end
end
def draw_header(routes)
name_width, verb_width, path_width, reqs_width = widths(routes)
header = "| #{"Prefix".rjust(name_width)} | #{"Verb".ljust(verb_width)} | #{"URI Pattern".ljust(path_width)} | #{"Controller#Action".ljust(reqs_width)} |\n"
divider = "-" * header.size
header + divider
end
def widths(routes)
[routes.map { |r| r[:name].length }.max,
routes.map { |r| r[:verb].length }.max,
routes.map { |r| r[:path].length }.max,
routes.map { |r| r[:reqs].length }.max]
end
end
end
end
desc 'Print out all defined routes in match order, with names in markdown format. Target specific controller with CONTROLLER=x.'
task markdown_routes: :environment do
all_routes = Rails.application.routes.routes
require 'action_dispatch/routing/inspector'
inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
puts inspector.format(ActionDispatch::Routing::MarkdownFormatter.new, ENV['CONTROLLER'])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment