aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/commands/routes/routes_command.rb
blob: c4f3717095285bf92720b5c5eb3b77da7c5b7859 (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
48
49
# 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"

        all_routes = Rails.application.routes.routes
        inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)

        if options.has_key?("expanded_format")
          say inspector.format(ActionDispatch::Routing::ConsoleFormatter::Expanded.new, routes_filter)
        else
          say inspector.format(ActionDispatch::Routing::ConsoleFormatter::Sheet.new, routes_filter)
        end
      end

      private

        def routes_filter
          if options.has_key?("controller")
            { controller: options["controller"] }
          elsif options.has_key?("grep_pattern")
            options["grep_pattern"]
          else
            nil
          end
        end
    end
  end
end