Last active
August 22, 2016 03:00
-
-
Save yasaichi/d5629f7bfbb2d562b52dd057b5b4ef38 to your computer and use it in GitHub Desktop.
This file contains 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 AppName::JourneyRouterPatch | |
APP_NAME_GENERIC_ROUTING_INFO_KEY = "app_name.generic_routing_info".freeze | |
def call(env) | |
response_array = super | |
# NOTE: When Journey::Router#call can't resolve a request, it returns an array like: | |
# [404, {'X-Cascade' => 'pass'}, ['Not Found']] | |
# ref. https://github.com/rails/journey/blob/v1.0.4/lib/journey/router.rb#L80 | |
if response_array[1]["X-Cascade"] != "pass" && env.key?(APP_NAME_GENERIC_ROUTING_INFO_KEY) | |
# do something | |
end | |
response_array | |
end | |
def find_routes(env) | |
routes = super | |
_, params, route = routes.first | |
# NOTE: When a path of the request (resolved by Generic routing) is something like | |
# :controller/:action(/:id)(.:format), add the path parameters into Rack middleware's env. | |
if like_generic_routing?(route) && params.key?(:controller) && params.key?(:action) | |
env[APP_NAME_GENERIC_ROUTING_INFO_KEY] = params.slice(:controller, :action) | |
end | |
routes | |
end | |
private | |
# NOTE: It can't tell the Generic routing from the requests for not existing pages. | |
def like_generic_routing?(route) | |
route && | |
route.verb == // && # return false if the routing is specified by get, post, and so on | |
route.name != "root" && # return false if the routing is specified with "root to:" | |
route.defaults.blank? && # return false if the routing is specified by match (not Generic routing) | |
# return false if the request is for assets/**/*.[css|js] | |
route.app.instance_of?(ActionDispatch::Routing::RouteSet::Dispatcher) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment