Skip to content

Commit 65ba636

Browse files
authored
Merge pull request #168 from skryukov/fix-to-hash
Fix to_hash called on errors raises an error
2 parents 8170a37 + 3854acc commit 65ba636

File tree

7 files changed

+44
-5
lines changed

7 files changed

+44
-5
lines changed

.github/workflows/generators.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ jobs:
4545
ruby-version: ${{ matrix.ruby }}
4646
bundler-cache: true
4747

48+
- name: Run generator tests
49+
run: bundle exec rspec --tag type:generator
50+
4851
- name: Set up Node
4952
uses: actions/setup-node@v4
5053
with:

.github/workflows/push.yml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,17 @@ jobs:
3333
strategy:
3434
fail-fast: false
3535
matrix:
36-
ruby: ['3.1', '3.2', '3.3']
37-
rails: ['6.1', '7.0', '7.1', '7.2']
36+
ruby: ['3.0', '3.1', '3.2', '3.3']
37+
rails: ['6.1', '7.0', '7.1', '7.2', '8.0']
38+
exclude:
39+
- ruby: '3.0'
40+
rails: '8.0'
41+
- ruby: '3.1'
42+
rails: '8.0'
43+
- ruby: '3.0'
44+
rails: '7.2'
45+
- ruby: '3.1'
46+
rails: '7.2'
3847

3948
runs-on: ubuntu-latest
4049
name: Test against Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
@@ -54,11 +63,14 @@ jobs:
5463
uses: ruby/setup-ruby@v1
5564
with:
5665
ruby-version: ${{ matrix.ruby }}
66+
# Use the latest version of RubyGems with Ruby 3.0 to avoid:
67+
# https://bugs.ruby-lang.org/issues/19371
68+
rubygems: ${{ startsWith(matrix.ruby-version, '3.0') && 'latest' || 'default' }}
5769
bundler-cache: true
5870
env:
5971
RAILS_VERSION: ${{ matrix.rails }}
6072

6173
- name: Run tests
62-
run: bundle exec rspec
74+
run: bundle exec rspec --tag ~type:generator
6375
env:
6476
RAILS_VERSION: ${{ matrix.rails }}

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ source 'https://rubygems.org'
55
# Specify your gem's dependencies in inertia_rails.gemspec
66
gemspec
77

8-
version = ENV['RAILS_VERSION'] || '7.2'
8+
version = ENV['RAILS_VERSION'] || '8.0'
99
gem 'rails', "~> #{version}.0"
1010

1111
gem 'bundler', '~> 2.0'

lib/inertia_rails/controller.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ def inertia_location(url)
157157

158158
def capture_inertia_errors(options)
159159
if (inertia_errors = options.dig(:inertia, :errors))
160-
session[:inertia_errors] = inertia_errors.to_hash
160+
if inertia_errors.respond_to?(:to_hash)
161+
session[:inertia_errors] = inertia_errors.to_hash
162+
else
163+
InertiaRails.deprecator.warn(
164+
"Object passed to `inertia: { errors: ... }` must respond to `to_hash`. Pass a hash-like object instead."
165+
)
166+
session[:inertia_errors] = inertia_errors
167+
end
161168
end
162169
end
163170
end

spec/dummy/app/controllers/inertia_test_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def redirect_with_inertia_errors
4747
redirect_to empty_test_path, inertia: { errors: { uh: 'oh' } }
4848
end
4949

50+
def redirect_with_non_hash_inertia_errors
51+
redirect_to empty_test_path, inertia: { errors: 'uh oh' }
52+
end
53+
5054
def redirect_with_inertia_error_object
5155
redirect_to empty_test_path, inertia: { errors: MyError.new }
5256
end

spec/dummy/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
get 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2525
post 'redirect_with_inertia_errors' => 'inertia_test#redirect_with_inertia_errors'
2626
post 'redirect_with_inertia_error_object' => 'inertia_test#redirect_with_inertia_error_object'
27+
post 'redirect_with_non_hash_inertia_errors' => 'inertia_test#redirect_with_non_hash_inertia_errors'
2728
post 'redirect_back_with_inertia_errors' => 'inertia_test#redirect_back_with_inertia_errors'
2829
post 'redirect_back_or_to_with_inertia_errors' => 'inertia_test#redirect_back_or_to_with_inertia_errors'
2930
get 'error_404' => 'inertia_test#error_404'

spec/inertia/error_sharing_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
expect(session[:inertia_errors]).not_to be
1818
end
1919

20+
it 'accepts a non-hash error object' do
21+
expect { post redirect_with_non_hash_inertia_errors_path, headers: headers }
22+
.to output(/Object passed to `inertia: { errors: ... }` must respond to `to_hash`/).to_stderr
23+
expect(response.headers['Location']).to eq(empty_test_url)
24+
expect(session[:inertia_errors]).to eq('uh oh')
25+
26+
# Follow the redirect
27+
get response.headers['Location'], headers: headers
28+
expect(response.body).to include({ errors: 'uh oh' }.to_json)
29+
expect(session[:inertia_errors]).not_to be
30+
end
31+
2032
it 'keeps errors around when the post has a stale version' do
2133
post redirect_with_inertia_errors_path, headers: headers
2234
expect(response.headers['Location']).to eq(empty_test_url)

0 commit comments

Comments
 (0)