Skip to content

Commit 602394f

Browse files
authored
Merge pull request #164 from adrianpacala/refactor-monkey-patches
Avoid monkey patching
2 parents bbb2717 + 0a0c4e7 commit 602394f

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

lib/patches/better_errors.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
# https://github.com/BetterErrors/better_errors/blob/v2.5.1/lib/better_errors/middleware.rb
55
#
66

7-
if defined?(BetterErrors)
8-
BetterErrors::Middleware.class_eval do
9-
prepend(InertiaBetterErrors = Module.new do
10-
def text?(env)
11-
return false if env["HTTP_X_INERTIA"]
7+
module InertiaRails
8+
module InertiaBetterErrors
9+
def text?(env)
10+
return false if env["HTTP_X_INERTIA"]
1211

13-
super
14-
end
15-
end)
12+
super
13+
end
1614
end
1715
end
16+
17+
if defined?(BetterErrors)
18+
BetterErrors::Middleware.include InertiaRails::InertiaBetterErrors
19+
end

lib/patches/debug_exceptions/patch-5-0.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
# https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
55
#
66

7-
ActionDispatch::DebugExceptions.class_eval do
8-
prepend(InertiaDebugExceptions = Module.new do
7+
module InertiaRails
8+
module InertiaDebugExceptions
99
def render_for_default_application(request, wrapper)
1010
template = create_template(request, wrapper)
1111
file = "rescues/#{wrapper.rescue_template}"
@@ -19,5 +19,9 @@ def render_for_default_application(request, wrapper)
1919
end
2020
render(wrapper.status_code, body, format)
2121
end
22-
end)
22+
end
23+
end
24+
25+
if defined?(ActionDispatch::DebugExceptions)
26+
ActionDispatch::DebugExceptions.prepend InertiaRails::InertiaDebugExceptions
2327
end

lib/patches/debug_exceptions/patch-5-1.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
# https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
77
#
88

9-
ActionDispatch::DebugExceptions.class_eval do
10-
prepend(InertiaDebugExceptions = Module.new do
9+
module InertiaRails
10+
module InertiaDebugExceptions
1111
def render_for_browser_request(request, wrapper)
1212
template = create_template(request, wrapper)
1313
file = "rescues/#{wrapper.rescue_template}"
@@ -22,5 +22,9 @@ def render_for_browser_request(request, wrapper)
2222

2323
render(wrapper.status_code, body, format)
2424
end
25-
end)
25+
end
26+
end
27+
28+
if defined?(ActionDispatch::DebugExceptions)
29+
ActionDispatch::DebugExceptions.prepend InertiaRails::InertiaDebugExceptions
2630
end

lib/patches/mapper.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
ActionDispatch::Routing::Mapper.class_eval do
2-
def inertia(args, &block)
3-
route = args.keys.first
4-
component = args.values.first
1+
module InertiaRails
2+
module InertiaMapper
3+
def inertia(args, &block)
4+
route = args.keys.first
5+
component = args.values.first
56

6-
get(route => 'inertia_rails/static#static', defaults: {component: component})
7+
get(route => 'inertia_rails/static#static', defaults: { component: component })
8+
end
79
end
810
end
11+
12+
ActionDispatch::Routing::Mapper.include InertiaRails::InertiaMapper

lib/patches/request.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
ActionDispatch::Request.class_eval do
2-
def inertia?
3-
key? 'HTTP_X_INERTIA'
4-
end
1+
module InertiaRails
2+
module InertiaRequest
3+
def inertia?
4+
key? 'HTTP_X_INERTIA'
5+
end
56

6-
def inertia_partial?
7-
key?('HTTP_X_INERTIA_PARTIAL_COMPONENT')
7+
def inertia_partial?
8+
key?('HTTP_X_INERTIA_PARTIAL_COMPONENT')
9+
end
810
end
911
end
12+
13+
ActionDispatch::Request.include InertiaRails::InertiaRequest

0 commit comments

Comments
 (0)