inertia_rails 1.3.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 684a3d8e7aa84b71947e75e79b991c9321a1b7dc86b57b3fae25cc1845518d03
4
- data.tar.gz: 82d04030486998787f63d998a58ee4b6c3720c2e3424e9b76d937bb26a438f33
3
+ metadata.gz: b889c5a06cf6c9310b1d64c1e19a5e979aea7ba874c3014867a462386dd28937
4
+ data.tar.gz: c5e1bf21eb3ff8dc1e14ebadedf43bb82f8b1af6aa46680610ad4b0054d90b3b
5
5
  SHA512:
6
- metadata.gz: d9fd1e10102a1ab46234b855308c5d7dcb1cf04e956b972760a741b609de9e473a6a56815580ec1c059cc1f13c1b7d65950ca73d707599e5a5e28c6905000f82
7
- data.tar.gz: e6fe87e5459eaa6e4309d165b665170ea4bcda4f7d3b4ce8c52ac7a9424d116d1672f2c2033c6ba927edaf9b40dcb5b9c6b84c5eeff64545c89a6ca48fddab93
6
+ metadata.gz: 65a66ab26b16808a9b88467a6b579074f68ba68877c055b8d2e333422454041f3461ff4ab0a947f2a52572edd8bf1c88d62d6d32ec7a1c8bd6ca776158f756c0
7
+ data.tar.gz: 724961702c0bd5ffb53e427d8791f1064dcc080c4cbc253297f30207c0017065b0f2c12b4822c5969401582972810c8960fbc494884bebb32fe1df907e6a7d79
@@ -1,24 +1,34 @@
1
1
  name: Testing
2
2
 
3
- on: [push]
3
+ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  test:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ ruby: [2.6, 2.7]
11
+ rails: ['5.0', '5.1', '5.2', '6.0']
12
+
7
13
  runs-on: ubuntu-latest
14
+ name: Test against Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
8
15
 
9
16
  steps:
10
- - uses: actions/checkout@v1
17
+ - uses: actions/checkout@v2
11
18
 
12
19
  - name: Setup System
13
20
  run: sudo apt-get install libsqlite3-dev
14
21
 
15
- - name: Set up Ruby 2.6
16
- uses: actions/setup-ruby@v1
22
+ - name: Set up Ruby
23
+ uses: ruby/setup-ruby@v1
17
24
  with:
18
- ruby-version: 2.6.x
25
+ ruby-version: ${{ matrix.ruby }}
19
26
 
20
27
  - name: Install gems
28
+ env:
29
+ MATRIX_RAILS_VERSION: ${{ matrix.rails }}
21
30
  run: |
31
+ export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/rails_${MATRIX_RAILS_VERSION}.gemfile"
22
32
  gem install bundler
23
33
  bundle install --jobs 4 --retry 3
24
34
 
data/.gitignore CHANGED
@@ -17,3 +17,6 @@
17
17
 
18
18
  # rspec failure tracking
19
19
  .rspec_status
