Last active
April 29, 2021 17:54
-
-
Save vitobotta/e73063d98543e29385b18998b2a795ae to your computer and use it in GitHub Desktop.
StimulusReflex working with route constraints
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
# frozen_string_literal: true | |
class StimulusReflex::Reflex | |
... | |
def request | |
@request ||= begin | |
uri = URI.parse(url) | |
path = ActionDispatch::Journey::Router::Utils.normalize_path(uri.path) | |
query_hash = Rack::Utils.parse_nested_query(uri.query) | |
origin = connection.env["HTTP_ORIGIN"] | |
hostname = URI.parse(origin).host | |
env = connection.env.merge( | |
Rack::MockRequest.env_for(uri.to_s).merge( | |
"HTTP_COOKIE" => connection.env["HTTP_COOKIE"], | |
"HTTP_HOST" => hostname, | |
"SERVER_NAME" => hostname, | |
"REMOTE_ADDR" => connection.env["REMOTE_ADDR"], | |
"HTTP_X_FORWARDED_FOR" => connection.env["HTTP_X_FORWARDED_FOR"], | |
"rack.request.query_hash" => query_hash, | |
"rack.request.query_string" => uri.query, | |
"ORIGINAL_SCRIPT_NAME" => "", | |
"ORIGINAL_FULLPATH" => path, | |
Rack::SCRIPT_NAME => "", | |
Rack::PATH_INFO => path, | |
Rack::REQUEST_PATH => path, | |
Rack::QUERY_STRING => uri.query, | |
) | |
) | |
req = Rails.application.routes.send(:make_request, env) | |
path_params = Rails.application.routes.recognize_path_with_request(req, path, {}) | |
req = ActionDispatch::Request.new( | |
env.merge( | |
ActionDispatch::Http::Parameters::PARAMETERS_KEY => path_params | |
) | |
) | |
req.session = connection.request.session.rack_session | |
req.session.send :load! | |
req | |
end | |
end | |
... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment