class PryRails::FindRoute
Public Instance Methods
process(controller)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 16 def process(controller) controller_string = controller.to_s if single_action?(controller_string) single_action(controller_string) else all_actions(controller_string) end end
Private Instance Methods
all_actions(controller)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 31 def all_actions(controller) show_routes do |route| route.defaults[:controller].to_s =~ /#{normalize_controller_name(controller)}/ end end
controller_and_action_from(controller_and_action)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 37 def controller_and_action_from(controller_and_action) controller, action = controller_and_action.split("#") {controller: normalize_controller_name(controller), action: action} end
normalize_controller_name(controller)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 46 def normalize_controller_name(controller) controller.underscore.chomp('_controller') end
route_helper(name)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 69 def route_helper(name) name && "[#{name}]" end
routes()
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 42 def routes Rails.application.routes.routes end
show_routes(&block)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 50 def show_routes(&block) all_routes = routes.select(&block) if all_routes.any? grouped_routes = all_routes.group_by { |route| route.defaults[:controller] } result = grouped_routes.each_with_object("") do |(controller, routes), res| res << "Routes for " + text.bold(controller.to_s.camelize + "Controller") + "\n" res << "--\n" routes.each do |route| spec = route.path.is_a?(String) ? route.path : route.path.spec res << "#{route.defaults[:action]} #{text.bold(verb_for(route))} #{spec} #{route_helper(route.name)}" + "\n" end res << "\n" end stagger_output result else output.puts "No routes found." end end
single_action(controller)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 27 def single_action(controller) show_routes { |route| route.defaults == controller_and_action_from(controller) } end
single_action?(controller)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 77 def single_action?(controller) controller =~ /#/ end
verb_for(route)
click to toggle source
# File lib/pry-rails/commands/find_route.rb, line 73 def verb_for(route) %w(GET PUT POST PATCH DELETE).find { |v| route.verb === v } end