20
+
21
+ # Appraisal
22
+ gemfiles/*.gemfile.lock
@@ -0,0 +1,15 @@
1
+ appraise "rails-6.0" do
2
+ gem "rails", "~> 6.0.3", '>= 6.0.3.2'
3
+ end
4
+
5
+ appraise "rails-5.2" do
6
+ gem "rails", "~> 5.2.4", '>= 5.2.4.3'
7
+ end
8
+
9
+ appraise "rails-5.1" do
10
+ gem "rails", "~> 5.1.7"
11
+ end
12
+
13
+ appraise "rails-5.0" do
14
+ gem "rails", "~> 5.0.7", '>= 5.0.7.2'
15
+ end
@@ -4,6 +4,32 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
5
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.6.0] - 2020-11-20
8
+
9
+ * Built in error sharing across redirects! adding `{ inertia: { errors: 'errors go here' } }` as an option in `redirect_to` will automatically feed an `errors` prop to whatever is rendered after the redirect.
10
+ * Set content type to json for Inertia responses
11
+ * Return the original response status with Inertia responses
12
+
13
+ ## [1.5.0] - 2020-10-07
14
+
15
+ * Test against multiple Rails versions in Github Actions
16
+ * Add the `inertia_location` controller method that forces a full page refresh
17
+
18
+ ## [1.4.1] - 2020-08-06
19
+
20
+ * Fixed a bug involving threadsafe versions and layouts
21
+
22
+ ## [1.4.0] - 2020-07-09
23
+
24
+ * Fixed Ruby 2.7 deprecation warnings
25
+ * Added `inertia_partial?` method
26
+ * Fixed homepage in the gemspec
27
+ * Make the InertiaRails module data threadsafe
28
+
29
+ ## [1.3.1] - 2020-02-20
30
+
31
+ * Fix a typo in the README (inertia only has 1 t!)
32
+
7
33
  ## [1.3.0] - 2020-01-28
8
34
 
9
35
  ### Added
data/README.md CHANGED
@@ -6,7 +6,7 @@ Visit [inertiajs.com](https://inertiajs.com/) to learn more.
6
6
 
7
7
  The initial version of the gem was named `inertia`; however, that name was not available on Rubygems.
8
8
 
9
- The 1.0.0 version release on Rubygems is `intertia_rails`.
9
+ The 1.0.0 version release on Rubygems is `inertia_rails`.
10
10
 
11
11
  The changes required are:
12
12
 
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.0.7", ">= 5.0.7.2"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.1.7"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.2.4", ">= 5.2.4.3"
6
+
7
+ gemspec path: "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 6.0.3", ">= 6.0.3.2"
6
+
7
+ gemspec path: "../"
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["brain@bellawatt.com", "brandon@bellawatt.com", "eugene@bellawatt.com"]
10
10
 
11
11
  spec.summary = %q{Inertia adapter for Rails}
12
- spec.homepage = "https://github.com/inertiajs/inertia-rails/"
12
+ spec.homepage = "https://github.com/inertiajs/inertia-rails"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
@@ -25,10 +25,12 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
+ spec.add_runtime_dependency "rails", '>= 5'
29
+
28
30
  spec.add_development_dependency "bundler", "~> 2.0"
29
- spec.add_development_dependency "rake", "~> 10.0"
30
- spec.add_development_dependency "rspec-rails", "~> 3.0"
31
- spec.add_development_dependency "rails"
31
+ spec.add_development_dependency "rake", "~> 13.0"
32
+ spec.add_development_dependency "rspec-rails", "~> 4.0"
32
33
  spec.add_development_dependency "rails-controller-testing"
33
34
  spec.add_development_dependency "sqlite3"
35
+ spec.add_development_dependency "appraisal"
34
36
  end
@@ -4,13 +4,33 @@ module InertiaRails
4
4
  module Controller
5
5
  extend ActiveSupport::Concern
6
6
 
7
+ included do
8
+ before_action do
9
+ # :inertia_errors are deleted from the session by the middleware
10
+ InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
11
+ end
12
+ end
13
+
7
14
  module ClassMethods
8
15
  def inertia_share(**args, &block)
9
16
  before_action do
10
- InertiaRails.share(args) if args
17
+ InertiaRails.share(**args) if args
11
18
  InertiaRails.share_block(block) if block
12
19
  end
13
20
  end
14
21
  end
22
+
23
+ def inertia_location(url)
24
+ headers['X-Inertia-Location'] = url
25
+ head :conflict
26
+ end
27
+
28
+ def redirect_to(options = {}, response_options = {})
29
+ if (inertia_errors = response_options.fetch(:inertia, {}).fetch(:errors, nil))
30
+ session[:inertia_errors] = inertia_errors
31
+ end
32
+
33
+ super(options, response_options)
34
+ end
15
35
  end
16
36
  end
@@ -1,6 +1,9 @@
1
+ # Needed for `thread_mattr_accessor`
2
+ require 'active_support/core_ext/module/attribute_accessors_per_thread'
3
+
1
4
  module InertiaRails
2
- mattr_accessor(:shared_plain_data) { Hash.new }
3
- mattr_accessor(:shared_blocks) { [] }
5
+ thread_mattr_accessor :threadsafe_shared_plain_data
6
+ thread_mattr_accessor :threadsafe_shared_blocks
4
7
 
5
8
  def self.configure
6
9
  yield(Configuration)
@@ -21,11 +24,11 @@ module InertiaRails
21
24
 
22
25
  # "Setters"
23
26
  def self.share(**args)
24
- shared_plain_data.merge!(args)
27
+ self.shared_plain_data = self.shared_plain_data.merge(args)
25
28
  end
26
29
 
27
30
  def self.share_block(block)
28
- shared_blocks.push(block)
31
+ self.shared_blocks = self.shared_blocks + [block]
29
32
  end
30
33
 
31
34
  def self.reset!
@@ -44,6 +47,23 @@ module InertiaRails
44
47
  end
45
48
  end
46
49
 
50
+ # Getters and setters to provide default values for the threadsafe attributes
51
+ def self.shared_plain_data
52
+ self.threadsafe_shared_plain_data || {}
53
+ end
54
+
55
+ def self.shared_plain_data=(val)
56
+ self.threadsafe_shared_plain_data = val
57
+ end
58
+
59
+ def self.shared_blocks
60
+ self.threadsafe_shared_blocks || []
61
+ end
62
+
63
+ def self.shared_blocks=(val)
64
+ self.threadsafe_shared_blocks = val
65
+ end
66
+
47
67
  def self.evaluated_blocks(controller, blocks)
48
68
  blocks.flat_map { |block| controller.instance_exec(&block) }.reduce(&:merge) || {}
49
69
  end
@@ -3,41 +3,78 @@ module InertiaRails
3
3
  def initialize(app)
4
4
  @app = app
5
5
  end
6
-
6
+
7
7
  def call(env)
8
+ @env = env
9
+
8
10
  status, headers, body = @app.call(env)
9
11
  request = ActionDispatch::Request.new(env)
10
12
 
11
13
  ::InertiaRails.reset!
12
-
13
- return [status, headers, body] unless env['HTTP_X_INERTIA'].present?
14
-
15
- return force_refresh(request) if stale?(env['REQUEST_METHOD'], env['HTTP_X_INERTIA_VERSION'])
16
-
17
- if is_redirect_status?(status) &&
18
- is_non_get_redirectable_method?(env['REQUEST_METHOD'])
19
- status = 303
20
- end
21
-
22
- [status, headers, body]
23
- end
24
-
14
+
15
+ # Inertia errors are added to the session via redirect_to
16
+ request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
17
+
18
+ status = 303 if inertia_non_post_redirect?(status)
19
+
20
+ return stale_inertia_get? ? force_refresh(request) : [status, headers, body]
21
+ end
22
+
25
23
  private
26
-
27
- def is_redirect_status?(status)
24
+
25
+ def keep_inertia_errors?(status)
26
+ redirect_status?(status) || stale_inertia_request?
27
+ end
28
+
29
+ def stale_inertia_request?
30
+ inertia_request? && version_stale?
31
+ end
32
+
33
+ def redirect_status?(status)
28
34
  [301, 302].include? status
29
35
  end
30
-
31
- def is_non_get_redirectable_method?(request_method)
36
+
37
+ def non_get_redirectable_method?
32
38
  ['PUT', 'PATCH', 'DELETE'].include? request_method
33
39
  end
34
-
35
- def stale?(request_method, inertia_version)
36
- sent_version = InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
37
- saved_version = InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
38
- request_method == 'GET' && sent_version != saved_version
40
+
41
+ def inertia_non_post_redirect?(status)
42
+ inertia_request? && redirect_status?(status) && non_get_redirectable_method?
43
+ end
44
+
45
+ def stale_inertia_get?
46
+ get? && stale_inertia_request?
39
47
  end
40
-
48
+
49
+ def get?
50
+ request_method == 'GET'
51
+ end
52
+
53
+ def request_method
54
+ @env['REQUEST_METHOD']
55
+ end
56
+
57
+ def inertia_version
58
+ @env['HTTP_X_INERTIA_VERSION']
59
+ end
60
+
61
+ def inertia_request?
62
+ @env['HTTP_X_INERTIA'].present?
63
+ end
64
+
65
+ def version_stale?
66
+ sent_version != saved_version
67
+ end
68
+
69
+ def sent_version
70
+ return nil if inertia_version.nil?
71
+ InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
72
+ end
73
+
74
+ def saved_version
75
+ InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
76
+ end
77
+
41
78
  def force_refresh(request)
42
79
  request.flash.keep
43
80
  Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
@@ -18,7 +18,7 @@ module InertiaRails
18
18
  if @request.headers['X-Inertia']
19
19
  @response.set_header('Vary', 'Accept')
20
20
  @response.set_header('X-Inertia', 'true')
21
- @render_method.call json: page, status: 200
21
+ @render_method.call json: page, status: @response.status, content_type: Mime[:json]
22
22
  else
23
23
  @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
24
24
  end
@@ -61,7 +61,7 @@ RSpec.configure do |config|
61
61
  config.before(:each, inertia: true) do
62
62
  new_renderer = InertiaRails::Renderer.method(:new)
63
63
  allow(InertiaRails::Renderer).to receive(:new) do |component, controller, request, response, render, named_args|
64
- new_renderer.call(component, controller, request, response, inertia_wrap_render(render), named_args)
64
+ new_renderer.call(component, controller, request, response, inertia_wrap_render(render), **named_args)
65
65
  end
66
66
  end
67
67
  end
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.3.0"
2
+ VERSION = "1.6.0"
3
3
  end
@@ -11,13 +11,7 @@ if defined?(ActionDispatch::DebugExceptions)
11
11
  require 'patches/debug_exceptions/patch-5-1'
12
12
  elsif ActionPack.version.to_s >= '5.0'
13
13
  require 'patches/debug_exceptions/patch-5-0'
14
- elsif ActionPack.version.to_s >= '4.2'
15
- require 'patches/debug_exceptions/patch-4-2'
16
- elsif ActionPack.version.to_s >= '4.1'
17
- require 'patches/debug_exceptions/patch-4-1'
18
14
  else
19
- # No patch required, because text rendering for
20
- # XHR requests was introduced with Rails 4.1:
21
- # https://github.com/rails/rails/pull/11960
15
+ # This gem supports Rails 5 or later
22
16
  end
23
17
  end
@@ -2,4 +2,8 @@ ActionDispatch::Request.class_eval do
2
2
  def inertia?
3
3
  key? 'HTTP_X_INERTIA'
4
4
  end
5
+
6
+ def inertia_partial?
7
+ key? 'HTTP_X_INERTIA_PARTIAL_DATA'
8
+ end
5
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Knoles
@@ -10,8 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-01-29 00:00:00.000000000 Z
13
+ date: 2020-11-20 00:00:00.000000000 Z
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '5'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '5'
15
29
  - !ruby/object:Gem::Dependency
16
30
  name: bundler
17
31
  requirement: !ruby/object:Gem::Requirement
@@ -32,30 +46,30 @@ dependencies:
32
46
  requirements:
33
47
  - - "~>"
34
48
  - !ruby/object:Gem::Version
35
- version: '10.0'
49
+ version: '13.0'
36
50
  type: :development
37
51
  prerelease: false
38
52
  version_requirements: !ruby/object:Gem::Requirement
39
53
  requirements:
40
54
  - - "~>"
41
55
  - !ruby/object:Gem::Version
42
- version: '10.0'
56
+ version: '13.0'
43
57
  - !ruby/object:Gem::Dependency
44
58
  name: rspec-rails
45
59
  requirement: !ruby/object:Gem::Requirement
46
60
  requirements:
47
61
  - - "~>"
48
62
  - !ruby/object:Gem::Version
49
- version: '3.0'
63
+ version: '4.0'
50
64
  type: :development
51
65
  prerelease: false
52
66
  version_requirements: !ruby/object:Gem::Requirement
53
67
  requirements:
54
68
  - - "~>"
55
69
  - !ruby/object:Gem::Version
56
- version: '3.0'
70
+ version: '4.0'
57
71
  - !ruby/object:Gem::Dependency
58
- name: rails
72
+ name: rails-controller-testing
59
73
  requirement: !ruby/object:Gem::Requirement
60
74
  requirements:
61
75
  - - ">="
@@ -69,7 +83,7 @@ dependencies:
69
83
  - !ruby/object:Gem::Version
70
84
  version: '0'
71
85
  - !ruby/object:Gem::Dependency
72
- name: rails-controller-testing
86
+ name: sqlite3
73
87
  requirement: !ruby/object:Gem::Requirement
74
88
  requirements:
75
89
  - - ">="
@@ -83,7 +97,7 @@ dependencies:
83
97
  - !ruby/object:Gem::Version
84
98
  version: '0'
85
99
  - !ruby/object:Gem::Dependency
86
- name: sqlite3
100
+ name: appraisal
87
101
  requirement: !ruby/object:Gem::Requirement
88
102
  requirements:
89
103
  - - ">="
@@ -108,6 +122,7 @@ files:
108
122
  - ".github/workflows/push.yml"
109
123
  - ".gitignore"
110
124
  - ".rspec"
125
+ - Appraisals
111
126
  - CHANGELOG.md
112
127
  - CODE_OF_CONDUCT.md
113
128
  - Gemfile
@@ -117,6 +132,10 @@ files:
117
132
  - app/views/inertia.html.erb
118
133
  - bin/console
119
134
  - bin/setup
135
+ - gemfiles/rails_5.0.gemfile
136
+ - gemfiles/rails_5.1.gemfile
137
+ - gemfiles/rails_5.2.gemfile
138
+ - gemfiles/rails_6.0.gemfile
120
139
  - inertia_rails.gemspec
121
140
  - lib/inertia_rails.rb
122
141
  - lib/inertia_rails/controller.rb
@@ -128,18 +147,16 @@ files:
128
147
  - lib/inertia_rails/version.rb
129
148
  - lib/patches/better_errors.rb
130
149
  - lib/patches/debug_exceptions.rb
131
- - lib/patches/debug_exceptions/patch-4-1.rb
132
- - lib/patches/debug_exceptions/patch-4-2.rb
133
150
  - lib/patches/debug_exceptions/patch-5-0.rb
134
151
  - lib/patches/debug_exceptions/patch-5-1.rb
135
152
  - lib/patches/request.rb
136
- homepage: https://github.com/inertiajs/inertia-rails/
153
+ homepage: https://github.com/inertiajs/inertia-rails
137
154
  licenses:
138
155
  - MIT
139
156
  metadata:
140
- homepage_uri: https://github.com/inertiajs/inertia-rails/
141
- source_code_uri: https://github.com/inertiajs/inertia-rails/
142
- changelog_uri: https://github.com/inertiajs/inertia-rails//CHANGELOG.md
157
+ homepage_uri: https://github.com/inertiajs/inertia-rails
158
+ source_code_uri: https://github.com/inertiajs/inertia-rails
159
+ changelog_uri: https://github.com/inertiajs/inertia-rails/CHANGELOG.md
143
160
  post_install_message:
144
161
  rdoc_options: []
145
162
  require_paths:
@@ -155,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
155
172
  - !ruby/object:Gem::Version
156
173
  version: '0'
157
174
  requirements: []
158
- rubygems_version: 3.0.3
175
+ rubygems_version: 3.1.4
159
176
  signing_key:
160
177
  specification_version: 4
161
178
  summary: Inertia adapter for Rails
@@ -1,41 +0,0 @@
1
- # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
- #
3
- # Original source:
4
- # https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5
- #
6
-
7
- ActionDispatch::DebugExceptions.class_eval do
8
- prepend(InertiaDebugExceptions = Module.new do
9
- def render_exception(env, exception)
10
- wrapper = ExceptionWrapper.new(env, exception)
11
- log_error(env, wrapper)
12
-
13
- if env['action_dispatch.show_detailed_exceptions']
14
- request = Request.new(env)
15
- template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
16
- request: request,
17
- exception: wrapper.exception,
18
- application_trace: wrapper.application_trace,
19
- framework_trace: wrapper.framework_trace,
20
- full_trace: wrapper.full_trace,
21
- routes_inspector: routes_inspector(exception),
22
- source_extract: wrapper.source_extract,
23
- line_number: wrapper.line_number,
24
- file: wrapper.file
25
- )
26
- file = "rescues/#{wrapper.rescue_template}"
27
-
28
- if request.xhr? && !request.headers['X-Inertia'] # <<<< this line is changed only
29
- body = template.render(template: file, layout: false, formats: [:text])
30
- format = "text/plain"
31
- else
32
- body = template.render(template: file, layout: 'rescues/layout')
33
- format = "text/html"
34
- end
35
- render(wrapper.status_code, body, format)
36
- else
37
- raise exception
38
- end
39
- end
40
- end)
41
- end
@@ -1,52 +0,0 @@
1
- # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
- #
3
- # Original source:
4
- # https://github.com/rails/rails/blob/4-2-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5
- #
6
-
7
- ActionDispatch::DebugExceptions.class_eval do
8
- prepend(InertiaDebugExceptions = Module.new do
9
- def render_exception(env, exception)
10
- wrapper = ExceptionWrapper.new(env, exception)
11
- log_error(env, wrapper)
12
-
13
- if env['action_dispatch.show_detailed_exceptions']
14
- request = Request.new(env)
15
- traces = wrapper.traces
16
-
17
- trace_to_show = 'Application Trace'
18
- if traces[trace_to_show].empty? && wrapper.rescue_template != 'routing_error'
19
- trace_to_show = 'Full Trace'
20
- end
21
-
22
- if source_to_show = traces[trace_to_show].first
23
- source_to_show_id = source_to_show[:id]
24
- end
25
-
26
- template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
27
- request: request,
28
- exception: wrapper.exception,
29
- traces: traces,
30
- show_source_idx: source_to_show_id,
31
- trace_to_show: trace_to_show,
32
- routes_inspector: routes_inspector(exception),
33
- source_extracts: wrapper.source_extracts,
34
- line_number: wrapper.line_number,
35
- file: wrapper.file
36
- )
37
- file = "rescues/#{wrapper.rescue_template}"
38
-
39
- if request.xhr? && !request.headers['X-Inertia'] # <<<< this line is changed only
40
- body = template.render(template: file, layout: false, formats: [:text])
41
- format = "text/plain"
42
- else
43
- body = template.render(template: file, layout: 'rescues/layout')
44
- format = "text/html"
45
- end
46
- render(wrapper.status_code, body, format)
47
- else
48
- raise exception
49
- end
50
- end
51
- end)
52
- end