aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/routes/routes_command.rb
blob: 41162a44259906b18bd0b68d927cb7285c59d2cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true

require "rails/command"

module Rails
  module Command
    class RoutesCommand < Base # :nodoc:
      class_option :controller, aliases: "-c", type: :string, desc: "Specifies the controller."
      class_option :grep_pattern, aliases: "-g", type: :string, desc: "Specifies grep pattern."
      class_option :expanded_format, aliases: "--expanded", type: :string, desc: "Turn on expanded format mode."

      no_commands do
        def help
          say "Usage: Print out all defined routes in match order, with names."
          say ""
          say "Target specific controller with -c option, or grep routes using -g option"
          say "Use expanded format with --expanded option"
          say ""
        end
      end

      def perform(*)
        require_application_and_environment!
        require "action_dispatch/routing/inspector"

        say inspector.format(formatter, routes_filter)
      end

      private
        def inspector
          ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes)
        end

        def formatter
          if options.key?("expanded_format")
            ActionDispatch::Routing::ConsoleFormatter::Expanded.new
          else
            ActionDispatch::Routing::ConsoleFormatter::Sheet.new
          end
        end

        def routes_filter
          options.symbolize_keys.slice(:controller, :grep_pattern)
        end
    end
  end
end