Skip to content

Commit 73e0a32

Browse files
committed
Fix inertia routes helper
1 parent f59f324 commit 73e0a32

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

lib/patches/mapper.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
module InertiaRails
22
module InertiaMapper
3-
def inertia(args, &block)
4-
route = args.keys.first
5-
component = args.values.first
3+
def inertia(args)
4+
route, component = extract_route_and_component(args)
5+
@scope = @scope.new(module: nil)
6+
get(route, to: 'inertia_rails/static#static', defaults: { component: component })
7+
ensure
8+
@scope = @scope.parent
9+
end
10+
11+
private
612

7-
get(route => 'inertia_rails/static#static', defaults: { component: component })
13+
def extract_route_and_component(args)
14+
if args.is_a?(Hash)
15+
args.first
16+
elsif @scope[:module].blank?
17+
[args, args]
18+
else
19+
[args, InertiaRails.configuration.component_path_resolver(path: @scope[:module], action: args)]
20+
end
821
end
922
end
1023
end

spec/dummy/config/routes.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,19 @@
4646
get 'provided_props_test' => 'inertia_rails_mimic#provided_props_test'
4747

4848
post 'redirect_to_share_test' => 'inertia_test#redirect_to_share_test'
49+
4950
inertia 'inertia_route' => 'TestComponent'
51+
inertia :inertia_route_with_default_component
52+
scope :scoped, as: "scoped" do
53+
inertia 'inertia_route' => 'TestComponent'
54+
end
55+
namespace :namespaced do
56+
inertia inertia_route: 'TestComponent'
57+
inertia :inertia_route_with_default_component
58+
scope :scoped, as: "scoped" do
59+
inertia :inertia_route_with_default_component
60+
end
61+
end
5062

5163
get 'merge_shared' => 'inertia_merge_shared#merge_shared'
5264
get 'deep_merge_shared' => 'inertia_merge_shared#deep_merge_shared'

spec/inertia/rendering_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@
5858

5959
it { is_expected.to include inertia_div(page) }
6060
end
61+
62+
context 'via a namespaced inertia route' do
63+
before { get namespaced_inertia_route_path }
64+
65+
it { is_expected.to include inertia_div(page) }
66+
end
67+
68+
context 'via a scoped inertia route' do
69+
before { get scoped_inertia_route_path }
70+
71+
it { is_expected.to include inertia_div(page) }
72+
end
73+
74+
context 'with a default component' do
75+
let(:page) { InertiaRails::Renderer.new('inertia_route_with_default_component', controller, request, response, '').send(:page) }
76+
77+
before { get inertia_route_with_default_component_path }
78+
79+
it { is_expected.to include inertia_div(page) }
80+
end
81+
82+
context 'with a default component namespaced' do
83+
let(:page) { InertiaRails::Renderer.new('namespaced/inertia_route_with_default_component', controller, request, response, '').send(:page) }
84+
85+
before { get namespaced_inertia_route_with_default_component_path }
86+
87+
it { is_expected.to include inertia_div(page) }
88+
end
89+
90+
context 'with a default component namespaced & scoped' do
91+
let(:page) { InertiaRails::Renderer.new('namespaced/inertia_route_with_default_component', controller, request, response, '').send(:page) }
92+
93+
before { get namespaced_scoped_inertia_route_with_default_component_path }
94+
95+
it { is_expected.to include inertia_div(page) }
96+
end
6197
end
6298

6399
context 'subsequent requests' do

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# Disable RSpec exposing methods globally on `Module` and `main`
66
config.disable_monkey_patching!
77

8+
config.filter_run_when_matching :focus
9+
810
config.expect_with :rspec do |c|
911
c.syntax = :expect
1012
end

0 commit comments

Comments
 (0)