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

      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 ""
        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)

        say inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, routes_filter)
      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