lib/chusaku/routes.rb in chusaku-0.1.3 vs lib/chusaku/routes.rb in chusaku-0.1.4

- old
+ new

@@ -6,60 +6,70 @@ # # Example output: # # { # 'users' => { - # 'edit' => { - # verbs: ['GET'], - # path: '/users/:id', - # name: 'edit_user' - # }, - # 'update' => { - # verbs: ['PUT', 'PATCH'], - # path: '/users', - # name: nil - # } + # 'edit' => [ + # { verb: ['GET'], path: '/users/:id', name: ['edit_user'] }], + # 'update' => [ + # { verb: ['PUT', 'PATCH'], + # path: '/users', + # name: ['edit_user', 'edit_user2'] }] # }, # 'empanadas' => { - # 'create' => { - # verbs: ['POST'], - # path: '/empanadas', - # name: nil - # } + # 'create' => [ + # { verb: 'POST', path: '/empanadas', name: nil }] # } # } # - # @return {Hash} + # @return {Hash} Routes hash def self.call routes = {} Rails.application.routes.routes.each do |route| defaults = route.defaults controller = defaults[:controller] action = defaults[:action] routes[controller] ||= {} - if routes[controller][action].nil? - routes[controller][action] = format_action(route) - else - routes[controller][action][:verbs].push(route.verb) - end + routes[controller][action] ||= [] + routes[controller][action].push(format_route(route)) end - routes + backfill_routes(routes) end private # Extract information of a given route. # - # @param {ActionDispatch::Journey::Route} route - # @return {Hash} - def self.format_action(route) + # @param {ActionDispatch::Journey::Route} route - Route given by Rails + # @return {Hash} Formatted hash for given route + def self.format_route(route) { - verbs: [route.verb], + verb: route.verb, path: route.path.spec.to_s.gsub('(.:format)', ''), name: route.name } + 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 + def self.backfill_routes(routes) + paths = {} + + routes.each do |_controller, actions| + actions.each do |action, data| + data.each do |datum| + paths[datum[:path]] ||= datum[:name] + datum[:name] ||= paths[datum[:path]] + end + end + end + + routes end end end