inertia_rails 1.5.0 → 1.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/push.yml +1 -1
- data/Appraisals +4 -0
- data/CHANGELOG.md +22 -0
- data/app/controllers/inertia_rails/static_controller.rb +7 -0
- data/gemfiles/rails_6.1.gemfile +7 -0
- data/lib/inertia_rails.rb +1 -0
- data/lib/inertia_rails/controller.rb +29 -0
- data/lib/inertia_rails/inertia_rails.rb +5 -0
- data/lib/inertia_rails/lazy.rb +28 -0
- data/lib/inertia_rails/middleware.rb +61 -24
- data/lib/inertia_rails/renderer.rb +16 -8
- data/lib/inertia_rails/version.rb +1 -1
- data/lib/patches/mapper.rb +8 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a3b7e29654788cf3c7ac7307d83fd258d47c7ee5daa36745998c944ae31b472
|
4
|
+
data.tar.gz: 4070f2fb9ae9d253a94c98d7f8705f713248788b63611bfa30f36edaaa15a73e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2aa986d0e69a1db45d94ca921771e7ff82ec61c53008dbf756b9eed3728837d314ee773e12acd27a98fcb1372d67f44653fa749ef24b2959759849b2be925ae9
|
7
|
+
data.tar.gz: ab880524135f77d437aeb4c8dfa1e3e23ddc69db5d99a5adea6139d17331db7981f691921ae10b84d41122d06fc1a01a456af902ed3fcde704fb9d4bf1b9f08a
|
data/.github/workflows/push.yml
CHANGED
data/Appraisals
CHANGED
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,28 @@ 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.9.0] - 2021-01-17
|
8
|
+
|
9
|
+
* Added the same inertia awareness that redirect_to has to redirect_back
|
10
|
+
|
11
|
+
## [1.8.0] - 2020-12-08
|
12
|
+
|
13
|
+
* Add `inertia` route helper feature
|
14
|
+
|
15
|
+
## [1.7.1] - 2020-11-24
|
16
|
+
|
17
|
+
* Fix the definition for InertiaRails::Lazy to avoid an uninitialized constant error when booting an application.
|
18
|
+
|
19
|
+
## [1.7.0] - 2020-11-24
|
20
|
+
|
21
|
+
* Add support for "lazy" props while rendering. These are props that never compute on the initial page load. The only render during a partial update that calls for them explicitly.
|
22
|
+
|
23
|
+
## [1.6.0] - 2020-11-20
|
24
|
+
|
25
|
+
* 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.
|
26
|
+
* Set content type to json for Inertia responses
|
27
|
+
* Return the original response status with Inertia responses
|
28
|
+
|
7
29
|
## [1.5.0] - 2020-10-07
|
8
30
|
|
9
31
|
* Test against multiple Rails versions in Github Actions
|
data/lib/inertia_rails.rb
CHANGED
@@ -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
|
@@ -13,9 +20,31 @@ module InertiaRails
|
|
13
20
|
end
|
14
21
|
end
|
15
22
|
|
23
|
+
private
|
24
|
+
|
16
25
|
def inertia_location(url)
|
17
26
|
headers['X-Inertia-Location'] = url
|
18
27
|
head :conflict
|
19
28
|
end
|
29
|
+
|
30
|
+
def redirect_to(options = {}, response_options = {})
|
31
|
+
capture_inertia_errors(response_options)
|
32
|
+
super(options, response_options)
|
33
|
+
end
|
34
|
+
|
35
|
+
def redirect_back(fallback_location:, allow_other_host: true, **options)
|
36
|
+
capture_inertia_errors(options)
|
37
|
+
super(
|
38
|
+
fallback_location: fallback_location,
|
39
|
+
allow_other_host: allow_other_host,
|
40
|
+
**options,
|
41
|
+
)
|
42
|
+
end
|
43
|
+
|
44
|
+
def capture_inertia_errors(options)
|
45
|
+
if (inertia_errors = options.dig(:inertia, :errors))
|
46
|
+
session[:inertia_errors] = inertia_errors
|
47
|
+
end
|
48
|
+
end
|
20
49
|
end
|
21
50
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
# Needed for `thread_mattr_accessor`
|
2
2
|
require 'active_support/core_ext/module/attribute_accessors_per_thread'
|
3
|
+
require 'inertia_rails/lazy'
|
3
4
|
|
4
5
|
module InertiaRails
|
5
6
|
thread_mattr_accessor :threadsafe_shared_plain_data
|
@@ -36,6 +37,10 @@ module InertiaRails
|
|
36
37
|
self.shared_blocks = []
|
37
38
|
end
|
38
39
|
|
40
|
+
def self.lazy(value = nil, &block)
|
41
|
+
InertiaRails::Lazy.new(value, &block)
|
42
|
+
end
|
43
|
+
|
39
44
|
private
|
40
45
|
|
41
46
|
module Configuration
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module InertiaRails
|
2
|
+
class Lazy
|
3
|
+
def initialize(value = nil, &block)
|
4
|
+
@value = value
|
5
|
+
@block = block
|
6
|
+
end
|
7
|
+
|
8
|
+
def call
|
9
|
+
to_proc.call
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_proc
|
13
|
+
# This is called by controller.instance_exec, which changes self to the
|
14
|
+
# controller instance. That makes the instance variables unavailable to the
|
15
|
+
# proc via closure. Copying the instance variables to local variables before
|
16
|
+
# the proc is returned keeps them in scope for the returned proc.
|
17
|
+
value = @value
|
18
|
+
block = @block
|
19
|
+
if value.respond_to?(:call)
|
20
|
+
value
|
21
|
+
elsif value
|
22
|
+
Proc.new { value }
|
23
|
+
else
|
24
|
+
block
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
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
|
36
|
+
|
37
|
+
def non_get_redirectable_method?
|
32
38
|
['PUT', 'PATCH', 'DELETE'].include? request_method
|
33
39
|
end
|
34
|
-
|
35
|
-
def
|
36
|
-
|
37
|
-
|
38
|
-
|
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:
|
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
|
@@ -27,13 +27,13 @@ module InertiaRails
|
|
27
27
|
private
|
28
28
|
|
29
29
|
def props
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
30
|
+
_props = ::InertiaRails.shared_data(@controller).merge(@props).select do |key, prop|
|
31
|
+
if rendering_partial_component?
|
32
|
+
key.in? partial_keys
|
33
|
+
else
|
34
|
+
!prop.is_a?(InertiaRails::Lazy)
|
35
|
+
end
|
36
|
+
end
|
37
37
|
|
38
38
|
deep_transform_values(_props, lambda {|prop| prop.respond_to?(:call) ? @controller.instance_exec(&prop) : prop })
|
39
39
|
end
|
@@ -52,5 +52,13 @@ module InertiaRails
|
|
52
52
|
|
53
53
|
hash.transform_values {|value| deep_transform_values(value, proc)}
|
54
54
|
end
|
55
|
+
|
56
|
+
def partial_keys
|
57
|
+
(@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)
|
58
|
+
end
|
59
|
+
|
60
|
+
def rendering_partial_component?
|
61
|
+
@request.inertia_partial? && @request.headers['X-Inertia-Partial-Component'] == component
|
62
|
+
end
|
55
63
|
end
|
56
64
|
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.
|
4
|
+
version: 1.9.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:
|
13
|
+
date: 2021-01-17 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -129,6 +129,7 @@ files:
|
|
129
129
|
- LICENSE.txt
|
130
130
|
- README.md
|
131
131
|
- Rakefile
|
132
|
+
- app/controllers/inertia_rails/static_controller.rb
|
132
133
|
- app/views/inertia.html.erb
|
133
134
|
- bin/console
|
134
135
|
- bin/setup
|
@@ -136,11 +137,13 @@ files:
|
|
136
137
|
- gemfiles/rails_5.1.gemfile
|
137
138
|
- gemfiles/rails_5.2.gemfile
|
138
139
|
- gemfiles/rails_6.0.gemfile
|
140
|
+
- gemfiles/rails_6.1.gemfile
|
139
141
|
- inertia_rails.gemspec
|
140
142
|
- lib/inertia_rails.rb
|
141
143
|
- lib/inertia_rails/controller.rb
|
142
144
|
- lib/inertia_rails/engine.rb
|
143
145
|
- lib/inertia_rails/inertia_rails.rb
|
146
|
+
- lib/inertia_rails/lazy.rb
|
144
147
|
- lib/inertia_rails/middleware.rb
|
145
148
|
- lib/inertia_rails/renderer.rb
|
146
149
|
- lib/inertia_rails/rspec.rb
|
@@ -149,6 +152,7 @@ files:
|
|
149
152
|
- lib/patches/debug_exceptions.rb
|
150
153
|
- lib/patches/debug_exceptions/patch-5-0.rb
|
151
154
|
- lib/patches/debug_exceptions/patch-5-1.rb
|
155
|
+
- lib/patches/mapper.rb
|
152
156
|
- lib/patches/request.rb
|
153
157
|
homepage: https://github.com/inertiajs/inertia-rails
|
154
158
|
licenses:
|
@@ -172,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
172
176
|
- !ruby/object:Gem::Version
|
173
177
|
version: '0'
|
174
178
|
requirements: []
|
175
|
-
rubygems_version: 3.
|
179
|
+
rubygems_version: 3.1.4
|
176
180
|
signing_key:
|
177
181
|
specification_version: 4
|
178
182
|
summary: Inertia adapter for Rails
|