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.
This commit is contained in:
Jon Moss 2016-02-13 22:18:22 -05:00
parent e729949787
commit cd8bb8b6ce
6 changed files with 36 additions and 10 deletions

@ -47,8 +47,8 @@ class Railtie < Rails::Railtie # :nodoc:
if options.show_previews
app.routes.prepend do
get '/rails/mailers' => "rails/mailers#index"
get '/rails/mailers/*path' => "rails/mailers#preview"
get '/rails/mailers' => "rails/mailers#index", internal: true
get '/rails/mailers/*path' => "rails/mailers#preview", internal: true
end
end

@ -3,7 +3,7 @@ module Journey # :nodoc:
class Route # :nodoc:
attr_reader :app, :path, :defaults, :name, :precedence
attr_reader :constraints
attr_reader :constraints, :internal
alias :conditions :constraints
module VerbMatchers
@ -55,7 +55,7 @@ def self.build(name, app, path, constraints, required_defaults, defaults)
##
# +path+ is a path constraint.
# +constraints+ is a hash of constraints to be applied to this route.
def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence)
def initialize(name, app, path, constraints, required_defaults, defaults, request_method_match, precedence, internal = false)
@name = name
@app = app
@path = path
@ -70,6 +70,7 @@ def initialize(name, app, path, constraints, required_defaults, defaults, reques
@decorated_ast = nil
@precedence = precedence
@path_formatter = @path.build_formatter
@internal = internal
end
def ast

@ -41,7 +41,7 @@ def action
end
def internal?
controller.to_s =~ %r{\Arails/(info|mailers|welcome)}
internal
end
def engine?

@ -107,6 +107,7 @@ def initialize(set, ast, defaults, controller, default_action, modyoule, to, for
@ast = ast
@anchor = anchor
@via = via
@internal = options[:internal]
path_params = ast.find_all(&:symbol?).map(&:to_sym)
@ -148,7 +149,8 @@ def make_route(name, precedence)
required_defaults,
defaults,
request_method,
precedence)
precedence,
@internal)
route
end

@ -389,6 +389,29 @@ def test_no_routes_were_defined
], output
end
def test_displaying_routes_for_internal_engines
engine = Class.new(Rails::Engine) do
def self.inspect
"Blog::Engine"
end
end
engine.routes.draw do
get '/cart', to: 'cart#show'
post '/cart', to: 'cart#create'
patch '/cart', to: 'cart#update'
end
output = draw do
get '/custom/assets', to: 'custom_assets#show'
mount engine => "/blog", as: "blog", internal: true
end
assert_equal [
" Prefix Verb URI Pattern Controller#Action",
"custom_assets GET /custom/assets(.:format) custom_assets#show",
], output
end
end
end
end

@ -22,10 +22,10 @@ module Finisher
initializer :add_builtin_route do |app|
if Rails.env.development?
app.routes.append do
get '/rails/info/properties' => "rails/info#properties"
get '/rails/info/routes' => "rails/info#routes"
get '/rails/info' => "rails/info#index"
get '/' => "rails/welcome#index"
get '/rails/info/properties' => "rails/info#properties", internal: true
get '/rails/info/routes' => "rails/info#routes", internal: true
get '/rails/info' => "rails/info#index", internal: true
get '/' => "rails/welcome#index", internal: true
end
end
end