Created
September 10, 2009 21:39
-
-
Save aiwilliams/184848 to your computer and use it in GitHub Desktop.
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
# Modified from http://www.elctech.com/articles/rake-routes-review-repeat | |
namespace :routes do | |
desc 'Brief listing of routes info plus matches to app controller methods' | |
task :by_controller => :environment do | |
task_setup "Attempting to match routes controller methods to app controller methods" | |
methods = find_routes_controller_methods_matched_to_app_controller_methods(@pattern) | |
index_width, controller_width, action_width, path_width, method_width, segment_width, app_controller_action = get_array_elements_max_width(methods) | |
last_controller = '' | |
methods.each do |index, controller, action, path, method, segment, app_controller_action| | |
@methods_count += 1 | |
@missing_methods_count += 1 unless app_controller_action | |
puts "\n\nCONTROLLER: #{controller.ljust(controller_width).strip}\n" unless last_controller == controller | |
puts " #{(app_controller_action ? green(action) : yellow("*#{action}")).ljust(action_width+colorize_width)} #{method.ljust(method_width)} #{path.ljust(path_width)}" | |
last_controller = controller | |
end | |
display_counts | |
end | |
def get_array_elements_max_width(array) | |
array.first.enum_with_index.map {|element,idx| array.map {|element| element[idx]}.map {|n| n ? n.length : 0}.max} | |
end | |
def list_directories(directory, pattern) | |
result = [] | |
Dir.glob("#{directory}/*") do |file| | |
next if file[0] == ?. | |
if File.directory? file | |
result.push(*list_directories(file, pattern)) | |
elsif file =~ pattern | |
result << file | |
end | |
end | |
result | |
end | |
def find_routes_controller_actions(pattern='') | |
routes = [] | |
ActionController::Routing::Routes.routes.each do |route| | |
path = ActionController::Routing::Routes.named_routes.routes.index(route).to_s | |
method = route.conditions[:method].blank? ? 'GET' : route.conditions[:method].to_s.upcase | |
segment = route.segments.inject("") { |str,s| str << s.to_s } | |
segment.chop! if segment.length > 1 | |
controller = route.requirements.empty? ? "" : route.requirements[:controller] | |
action = route.requirements.empty? ? "" : route.requirements[:action] | |
route.requirements.delete :controller | |
route.requirements.delete :action | |
if controller =~ /#{@pattern}/ | |
unless routes.assoc "#{controller}:#{action}" | |
routes << ["#{controller}:#{action}", controller, action, path, method, segment] unless controller.blank? | |
end | |
end | |
end | |
routes | |
end | |
def find_app_controller_actions(pattern='') | |
all_controller_actions = [] | |
controllers = list_directories "#{RAILS_ROOT}/app/controllers", /_controller.rb$/ | |
controllers.each {|a| a.gsub!("#{RAILS_ROOT}/app/controllers/",''); a.gsub!('.rb','')} | |
controllers.each do |controller_path| | |
if controller_path =~ /#{@pattern}/ | |
controller_actions = [] | |
controller_name = controller_path.camelize.gsub(".rb","") | |
controller_class = eval(controller_name) | |
(controller_class.public_instance_methods - | |
ApplicationController.public_instance_methods - | |
Object.methods).sort.each {|method| controller_actions << method} | |
controller_class.view_paths.each do |e| | |
controller_view_path = Rails.root.join(e, controller_path.sub(/_controller$/, ''), '*') | |
Dir[controller_view_path].each do |template| | |
action_name = File.basename(template) | |
action_name.sub!(/\..*$/, '') | |
if action_name !~ /^_/ && !controller_actions.include?(action_name) | |
controller_actions << action_name | |
end | |
end | |
end | |
controller_actions.each do |e| | |
all_controller_actions << ["#{controller_name.underscore.gsub!("_controller",'')}:#{e}"] | |
end | |
end | |
end | |
all_controller_actions | |
end | |
def find_routes_controller_methods_matched_to_app_controller_methods(pattern='') | |
controller_actions = find_app_controller_actions(@pattern) | |
find_routes_controller_actions(@pattern).map! {|p| p << controller_actions.find {|i| i[0] == p[0]}} | |
end | |
def task_setup(title) | |
@pattern = ENV['pattern'] ? ENV['pattern'].downcase : '' | |
@methods_count = 0 | |
@missing_methods_count = 0 | |
puts "\n#{title}\n\n" | |
puts "Match controllers on pattern =~ /#{@pattern}/\n\n" | |
puts green "Controller methods implemented" | |
puts yellow "*Controller methods not implemented" | |
end | |
def display_counts | |
puts "\n\nNumber of methods: #{@methods_count}" | |
puts green "Controller methods implemented #{@methods_count - @missing_methods_count}" | |
puts yellow "*Controller methods not implemented #{@missing_methods_count}" | |
end | |
def colorize(text, color_code) | |
"#{color_code}#{text}\e[0m" | |
end | |
def colorize_width; 10;end | |
def red(text); colorize(text, "\e[31m"); end | |
def green(text); colorize(text, "\e[32m"); end | |
def yellow(text); colorize(text, "\e[33m"); end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment