Last active
November 7, 2019 18:48
-
-
Save covard/4740914a2797e7bb671a2fa9002da00a to your computer and use it in GitHub Desktop.
Unused Rails routes [ https://hackernoon.com/get-unused-routes-of-large-rails-app-0h1cu32lj ]
This file contains 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
require 'tabulo' | |
require 'rainbow' | |
Rails.application.eager_load! | |
unused_routes = {} | |
# Iterating over all non-empty routes from RouteSet | |
Rails.application.routes.routes.map(&:requirements).reject(&:empty?).each do |route| | |
name = route[:controller].camelcase | |
next if name.start_with?("Rails") | |
controller = "#{name}Controller" | |
if Object.const_defined?(controller) && !controller.constantize.new.respond_to?(route[:action]) | |
# Get route for which associated action is not present and add it in final results | |
unless Dir.glob(Rails.root.join("app", "views", name.downcase, "#{route[:action]}.*")).any? | |
unused_routes[controller] = [] if unused_routes[controller].nil? | |
unused_routes[controller] << route[:action] | |
end | |
end | |
end | |
table = Tabulo::Table.new(unused_routes, border: :modern) | |
table.add_column(:first, header: 'Controller', styler: -> (cell_value, s) { cell_value ? Rainbow(s).purple : Rainbow(s).red }) | |
table.add_column('Unused Actions', styler: -> (cell_value, s) { cell_value ? Rainbow(s).red : Rainbo(s).green }) { |k, v| v.join(' | ') } | |
puts table.pack |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment