Skip to content

Commit 4ceb8c3

Browse files
authored
Merge pull request #209 from Andy9822/add-helper-for-inertia-rendering
Add `inertia_rendering?` method and set instance variable in renderer
2 parents efc9ec8 + 085e936 commit 4ceb8c3

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

lib/inertia_rails/helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ def inertia_headers
1111
)
1212
inertia_ssr_head
1313
end
14+
15+
def inertia_rendering?
16+
controller.instance_variable_get("@_inertia_rendering")
17+
end
1418
end

lib/inertia_rails/renderer.rb

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def initialize(component, controller, request, response, render_method, props: n
3434
@deep_merge = deep_merge.nil? ? configuration.deep_merge_shared_data : deep_merge
3535
@encrypt_history = encrypt_history.nil? ? configuration.encrypt_history : encrypt_history
3636
@clear_history = clear_history || controller.session[:inertia_clear_history] || false
37+
@controller.instance_variable_set('@_inertia_rendering', true)
3738
end
3839

3940
def render

spec/inertia/helper_spec.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe InertiaRails::Helper do
4+
let(:controller) { ApplicationController.new }
5+
6+
let(:test_helper) do
7+
Class.new do
8+
include InertiaRails::Helper
9+
attr_accessor :controller
10+
end.new
11+
end
12+
13+
before do
14+
test_helper.controller = controller
15+
end
16+
17+
describe '#inertia_rendering?' do
18+
context 'when not rendering through Inertia' do
19+
it 'returns nil' do
20+
expect(test_helper.inertia_rendering?).to be_nil
21+
end
22+
end
23+
24+
context 'when rendering through Inertia' do
25+
before do
26+
controller.instance_variable_set('@_inertia_rendering', true)
27+
end
28+
29+
it 'returns true' do
30+
expect(test_helper.inertia_rendering?).to be true
31+
end
32+
end
33+
end
34+
end

0 commit comments

Comments
 (0)