inertia_rails 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ca1386958c2d3e3c0b0dfbab50c7e59f5a21144c1fc879e62493dcd54bc11ef5
4
- data.tar.gz: de0fc529f805b096ea8c567c2f9c78b9d30cd730aa2a8439f03b976d6e4e9a82
3
+ metadata.gz: b889c5a06cf6c9310b1d64c1e19a5e979aea7ba874c3014867a462386dd28937
4
+ data.tar.gz: c5e1bf21eb3ff8dc1e14ebadedf43bb82f8b1af6aa46680610ad4b0054d90b3b
5
5
  SHA512:
6
- metadata.gz: bb44ba24fb2f5ef1e020824a31b806840be221da969d73b19404db03c537ff1cb38cb8500b15dbe456ede6e0aec451fa06652b1b7bfa387f5ab6f0f007972f18
7
- data.tar.gz: 1db014d57a1a78a505e26b70b18f67e365b40829e7c8a847de29cd428ccb2882d6fb87a664758f6e77918cd7570132c05aecd102d5ed97eed5a72d73e68913cd
6
+ metadata.gz: 65a66ab26b16808a9b88467a6b579074f68ba68877c055b8d2e333422454041f3461ff4ab0a947f2a52572edd8bf1c88d62d6d32ec7a1c8bd6ca776158f756c0
7
+ data.tar.gz: 724961702c0bd5ffb53e427d8791f1064dcc080c4cbc253297f30207c0017065b0f2c12b4822c5969401582972810c8960fbc494884bebb32fe1df907e6a7d79
@@ -4,6 +4,12 @@ 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
+
7
13
  ## [1.5.0] - 2020-10-07
8
14
 
9
15
  * Test against multiple Rails versions in Github Actions
@@ -4,6 +4,13 @@ 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
@@ -17,5 +24,13 @@ module InertiaRails
17
24
  headers['X-Inertia-Location'] = url
18
25
  head :conflict
19
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
20
35
  end
21
36
  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
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.5.0"
2
+ VERSION = "1.6.0"
3
3
  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.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Knoles
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2020-10-07 00:00:00.000000000 Z
13
+ date: 2020-11-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -172,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
172
  - !ruby/object:Gem::Version
173
173
  version: '0'
174
174
  requirements: []
175
- rubygems_version: 3.0.3
175
+ rubygems_version: 3.1.4
176
176
  signing_key:
177
177
  specification_version: 4
178
178
  summary: Inertia adapter for Rails