Skip to content

Commit 32a2506

Browse files
authored
feat: support multiple inertia apps by allowing different layouts (#86)
* feat: support multiple inertia apps by allowing different layouts * doc: document the intent for `inertia_layout`
1 parent 0a1ed06 commit 32a2506

File tree

10 files changed

+48
-6
lines changed

10 files changed

+48
-6
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ InertiaRails.configure do |config|
7373
# set the current version for automatic asset refreshing. A string value should be used if any.
7474
config.version = nil
7575

76-
# set the layout you want inertia components to be rendered within. This layout must include any required inertia javascript.
77-
config.layout = 'application'
78-
7976
# ssr specific options
8077
config.ssr_enabled = false
8178
config.ssr_url = 'http://localhost:13714'

inertia_rails.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ Gem::Specification.new do |spec|
3434
spec.add_development_dependency "sqlite3"
3535
spec.add_development_dependency "appraisal"
3636
spec.add_development_dependency "responders"
37+
spec.add_development_dependency "debug"
3738
end

lib/inertia_rails/controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ def redirect_back(fallback_location:, allow_other_host: true, **options)
3838

3939
private
4040

41+
def inertia_layout
42+
layout = ::InertiaRails.layout
43+
44+
# When the global configuration is not set, let Rails decide which layout
45+
# should be used based on the controller configuration.
46+
layout.nil? ? true : layout
47+
end
48+
4149
def inertia_location(url)
4250
headers['X-Inertia-Location'] = url
4351
head :conflict

lib/inertia_rails/inertia_rails.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def self.lazy(value = nil, &block)
6262
private
6363

6464
module Configuration
65-
mattr_accessor(:layout) { 'application' }
65+
mattr_accessor(:layout) { nil }
6666
mattr_accessor(:version) { nil }
6767
mattr_accessor(:ssr_enabled) { false }
6868
mattr_accessor(:ssr_url) { 'http://localhost:13714' }

lib/inertia_rails/renderer.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def render
2323
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
2424
else
2525
return render_ssr if ::InertiaRails.ssr_enabled? rescue nil
26-
@render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
26+
@render_method.call template: 'inertia', layout: layout, locals: (view_data).merge({page: page})
2727
end
2828
end
2929

@@ -34,7 +34,11 @@ def render_ssr
3434
res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
3535

3636
::InertiaRails.html_headers = res['head']
37-
@render_method.call html: res['body'].html_safe, layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
37+
@render_method.call html: res['body'].html_safe, layout: layout, locals: (view_data).merge({page: page})
38+
end
39+
40+
def layout
41+
@controller.send(:inertia_layout)
3842
end
3943

4044
def props

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
class InertiaTestController < ApplicationController
2+
layout 'conditional', only: [:with_different_layout]
3+
24
def empty_test
35
render inertia: 'EmptyTestComponent'
46
end
57

8+
def with_different_layout
9+
render inertia: 'EmptyTestComponent'
10+
end
11+
612
def redirect_test
713
redirect_to :empty_test
814
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<h1>Conditional layout specified by controller</h1>
2+
<%= yield %>

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
get 'share' => 'inertia_share_test#share'
88
get 'share_with_inherited' => 'inertia_child_share_test#share_with_inherited'
99
get 'empty_test' => 'inertia_test#empty_test'
10+
get 'with_different_layout' => 'inertia_test#with_different_layout'
1011
get 'redirect_test' => 'inertia_test#redirect_test'
1112
get 'inertia_request_test' => 'inertia_test#inertia_request_test'
1213
get 'inertia_partial_request_test' => 'inertia_test#inertia_partial_request_test'

spec/inertia/configuration_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,27 @@
9898
it { is_expected.to render_template 'testing' }
9999
it { is_expected.not_to render_template 'application' }
100100
end
101+
102+
context 'opting out of a different layout for Inertia' do
103+
before do
104+
InertiaRails.configure {|c| c.layout = nil }
105+
end
106+
107+
it 'uses default layout for controller' do
108+
get empty_test_path
109+
is_expected.to render_template 'inertia'
110+
is_expected.to render_template 'application'
111+
is_expected.not_to render_template 'testing'
112+
end
113+
114+
it 'applies conditional layouts as needed' do
115+
get with_different_layout_path
116+
is_expected.to render_template 'inertia'
117+
is_expected.to render_template 'conditional'
118+
is_expected.not_to render_template 'application'
119+
is_expected.not_to render_template 'testing'
120+
end
121+
end
101122
end
102123
end
103124
end

spec/rails_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
require File.expand_path('../dummy/config/environment', __FILE__)
55

6+
# Allow using `debugger` to debug failing tests.
7+
require 'debug'
68

79
# Prevent database truncation if the environment is production
810
abort("The Rails environment is running in production mode!") if Rails.env.production?

0 commit comments

Comments
 (0)