Skip to content

Commit cd8bb8b

Browse files
committed
Add internal attribute to routes
This is meant to provide a way for Action Cable, Sprockets, and possibly other Rack applications to mark themselves as internal, and to exclude themselves from the routing inspector, and thus `rails routes` / `rake routes`. I think this is the only way to have mounted Rack apps be marked as internal, within AD/Journey. Another option would be to create an array of regexes for internal apps, and then to iterate over that everytime a request comes through. Also, I only had the first `add_route` method set `internal`'s default to false, to avoid littering it all over the codebase.
1 parent e729949 commit cd8bb8b

File tree

6 files changed

+36
-10
lines changed

6 files changed

+36
-10
lines changed

actionmailer/lib/action_mailer/railtie.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ class Railtie < Rails::Railtie # :nodoc:
4747

4848
if options.show_previews
4949
app.routes.prepend do
50-
get '/rails/mailers' => "rails/mailers#index"
51-
get '/rails/mailers/*path' => "rails/mailers#preview"
50+
get '/rails/mailers' => "rails/mailers#index", internal: true
51+
get '/rails/mailers/*path' => "rails/mailers#preview", internal: true
5252
end
5353
end
5454

actionpack/lib/action_dispatch/journey/route.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Journey # :nodoc:
33
class Route # :nodoc:
44
attr_reader :app, :path, :defaults, :name, :precedence
55

6-
attr_reader :constraints
6+
attr_reader :constraints, :internal
77
alias :conditions :constraints
88

99
module VerbMatchers
@@ -55,7 +55,7 @@ def self.build(name, app, path, constraints, required_defaults, defaults)
5555
##
5656
# +path+ is a path constraint.
5757
# +constraints+ is a hash of constraints to be applied to this route.
58-
def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence)
58+
def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, internal = false)
5959
@name = name
6060
@app = app
6161
@path = path
@@ -70,6 +70,7 @@ def initialize(name, app, path, constraints, required_defaults, defaults, reques
7070
@decorated_ast = nil
7171
@precedence = precedence
7272
@path_formatter = @path.build_formatter
73+
@internal = internal
7374
end
7475

7576
def ast

actionpack/lib/action_dispatch/routing/inspector.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def action
4141
end
4242

4343
def internal?
44-
controller.to_s =~ %r{\Arails/(info|mailers|welcome)}
44+
internal
4545
end
4646

4747
def engine?

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def initialize(set, ast, defaults, controller, default_action, modyoule, to, for
107107
@ast = ast
108108
@anchor = anchor
109109
@via = via
110+
@internal = options[:internal]
110111

111112
path_params = ast.find_all(&:symbol?).map(&:to_sym)
112113

@@ -148,7 +149,8 @@ def make_route(name, precedence)
148149
required_defaults,
149150
defaults,
150151
request_method,
151-
precedence)
152+
precedence,
153+
@internal)
152154

153155
route
154156
end

actionpack/test/dispatch/routing/inspector_test.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,29 @@ def test_no_routes_were_defined
389389
], output
390390
end
391391

392+
def test_displaying_routes_for_internal_engines
393+
engine = Class.new(Rails::Engine) do
394+
def self.inspect
395+
"Blog::Engine"
396+
end
397+
end
398+
engine.routes.draw do
399+
get '/cart', to: 'cart#show'
400+
post '/cart', to: 'cart#create'
401+
patch '/cart', to: 'cart#update'
402+
end
403+
404+
output = draw do
405+
get '/custom/assets', to: 'custom_assets#show'
406+
mount engine => "/blog", as: "blog", internal: true
407+
end
408+
409+
assert_equal [
410+
" Prefix Verb URI Pattern Controller#Action",
411+
"custom_assets GET /custom/assets(.:format) custom_assets#show",
412+
], output
413+
end
414+
392415
end
393416
end
394417
end

railties/lib/rails/application/finisher.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ module Finisher
2222
initializer :add_builtin_route do |app|
2323
if Rails.env.development?
2424
app.routes.append do
25-
get '/rails/info/properties' => "rails/info#properties"
26-
get '/rails/info/routes' => "rails/info#routes"
27-
get '/rails/info' => "rails/info#index"
28-
get '/' => "rails/welcome#index"
25+
get '/rails/info/properties' => "rails/info#properties", internal: true
26+
get '/rails/info/routes' => "rails/info#routes", internal: true
27+
get '/rails/info' => "rails/info#index", internal: true
28+
get '/' => "rails/welcome#index", internal: true
2929
end
3030
end
3131
end

0 commit comments

Comments
 (0)