Skip to content

Commit efc9ec8

Browse files
authored
Merge pull request #207 from skryukov/fix-409-for-missed-pages
Fix 409 for missed pages
2 parents a2d3706 + 9999f7e commit efc9ec8

File tree

8 files changed

+138
-169
lines changed

8 files changed

+138
-169
lines changed

.rubocop_todo.yml

+11-58
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config --exclude-limit 10000`
3-
# on 2025-03-04 09:53:10 UTC using RuboCop version 1.73.2.
3+
# on 2025-04-01 18:59:20 UTC using RuboCop version 1.75.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -44,29 +44,12 @@ Layout/ExtraSpacing:
4444
- 'spec/inertia/request_spec.rb'
4545
- 'spec/inertia/response_spec.rb'
4646

47-
# Offense count: 1
48-
# This cop supports safe autocorrection (--autocorrect).
49-
# Configuration parameters: EnforcedStyle, IndentationWidth.
50-
# SupportedStyles: consistent, consistent_relative_to_receiver, special_for_inner_method_call, special_for_inner_method_call_in_parentheses
51-
Layout/FirstArgumentIndentation:
52-
Exclude:
53-
- 'spec/inertia/ssr_spec.rb'
54-
5547
# Offense count: 2
5648
# This cop supports safe autocorrection (--autocorrect).
57-
# Configuration parameters: EnforcedStyle, IndentationWidth.
49+
# Configuration parameters: IndentationWidth.
5850
# SupportedStyles: special_inside_parentheses, consistent, align_braces
5951
Layout/FirstHashElementIndentation:
60-
Exclude:
61-
- 'spec/inertia/rspec_helper_spec.rb'
62-
63-
# Offense count: 4
64-
# This cop supports safe autocorrection (--autocorrect).
65-
# Configuration parameters: EnforcedStyle, IndentationWidth.
66-
# SupportedStyles: aligned, indented, indented_relative_to_receiver
67-
Layout/MultilineMethodCallIndentation:
68-
Exclude:
69-
- 'spec/inertia/ssr_spec.rb'
52+
EnforcedStyle: consistent
7053

7154
# Offense count: 6
7255
# This cop supports safe autocorrection (--autocorrect).
@@ -99,24 +82,20 @@ Layout/SpaceInsideBlockBraces:
9982
- 'spec/inertia/error_sharing_spec.rb'
10083
- 'spec/inertia/request_spec.rb'
10184

102-
# Offense count: 106
85+
# Offense count: 56
10386
# This cop supports safe autocorrection (--autocorrect).
10487
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBraces.
10588
# SupportedStyles: space, no_space, compact
10689
# SupportedStylesForEmptyBraces: space, no_space
10790
Layout/SpaceInsideHashLiteralBraces:
10891
Exclude:
10992
- 'lib/inertia_rails/controller.rb'
110-
- 'lib/inertia_rails/middleware.rb'
11193
- 'spec/dummy/app/controllers/inertia_conditional_sharing_controller.rb'
11294
- 'spec/dummy/app/controllers/inertia_share_test_controller.rb'
11395
- 'spec/inertia/conditional_sharing_spec.rb'
114-
- 'spec/inertia/middleware_spec.rb'
11596
- 'spec/inertia/rails_mimic_spec.rb'
11697
- 'spec/inertia/request_spec.rb'
11798
- 'spec/inertia/rspec_helper_spec.rb'
118-
- 'spec/inertia/sharing_spec.rb'
119-
- 'spec/inertia/ssr_spec.rb'
12099

121100
# Offense count: 2
122101
# This cop supports safe autocorrection (--autocorrect).
@@ -164,14 +143,6 @@ Lint/SymbolConversion:
164143
Exclude:
165144
- 'spec/inertia/rails_mimic_spec.rb'
166145

167-
# Offense count: 1
168-
# This cop supports safe autocorrection (--autocorrect).
169-
# Configuration parameters: AutoCorrect, AllowUnusedKeywordArguments, IgnoreEmptyMethods, IgnoreNotImplementedMethods, NotImplementedExceptions.
170-
# NotImplementedExceptions: NotImplementedError
171-
Lint/UnusedMethodArgument:
172-
Exclude:
173-
- 'lib/patches/mapper.rb'
174-
175146
# Offense count: 1
176147
Naming/AccessorMethodName:
177148
Exclude:
@@ -190,12 +161,12 @@ Naming/FileName:
190161
# Offense count: 2
191162
# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns.
192163
# SupportedStyles: snake_case, normalcase, non_integer
193-
# AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
164+
# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64
194165
Naming/VariableNumber:
195166
Exclude:
196167
- 'spec/dummy/app/controllers/inertia_test_controller.rb'
197168

198-
# Offense count: 10
169+
# Offense count: 7
199170
# This cop supports safe autocorrection (--autocorrect).
200171
# Configuration parameters: EnforcedStyle, ProceduralMethods, FunctionalMethods, AllowedMethods, AllowedPatterns, AllowBracesOnProceduralOneLiners, BracesRequiredMethods.
201172
# SupportedStyles: line_count_based, semantic, braces_for_chaining, always_braces
@@ -210,12 +181,13 @@ Style/BlockDelimiters:
210181
- 'spec/inertia/rails_mimic_spec.rb'
211182
- 'spec/inertia/request_spec.rb'
212183
- 'spec/inertia/rspec_helper_spec.rb'
213-
- 'spec/inertia/sharing_spec.rb'
214184

215185
# Offense count: 1
216186
# This cop supports unsafe autocorrection (--autocorrect-all).
217-
# Configuration parameters: EnforcedStyle.
187+
# Configuration parameters: EnforcedStyle, EnforcedStyleForClasses, EnforcedStyleForModules.
218188
# SupportedStyles: nested, compact
189+
# SupportedStylesForClasses: , nested, compact
190+
# SupportedStylesForModules: , nested, compact
219191
Style/ClassAndModuleChildren:
220192
Exclude:
221193
- 'lib/inertia_rails/helper.rb'
@@ -255,7 +227,7 @@ Style/ExpandPathArguments:
255227
Exclude:
256228
- 'spec/rails_helper.rb'
257229

258-
# Offense count: 69
230+
# Offense count: 65
259231
# This cop supports unsafe autocorrection (--autocorrect-all).
260232
# Configuration parameters: EnforcedStyle.
261233
# SupportedStyles: always, always_true, never
@@ -321,15 +293,11 @@ Style/FrozenStringLiteralComment:
321293
- 'spec/inertia/conditional_sharing_spec.rb'
322294
- 'spec/inertia/error_sharing_spec.rb'
323295
- 'spec/inertia/lazy_prop_spec.rb'
324-
- 'spec/inertia/middleware_spec.rb'
325296
- 'spec/inertia/rails_mimic_spec.rb'
326297
- 'spec/inertia/request_spec.rb'
327298
- 'spec/inertia/response_spec.rb'
328299
- 'spec/inertia/rspec_helper_spec.rb'
329-
- 'spec/inertia/sharing_spec.rb'
330-
- 'spec/inertia/ssr_spec.rb'
331300
- 'spec/rails_helper.rb'
332-
- 'spec/spec_helper.rb'
333301
- 'spec/support/shared_examples.rb'
334302

335303
# Offense count: 1
@@ -399,11 +367,6 @@ Style/NestedModifier:
399367
Exclude:
400368
- 'lib/inertia_rails/controller.rb'
401369

402-
# Offense count: 1
403-
Style/OpenStructUse:
404-
Exclude:
405-
- 'spec/inertia/ssr_spec.rb'
406-
407370
# Offense count: 2
408371
# This cop supports safe autocorrection (--autocorrect).
409372
Style/RedundantBegin:
@@ -456,7 +419,6 @@ Style/StringLiterals:
456419
- 'lib/inertia_rails/controller.rb'
457420
- 'lib/inertia_rails/engine.rb'
458421
- 'lib/inertia_rails/helper.rb'
459-
- 'lib/inertia_rails/middleware.rb'
460422
- 'lib/inertia_rails/rspec.rb'
461423
- 'lib/inertia_rails/version.rb'
462424
- 'lib/patches/better_errors.rb'
@@ -476,7 +438,6 @@ Style/StringLiterals:
476438
- 'spec/inertia/response_spec.rb'
477439
- 'spec/inertia/rspec_helper_spec.rb'
478440
- 'spec/rails_helper.rb'
479-
- 'spec/spec_helper.rb'
480441

481442
# Offense count: 2
482443
# This cop supports safe autocorrection (--autocorrect).
@@ -511,15 +472,7 @@ Style/TrailingCommaInHashLiteral:
511472
- 'spec/inertia/response_spec.rb'
512473
- 'spec/inertia/rspec_helper_spec.rb'
513474

514-
# Offense count: 1
515-
# This cop supports safe autocorrection (--autocorrect).
516-
# Configuration parameters: EnforcedStyle, MinSize, WordRegex.
517-
# SupportedStyles: percent, brackets
518-
Style/WordArray:
519-
Exclude:
520-
- 'lib/inertia_rails/middleware.rb'
521-
522-
# Offense count: 17
475+
# Offense count: 24
523476
# This cop supports safe autocorrection (--autocorrect).
524477
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, SplitStrings.
525478
# URISchemes: http, https

lib/inertia_rails/middleware.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def redirect_status?(status)
4747
end
4848

4949
def non_get_redirectable_method?
50-
['PUT', 'PATCH', 'DELETE'].include? request_method
50+
%w[PUT PATCH DELETE].include? request_method
5151
end
5252

5353
def inertia_non_post_redirect?(status)
@@ -63,7 +63,7 @@ def get?
6363
end
6464

6565
def controller
66-
@env["action_controller.instance"]
66+
@env['action_controller.instance']
6767
end
6868

6969
def request_method
@@ -83,7 +83,7 @@ def version_stale?
8383
end
8484

8585
def server_version
86-
controller&.send(:inertia_configuration)&.version
86+
(controller&.send(:inertia_configuration) || InertiaRails.configuration).version
8787
end
8888

8989
def coerce_version(version)
@@ -92,7 +92,7 @@ def coerce_version(version)
9292

9393
def force_refresh(request)
9494
request.flash.keep
95-
Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
95+
Rack::Response.new('', 409, { 'X-Inertia-Location' => request.original_url }).finish
9696
end
9797

9898
def copy_xsrf_to_csrf!

lib/inertia_rails/renderer.rb

+17-27
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
module InertiaRails
88
class Renderer
9-
KEEP_PROP = :keep
10-
DONT_KEEP_PROP = :dont_keep
11-
129
attr_reader(
1310
:component,
1411
:configuration,
@@ -82,31 +79,17 @@ def shared_data
8279
#
8380
# Functionally, this permits using either string or symbol keys in the controller. Since the results
8481
# is cast to json, we should treat string/symbol keys as identical.
85-
def merge_props(shared_data, props)
82+
def merge_props(shared_props, props)
8683
if @deep_merge
87-
shared_data.deep_symbolize_keys.deep_merge!(props.deep_symbolize_keys)
84+
shared_props.deep_symbolize_keys.deep_merge!(props.deep_symbolize_keys)
8885
else
89-
shared_data.symbolize_keys.merge(props.symbolize_keys)
86+
shared_props.symbolize_keys.merge(props.symbolize_keys)
9087
end
9188
end
9289

9390
def computed_props
9491
merged_props = merge_props(shared_data, props)
95-
96-
deep_transform_props merged_props do |prop, path|
97-
next [DONT_KEEP_PROP] unless keep_prop?(prop, path)
98-
99-
transformed_prop = case prop
100-
when BaseProp
101-
prop.call(controller)
102-
when Proc
103-
controller.instance_exec(&prop)
104-
else
105-
prop
106-
end
107-
108-
[KEEP_PROP, transformed_prop]
109-
end
92+
deep_transform_props(merged_props)
11093
end
11194

11295
def page
@@ -128,16 +111,23 @@ def page
128111
default_page
129112
end
130113

131-
def deep_transform_props(props, parent_path = [], &block)
114+
def deep_transform_props(props, parent_path = [])
132115
props.each_with_object({}) do |(key, prop), transformed_props|
133116
current_path = parent_path + [key]
134117

135118
if prop.is_a?(Hash) && prop.any?
136-
nested = deep_transform_props(prop, current_path, &block)
137-
transformed_props.merge!(key => nested) unless nested.empty?
138-
else
139-
action, transformed_prop = block.call(prop, current_path)
140-
transformed_props.merge!(key => transformed_prop) if action == KEEP_PROP
119+
nested = deep_transform_props(prop, current_path)
120+
transformed_props[key] = nested unless nested.empty?
121+
elsif keep_prop?(prop, current_path)
122+
transformed_props[key] =
123+
case prop
124+
when BaseProp
125+
prop.call(controller)
126+
when Proc
127+
controller.instance_exec(&prop)
128+
else
129+
prop
130+
end
141131
end
142132
end
143133
end

spec/inertia/middleware_spec.rb

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
1+
# frozen_string_literal: true
2+
13
RSpec.describe InertiaRails::Middleware, type: :request do
2-
context 'the version is stale' do
3-
it 'tells the client to refresh' do
4-
get empty_test_path, headers: {'X-Inertia' => true, 'X-Inertia-Version' => 'blkajdf'}
4+
context 'the version is set' do
5+
with_inertia_config version: '1.0'
6+
7+
it 'tells the client with stale version to refresh' do
8+
get empty_test_path, headers: { 'X-Inertia' => true, 'X-Inertia-Version' => 'stale' }
59

610
expect(response.status).to eq 409
711
expect(response.headers['X-Inertia-Location']).to eq request.original_url
812
end
13+
14+
it 'returns page when version is up to date' do
15+
get empty_test_path, headers: { 'X-Inertia' => true, 'X-Inertia-Version' => '1.0' }
16+
17+
expect(response.status).to eq 200
18+
end
19+
20+
it 'returns response for non-inertia requests' do
21+
get empty_test_path, headers: { 'X-Inertia-Version' => 'stale' }
22+
23+
expect(response.status).to eq 200
24+
end
25+
26+
it 'returns 404 on unknown route' do
27+
expect do
28+
get '/unknown_route', headers: { 'X-Inertia' => true, 'X-Inertia-Version' => '1.0' }
29+
end.to raise_error(ActionController::RoutingError)
30+
end
931
end
1032

1133
context 'a redirect status was passed with an http method that preserves itself on 302 redirect' do
1234
subject { response.status }
1335

1436
context 'PUT' do
15-
before { put redirect_test_path, headers: {'X-Inertia' => true} }
37+
before { put redirect_test_path, headers: { 'X-Inertia' => true } }
1638

1739
it { is_expected.to eq 303 }
1840
end
1941

2042
context 'PATCH' do
21-
before { patch redirect_test_path, headers: {'X-Inertia' => true} }
43+
before { patch redirect_test_path, headers: { 'X-Inertia' => true } }
2244

2345
it { is_expected.to eq 303 }
2446
end
2547

2648
context 'DELETE' do
27-
before { delete redirect_test_path, headers: {'X-Inertia' => true} }
49+
before { delete redirect_test_path, headers: { 'X-Inertia' => true } }
2850

2951
it { is_expected.to eq 303 }
3052
end
@@ -47,12 +69,4 @@
4769
expect(statusses.uniq).to eq([303])
4870
end
4971
end
50-
51-
context 'a request not originating from inertia' do
52-
it 'is ignored' do
53-
get empty_test_path, headers: {'X-Inertia-Version' => 'blkajdf'}
54-
55-
expect(response.status).to eq 200
56-
end
57-
end
5872
end

0 commit comments

Comments
 (0)