-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
180 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
require "inertia_rails" if defined?(Rails) | ||
if defined?(Rails) | ||
require "inertia_rails" | ||
require_relative "inertia_rails_contrib/engine" | ||
end | ||
|
||
require_relative "inertia_rails_contrib/version" | ||
require_relative "inertia_rails_contrib/configuration" | ||
|
||
module InertiaRailsContrib | ||
class << self | ||
def configuration | ||
@configuration ||= Configuration.new | ||
end | ||
|
||
def configure | ||
yield(configuration) | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# lib/inertia_rails_contrib/engine.rb | ||
module InertiaRailsContrib | ||
class Configuration | ||
attr_accessor :enable_inertia_ui_modal | ||
|
||
def initialize | ||
@enable_inertia_ui_modal = false | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module InertiaRailsContrib | ||
class Engine < ::Rails::Engine | ||
initializer "inertia_rails_contrib.inertia_ui_modal" do | ||
require_relative "inertia_ui_modal" if InertiaRailsContrib.configuration.enable_inertia_ui_modal | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "inertia_ui_modal/renderer" | ||
require_relative "inertia_ui_modal/redirect" | ||
require_relative "inertia_ui_modal/inertia_rails_patch" | ||
|
||
module InertiaRailsContrib | ||
module InertiaUIModal | ||
HEADER_BASE_URL = "X-InertiaUI-Modal-Base-Url" | ||
HEADER_USE_ROUTER = "X-InertiaUI-Modal-Use-Router" | ||
end | ||
end | ||
|
||
ActionController::Renderers.add :inertia_modal do |component, options| | ||
InertiaRailsContrib::InertiaUIModal::Renderer.new( | ||
component, | ||
self, | ||
request, | ||
response, | ||
method(:render), | ||
**options | ||
).render | ||
end | ||
|
||
ActiveSupport.on_load(:action_controller_base) do | ||
prepend ::InertiaRailsContrib::InertiaUIModal::Redirect | ||
end |
20 changes: 20 additions & 0 deletions
20
lib/inertia_rails_contrib/inertia_ui_modal/inertia_rails_patch.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
require "inertia_rails/renderer" | ||
|
||
module InertiaRailsContrib | ||
module InertiaUIModal | ||
module RendererPatch | ||
def page | ||
super.tap do |modal| | ||
if @request.env[:_inertiaui_modal] | ||
modal[:props][:_inertiaui_modal] = @request.env[:_inertiaui_modal] | ||
modal[:url] = modal[:props][:_inertiaui_modal][:url] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
InertiaRails::Renderer.prepend(InertiaRailsContrib::InertiaUIModal::RendererPatch) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module InertiaRailsContrib | ||
module InertiaUIModal | ||
module Redirect | ||
def redirect_back_or_to(fallback_location, allow_other_host: _allow_other_host, **options) | ||
inertia_modal_referer = request.headers[HEADER_BASE_URL] | ||
if inertia_modal_referer && (allow_other_host || _url_host_allowed?(inertia_modal_referer)) | ||
redirect_to inertia_modal_referer, allow_other_host: allow_other_host, **options | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# frozen_string_literal: true | ||
|
||
module InertiaRailsContrib | ||
module InertiaUIModal | ||
class Renderer | ||
def initialize(component, controller, request, response, render_method, base_url: nil, **options) | ||
@request = request | ||
@response = response | ||
@base_url = base_url | ||
@inertia_renderer = InertiaRails::Renderer.new(component, controller, request, response, render_method, **options) | ||
end | ||
|
||
def render | ||
if @request.headers[HEADER_USE_ROUTER] == "0" || base_url.blank? | ||
return @inertia_renderer.render | ||
end | ||
|
||
@request.env[:_inertiaui_modal] = @inertia_renderer.page.merge(baseUrl: base_url) | ||
|
||
render_base_url | ||
end | ||
|
||
def base_url | ||
@request.headers[HEADER_BASE_URL] || @base_url | ||
end | ||
|
||
def render_base_url | ||
original_env = Rack::MockRequest.env_for( | ||
base_url, | ||
method: @request.method, | ||
params: @request.params | ||
) | ||
@request.each_header do |k, v| | ||
original_env[k] ||= v | ||
end | ||
|
||
original_request = ActionDispatch::Request.new(original_env) | ||
|
||
path = ActionDispatch::Journey::Router::Utils.normalize_path(original_request.path_info) | ||
Rails.application.routes.recognize_path_with_request(original_request, path, {}) | ||
controller = original_request.controller_class.new | ||
controller.request = @request | ||
controller.response = @response | ||
controller.process(original_request.path_parameters[:action]) | ||
end | ||
end | ||
end | ||
end |