Created
May 18, 2017 08:13
-
-
Save indyarocks/2ff226e8b2e164d1073e454809764835 to your computer and use it in GitHub Desktop.
Snippet from ActionDispatch::Routing::Mapper from Rails 4.2.5.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActionDispatch | |
module Routing | |
class Mapper | |
URL_OPTIONS = [:protocol, :subdomain, :domain, :host, :port] | |
class Constraints | |
# code here | |
end | |
class Mapping | |
# code here | |
end | |
module Base | |
# Mount a Rack-based application to be used within the application. | |
# | |
# mount SomeRackApp, at: "some_route" | |
# | |
# Alternatively: | |
# | |
# mount(SomeRackApp => "some_route") | |
# | |
# For options, see +match+, as +mount+ uses it internally. | |
# | |
# All mounted applications come with routing helpers to access them. | |
# These are named after the class specified, so for the above example | |
# the helper is either +some_rack_app_path+ or +some_rack_app_url+. | |
# To customize this helper's name, use the +:as+ option: | |
# | |
# mount(SomeRackApp => "some_route", as: "exciting") | |
# | |
# This will generate the +exciting_path+ and +exciting_url+ helpers | |
# which can be used to navigate to this mounted app. | |
def mount(app, options = nil) | |
if options | |
path = options.delete(:at) | |
else | |
unless Hash === app | |
raise ArgumentError, "must be called with mount point" | |
end | |
options = app | |
app, path = options.find { |k, _| k.respond_to?(:call) } | |
options.delete(app) if app | |
end | |
raise "A rack application must be specified" unless path | |
rails_app = rails_app? app | |
options[:as] ||= app_name(app, rails_app) | |
target_as = name_for_action(options[:as], path) | |
options[:via] ||= :all | |
match(path, options.merge(:to => app, :anchor => false, :format => false)) | |
define_generate_prefix(app, target_as) if rails_app | |
self | |
end | |
private | |
def rails_app?(app) | |
app.is_a?(Class) && app < Rails::Railtie | |
end | |
def app_name(app, rails_app) | |
if rails_app | |
app.railtie_name | |
elsif app.is_a?(Class) | |
class_name = app.name | |
ActiveSupport::Inflector.underscore(class_name).tr("/", "_") | |
end | |
end | |
def define_generate_prefix(app, name) | |
_route = @set.named_routes.get name | |
_routes = @set | |
app.routes.define_mounted_helper(name) | |
app.routes.extend Module.new { | |
def optimize_routes_generation?; false; end | |
define_method :find_script_name do |options| | |
if options.key? :script_name | |
super(options) | |
else | |
prefix_options = options.slice(*_route.segment_keys) | |
prefix_options[:relative_url_root] = ''.freeze | |
# we must actually delete prefix segment keys to avoid passing them to next url_for | |
_route.segment_keys.each { |k| options.delete(k) } | |
_routes.url_helpers.send("#{name}_path", prefix_options) | |
end | |
end | |
} | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment