Sha256: 6cbdb6aaf693116b60317d55b067df5076decc747a4447a76f0fb83dd26e643e

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Chusaku
  module Routes
    # Extract information about the Rails project's routes.
    #
    # Example output:
    #
    #   {
    #     'users' => {
    #       'edit' => [
    #         { verb: ['GET'], path: '/users/:id', name: ['edit_user'] }],
    #       'update' => [
    #         { verb: ['PUT', 'PATCH'],
    #           path: '/users', 
    #           name: ['edit_user', 'edit_user2'] }]
    #     },
    #     'empanadas' => {
    #       'create' => [
    #         { verb: 'POST', path: '/empanadas', name: nil }]
    #     }
    #   }
    #
    # @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] ||= {}
        routes[controller][action] ||= []
        routes[controller][action].push(format_route(route))
      end

      backfill_routes(routes)
    end

    private

      # Extract information of a given route.
      #
      # @param {ActionDispatch::Journey::Route} route - Route given by Rails
      # @return {Hash} Formatted hash for given route
      def self.format_route(route)
        {
          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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chusaku-0.1.4 lib/chusaku/routes.rb