Skip to content

Commit 20e9671

Browse files
committed
refactor: use configuration from controller for ssr
1 parent c5624be commit 20e9671

File tree

6 files changed

+14
-43
lines changed

6 files changed

+14
-43
lines changed

lib/inertia_rails/configuration.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ module InertiaRails
44
class Configuration
55
DEFAULTS = {
66
default_render: false,
7-
layout: nil,
7+
8+
# Let Rails decide which layout should be used based on the controller configuration.
9+
layout: true,
10+
811
deep_merge_shared_data: false,
9-
merge_props_with: ->(shared_data, props) { shared_data.merge(props) },
1012
ssr_enabled: false,
1113
ssr_url: 'http://localhost:13714',
1214
version: nil,

lib/inertia_rails/controller.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def use_inertia_instance_props
3434
end
3535

3636
def default_render
37-
if InertiaRails.default_render?
37+
if inertia_configuration.default_render
3838
render(inertia: true)
3939
else
4040
super
@@ -66,14 +66,6 @@ def inertia_configuration
6666
::InertiaRails.configuration
6767
end
6868

69-
def inertia_layout
70-
layout = ::InertiaRails.layout
71-
72-
# When the global configuration is not set, let Rails decide which layout
73-
# should be used based on the controller configuration.
74-
layout.nil? ? true : layout
75-
end
76-
7769
def inertia_location(url)
7870
headers['X-Inertia-Location'] = url
7971
head :conflict

lib/inertia_rails/inertia_rails.rb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,10 @@ def self.shared_data(controller)
2424
merge!(evaluated_blocks(controller, shared_blocks))
2525
end
2626

27-
def self.version
28-
configuration.version
29-
end
30-
31-
def self.layout
32-
configuration.layout
33-
end
34-
35-
def self.ssr_enabled?
36-
configuration.ssr_enabled
37-
end
38-
39-
def self.ssr_url
40-
configuration.ssr_url
41-
end
42-
43-
def self.default_render?
44-
configuration.default_render
45-
end
46-
4727
def self.html_headers
4828
self.threadsafe_html_headers || []
4929
end
5030

51-
def self.deep_merge_shared_data?
52-
configuration.deep_merge_shared_data
53-
end
54-
5531
# "Setters"
5632
def self.share(**args)
5733
self.shared_plain_data = self.shared_plain_data.merge(args)

lib/inertia_rails/renderer.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
module InertiaRails
66
class Renderer
7-
attr_reader :component, :view_data
7+
attr_reader :component, :view_data, :configuration
88

99
def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil)
1010
@component = component.is_a?(TrueClass) ? "#{controller.controller_path}/#{controller.action_name}" : component
1111
@controller = controller
12+
@configuration = controller.send(:inertia_configuration)
1213
@request = request
1314
@response = response
1415
@render_method = render_method
1516
@props = props ? props : controller.inertia_view_assigns
1617
@view_data = view_data || {}
17-
@deep_merge = !deep_merge.nil? ? deep_merge : InertiaRails.deep_merge_shared_data?
18+
@deep_merge = !deep_merge.nil? ? deep_merge : configuration.deep_merge_shared_data
1819
end
1920

2021
def render
@@ -23,23 +24,23 @@ def render
2324
@response.set_header('X-Inertia', 'true')
2425
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
2526
else
26-
return render_ssr if ::InertiaRails.ssr_enabled? rescue nil
27+
return render_ssr if configuration.ssr_enabled rescue nil
2728
@render_method.call template: 'inertia', layout: layout, locals: (view_data).merge({page: page})
2829
end
2930
end
3031

3132
private
3233

3334
def render_ssr
34-
uri = URI("#{::InertiaRails.ssr_url}/render")
35+
uri = URI("#{configuration.ssr_url}/render")
3536
res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
3637

3738
::InertiaRails.html_headers = res['head']
3839
@render_method.call html: res['body'].html_safe, layout: layout, locals: (view_data).merge({page: page})
3940
end
4041

4142
def layout
42-
@controller.send(:inertia_layout)
43+
configuration.layout
4344
end
4445

4546
def computed_props
@@ -69,7 +70,7 @@ def page
6970
component: component,
7071
props: computed_props,
7172
url: @request.original_fullpath,
72-
version: ::InertiaRails.version,
73+
version: configuration.version,
7374
}
7475
end
7576

spec/inertia/configuration_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101

102102
context 'opting out of a different layout for Inertia' do
103103
before do
104-
InertiaRails.configure {|c| c.layout = nil }
104+
InertiaRails.configure {|c| c.layout = true }
105105
end
106106

107107
it 'uses default layout for controller' do

spec/inertia/rendering_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RSpec.describe 'rendering inertia views', type: :request do
22
subject { response.body }
33

4-
let(:controller) { double('Controller', inertia_view_assigns: {})}
4+
let(:controller) { double('Controller', inertia_view_assigns: {}, inertia_configuration: InertiaRails.configuration)}
55

66
context 'first load' do
77
let(:page) { InertiaRails::Renderer.new('TestComponent', controller, request, response, '').send(:page) }

0 commit comments

Comments
 (0)