Skip to content

Commit 967fa4a

Browse files
committed
refactor: pull shared data out of the controller
1 parent a153301 commit 967fa4a

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

lib/inertia_rails/controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,21 @@ def redirect_back(fallback_location:, allow_other_host: true, **options)
5353
)
5454
end
5555

56+
private
57+
5658
def inertia_view_assigns
5759
return {} unless @_inertia_instance_props
5860
view_assigns.except(*@_inertia_skip_props)
5961
end
6062

61-
private
62-
6363
def inertia_configuration
6464
::InertiaRails.configuration
6565
end
6666

67+
def inertia_shared_data
68+
::InertiaRails.shared_data(self)
69+
end
70+
6771
def inertia_location(url)
6872
headers['X-Inertia-Location'] = url
6973
head :conflict

lib/inertia_rails/renderer.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44

55
module InertiaRails
66
class Renderer
7-
attr_reader :component, :view_data, :configuration
7+
attr_reader(
8+
:component,
9+
:configuration,
10+
:controller,
11+
:props,
12+
:view_data,
13+
)
814

915
def initialize(component, controller, request, response, render_method, props: nil, view_data: nil, deep_merge: nil)
1016
@component = component.is_a?(TrueClass) ? "#{controller.controller_path}/#{controller.action_name}" : component
1117
@controller = controller
12-
@configuration = controller.send(:inertia_configuration)
18+
@configuration = controller.__send__(:inertia_configuration)
1319
@request = request
1420
@response = response
1521
@render_method = render_method
16-
@props = props ? props : controller.inertia_view_assigns
22+
@props = props || controller.__send__(:inertia_view_assigns)
1723
@view_data = view_data || {}
1824
@deep_merge = !deep_merge.nil? ? deep_merge : configuration.deep_merge_shared_data
1925
end
@@ -25,7 +31,7 @@ def render
2531
@render_method.call json: page, status: @response.status, content_type: Mime[:json]
2632
else
2733
return render_ssr if configuration.ssr_enabled rescue nil
28-
@render_method.call template: 'inertia', layout: layout, locals: (view_data).merge({page: page})
34+
@render_method.call template: 'inertia', layout: layout, locals: view_data.merge(page: page)
2935
end
3036
end
3137

@@ -43,13 +49,17 @@ def layout
4349
configuration.layout
4450
end
4551

52+
def shared_data
53+
controller.__send__(:inertia_shared_data)
54+
end
55+
4656
def computed_props
4757
# Cast props to symbol keyed hash before merging so that we have a consistent data structure and
4858
# avoid duplicate keys after merging.
4959
#
5060
# Functionally, this permits using either string or symbol keys in the controller. Since the results
5161
# is cast to json, we should treat string/symbol keys as identical.
52-
_props = ::InertiaRails.shared_data(@controller).deep_symbolize_keys.send(prop_merge_method, @props.deep_symbolize_keys).select do |key, prop|
62+
_props = shared_data.deep_symbolize_keys.send(prop_merge_method, props.deep_symbolize_keys).select do |key, prop|
5363
if rendering_partial_component?
5464
key.in? partial_keys
5565
else
@@ -60,7 +70,7 @@ def computed_props
6070
deep_transform_values(
6171
_props,
6272
lambda do |prop|
63-
prop.respond_to?(:call) ? @controller.instance_exec(&prop) : prop
73+
prop.respond_to?(:call) ? controller.instance_exec(&prop) : prop
6474
end
6575
)
6676
end

spec/inertia/rendering_spec.rb

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

4-
let(:controller) { double('Controller', inertia_view_assigns: {}, inertia_configuration: InertiaRails.configuration)}
4+
let(:controller) {
5+
instance_double(
6+
InertiaRails::Controller,
7+
inertia_view_assigns: {},
8+
inertia_configuration: InertiaRails.configuration,
9+
inertia_shared_data: {},
10+
)
11+
}
512

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

0 commit comments

Comments
 (0)