5
5
require "uri"
6
6
require "active_support/core_ext/hash/indifferent_access"
7
7
require "active_support/core_ext/string/access"
8
+ require "active_support/core_ext/module/redefine_method"
8
9
require "action_controller/metal/exceptions"
9
10
10
11
module ActionDispatch
@@ -20,38 +21,43 @@ module WithIntegrationRouting # :nodoc:
20
21
module ClassMethods
21
22
def with_routing ( &block )
22
23
old_routes = nil
24
+ old_routes_call_method = nil
23
25
old_integration_session = nil
24
26
25
27
setup do
26
28
old_routes = app . routes
29
+ old_routes_call_method = old_routes . method ( :call )
27
30
old_integration_session = integration_session
28
31
create_routes ( &block )
29
32
end
30
33
31
34
teardown do
32
- reset_routes ( old_routes , old_integration_session )
35
+ reset_routes ( old_routes , old_routes_call_method , old_integration_session )
33
36
end
34
37
end
35
38
end
36
39
37
40
def with_routing ( &block )
38
41
old_routes = app . routes
42
+ old_routes_call_method = old_routes . method ( :call )
39
43
old_integration_session = integration_session
40
44
create_routes ( &block )
41
45
ensure
42
- reset_routes ( old_routes , old_integration_session )
46
+ reset_routes ( old_routes , old_routes_call_method , old_integration_session )
43
47
end
44
48
45
49
private
46
50
def create_routes
47
51
app = self . app
48
52
routes = ActionDispatch ::Routing ::RouteSet . new
49
- rack_app = app . config . middleware . build ( routes )
53
+
54
+ @original_routes ||= app . routes
55
+ @original_routes . singleton_class . redefine_method ( :call , &routes . method ( :call ) )
56
+
50
57
https = integration_session . https?
51
58
host = integration_session . host
52
59
53
60
app . instance_variable_set ( :@routes , routes )
54
- app . instance_variable_set ( :@app , rack_app )
55
61
@integration_session = Class . new ( ActionDispatch ::Integration ::Session ) do
56
62
include app . routes . url_helpers
57
63
include app . routes . mounted_helpers
@@ -63,11 +69,9 @@ def create_routes
63
69
yield routes
64
70
end
65
71
66
- def reset_routes ( old_routes , old_integration_session )
67
- old_rack_app = app . config . middleware . build ( old_routes )
68
-
72
+ def reset_routes ( old_routes , old_routes_call_method , old_integration_session )
69
73
app . instance_variable_set ( :@routes , old_routes )
70
- app . instance_variable_set ( :@app , old_rack_app )
74
+ @original_routes . singleton_class . redefine_method ( :call , & old_routes_call_method )
71
75
@integration_session = old_integration_session
72
76
@routes = old_routes
73
77
end
0 commit comments