lib/chusaku/routes.rb in chusaku-0.3.2 vs lib/chusaku/routes.rb in chusaku-0.4.0

- old
+ new

@@ -2,10 +2,12 @@ module Chusaku # Handles extracting information about the Rails project's routes. class Routes class << self + # Primary method to call. + # # Example output: # # { # 'users' => { # 'edit' => [ @@ -21,62 +23,82 @@ # { verb: 'POST', path: '/empanadas', name: nil } # ] # } # } # - # @return {Hash} - Routes hash + # @return [Hash] Routes hash def call routes = {} Rails.application.routes.routes.each do |route| - controller, action = extract_controller_and_action_from(route) + controller, action, defaults = extract_data_from(route) routes[controller] ||= {} routes[controller][action] ||= [] - verbs_for(route).each do |verb| - routes[controller][action].push(format(route: route, verb: verb)) - routes[controller][action].uniq! - end + add_info_for \ + route: route, + routes: routes, + controller: controller, + action: action, + defaults: defaults end backfill_routes(routes) end private + # Adds formatted route info for the given param combination. + # + # @param route [Hash] Route info + # @param routes [Hash] Collection of all route info + # @param controller [String] Controller key + # @param action [String] Action key + # @param defaults [Hash] Default parameters for route + # @return [void] + def add_info_for(route:, routes:, controller:, action:, defaults:) + verbs_for(route).each do |verb| + routes[controller][action] + .push(format(route: route, verb: verb, defaults: defaults)) + routes[controller][action].uniq! + end + end + # Extract the HTTP verbs for a Rails route. Required for older versions of # Rails that return regular expressions for a route verb which sometimes # contains multiple verbs. # - # @param {ActionDispatch::Journey::Route} route - Route given by Rails - # @return {Array<String>} - List of HTTP verbs for the given route + # @param route [ActionDispatch::Journey::Route] Route given by Rails + # @return [Array<String>] List of HTTP verbs for the given route def verbs_for(route) route_verb = route.verb.to_s - ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'].select do |verb| + %w[GET POST PUT PATCH DELETE].select do |verb| route_verb.include?(verb) end end # Formats information for a given route. # - # @param {ActionDispatch::Journey::Route} route - Route given by Rails - # @param {String} verb - HTTP verb - # @return {Hash} - { verb: String, path: String, name: String } - def format(route:, verb:) + # @param route [ActionDispatch::Journey::Route] Route given by Rails + # @param verb [String] HTTP verb + # @param defaults [Hash] Default parameters for route + # @return [Hash] { verb => String, path => String, name => String } + def format(route:, verb:, defaults:) { verb: verb, path: route.path.spec.to_s.gsub('(.:format)', ''), - name: route.name + name: route.name, + defaults: defaults } end # Given a routes hash, backfill entries that aren't already filled by # `Rails.application.routes`. # - # @param {Hash} routes - Routes hash generated by this class - # @return {Hash} - Backfilled routes hash + # @param routes [Hash] Routes hash generated by this class + # @return [Hash] Backfilled routes hash def backfill_routes(routes) paths = {} routes.each do |_controller, actions| actions.each do |_action, data| @@ -90,17 +112,17 @@ routes end # Given a route, extract the controller and action strings. # - # @param {ActionDispatch::Journey::Route} route - Route instance - # @return {Array<String>} - [String, String] - def extract_controller_and_action_from(route) - defaults = route.defaults - controller = defaults[:controller] - action = defaults[:action] + # @param route [ActionDispatch::Journey::Route] Route instance + # @return [Array<Object>] (String, String, Hash) + def extract_data_from(route) + defaults = route.defaults.dup + controller = defaults.delete(:controller) + action = defaults.delete(:action) - [controller, action] + [controller, action, defaults] end end end end