Skip to content

Commit 3e622fc

Browse files
committed
Add limited support for resource/resources
1 parent 73e0a32 commit 3e622fc

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

lib/patches/mapper.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module InertiaRails
22
module InertiaMapper
3-
def inertia(args)
4-
route, component = extract_route_and_component(args)
3+
def inertia(*args, **options)
4+
path = args.any? ? args.first : options
5+
route, component = extract_route_and_component(path)
56
@scope = @scope.new(module: nil)
6-
get(route, to: 'inertia_rails/static#static', defaults: { component: component })
7+
get(route, to: 'inertia_rails/static#static', defaults: { component: component }, **options)
78
ensure
89
@scope = @scope.parent
910
end
@@ -13,6 +14,8 @@ def inertia(args)
1314
def extract_route_and_component(args)
1415
if args.is_a?(Hash)
1516
args.first
17+
elsif resource_scope?
18+
[args, InertiaRails.configuration.component_path_resolver(path: [@scope[:module], @scope[:controller]].compact.join('/'), action: args)]
1619
elsif @scope[:module].blank?
1720
[args, args]
1821
else

spec/dummy/config/routes.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@
5252
scope :scoped, as: "scoped" do
5353
inertia 'inertia_route' => 'TestComponent'
5454
end
55-
namespace :namespaced do
56-
inertia inertia_route: 'TestComponent'
55+
resources :items do
56+
inertia inertia_route: 'TestComponent', on: :member
5757
inertia :inertia_route_with_default_component
58+
inertia :inertia_route_with_default_component_on_member, on: :member
59+
inertia :inertia_route_with_default_component_on_collection, on: :collection
5860
scope :scoped, as: "scoped" do
5961
inertia :inertia_route_with_default_component
6062
end

spec/inertia/rendering_spec.rb

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
it { is_expected.to include inertia_div(page) }
6060
end
6161

62-
context 'via a namespaced inertia route' do
63-
before { get namespaced_inertia_route_path }
62+
context 'via a resource inertia route' do
63+
before { get inertia_route_item_path(id: 1) }
6464

6565
it { is_expected.to include inertia_div(page) }
6666
end
@@ -79,18 +79,34 @@
7979
it { is_expected.to include inertia_div(page) }
8080
end
8181

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) }
82+
context 'with a default component resource' do
83+
let(:page) { InertiaRails::Renderer.new('items/inertia_route_with_default_component', controller, request, response, '').send(:page) }
8484

85-
before { get namespaced_inertia_route_with_default_component_path }
85+
before { get item_inertia_route_with_default_component_path(item_id: 1) }
8686

8787
it { is_expected.to include inertia_div(page) }
8888
end
8989

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) }
90+
context 'with a default component resource on member' do
91+
let(:page) { InertiaRails::Renderer.new('items/inertia_route_with_default_component_on_member', controller, request, response, '').send(:page) }
9292

93-
before { get namespaced_scoped_inertia_route_with_default_component_path }
93+
before { get inertia_route_with_default_component_on_member_item_path(id: 1) }
94+
95+
it { is_expected.to include inertia_div(page) }
96+
end
97+
98+
context 'with a default component resource on collection' do
99+
let(:page) { InertiaRails::Renderer.new('items/inertia_route_with_default_component_on_collection', controller, request, response, '').send(:page) }
100+
101+
before { get inertia_route_with_default_component_on_collection_items_path }
102+
103+
it { is_expected.to include inertia_div(page) }
104+
end
105+
106+
context 'with a default component resource & scoped' do
107+
let(:page) { InertiaRails::Renderer.new('items/inertia_route_with_default_component', controller, request, response, '').send(:page) }
108+
109+
before { get scoped_item_inertia_route_with_default_component_path(item_id: 1) }
94110

95111
it { is_expected.to include inertia_div(page) }
96112
end

0 commit comments

Comments
 (0)