Skip to content

Commit

Permalink
Merge pull request #164 from adrianpacala/refactor-monkey-patches
Browse files Browse the repository at this point in the history
Avoid monkey patching
  • Loading branch information
bknoles authored Nov 21, 2024
2 parents bbb2717 + 0a0c4e7 commit 602394f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
18 changes: 10 additions & 8 deletions lib/patches/better_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
# https://github.com/BetterErrors/better_errors/blob/v2.5.1/lib/better_errors/middleware.rb
#

if defined?(BetterErrors)
BetterErrors::Middleware.class_eval do
prepend(InertiaBetterErrors = Module.new do
def text?(env)
return false if env["HTTP_X_INERTIA"]
module InertiaRails
module InertiaBetterErrors
def text?(env)
return false if env["HTTP_X_INERTIA"]

super
end
end)
super
end
end
end

if defined?(BetterErrors)
BetterErrors::Middleware.include InertiaRails::InertiaBetterErrors
end
10 changes: 7 additions & 3 deletions lib/patches/debug_exceptions/patch-5-0.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
# https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
#

ActionDispatch::DebugExceptions.class_eval do
prepend(InertiaDebugExceptions = Module.new do
module InertiaRails
module InertiaDebugExceptions
def render_for_default_application(request, wrapper)
template = create_template(request, wrapper)
file = "rescues/#{wrapper.rescue_template}"
Expand All @@ -19,5 +19,9 @@ def render_for_default_application(request, wrapper)
end
render(wrapper.status_code, body, format)
end
end)
end
end

if defined?(ActionDispatch::DebugExceptions)
ActionDispatch::DebugExceptions.prepend InertiaRails::InertiaDebugExceptions
end
10 changes: 7 additions & 3 deletions lib/patches/debug_exceptions/patch-5-1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
# https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
#

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

render(wrapper.status_code, body, format)
end
end)
end
end

if defined?(ActionDispatch::DebugExceptions)
ActionDispatch::DebugExceptions.prepend InertiaRails::InertiaDebugExceptions
end
14 changes: 9 additions & 5 deletions lib/patches/mapper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
ActionDispatch::Routing::Mapper.class_eval do
def inertia(args, &block)
route = args.keys.first
component = args.values.first
module InertiaRails
module InertiaMapper
def inertia(args, &block)
route = args.keys.first
component = args.values.first

get(route => 'inertia_rails/static#static', defaults: {component: component})
get(route => 'inertia_rails/static#static', defaults: { component: component })
end
end
end

ActionDispatch::Routing::Mapper.include InertiaRails::InertiaMapper
16 changes: 10 additions & 6 deletions lib/patches/request.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
ActionDispatch::Request.class_eval do
def inertia?
key? 'HTTP_X_INERTIA'
end
module InertiaRails
module InertiaRequest
def inertia?
key? 'HTTP_X_INERTIA'
end

def inertia_partial?
key?('HTTP_X_INERTIA_PARTIAL_COMPONENT')
def inertia_partial?
key?('HTTP_X_INERTIA_PARTIAL_COMPONENT')
end
end
end

ActionDispatch::Request.include InertiaRails::InertiaRequest

0 comments on commit 602394f

Please sign in to comment.