inertia_rails 1.4.1 → 1.8.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: 9cd358fa9c397d7899a6d5856840c8a2d3f9d375ebfb080cc8255a7c8fdeb9e6
4
- data.tar.gz: 7fbc5d37523fda2f27781b6d39765319d8bb5cba876fd0b5a02af7ea7ece80f5
3
+ metadata.gz: 0c9c8c3b0d07a467179b741d6ce1f9d79d6190f0c71ecbe3e960536929055caf
4
+ data.tar.gz: 2f0f3a5ac49480f5fae1b28c771fe7bc3ea6de2953c734e0015b617fcc70a8bc
5
5
  SHA512:
6
- metadata.gz: d22c0fa1f8ce6f8f069e9dc72705f46f9a4ea7cf02540967aed17dbd8f548e11e096ed85c8f0cd3dd1b0ff28071c18c1687f22b8ec7cc847be355fbbe588930b
7
- data.tar.gz: a7bb964c860efa67cd9c3647a38eb981eba239facea5578c6250b3c611ccb727c1691baeefd7357392f81300166d2a475cbda21cf2fa955238ede06ee1bdaa06
6
+ metadata.gz: d696ea9c18535afb527dfead16bd271613d2bb561936ffd470e85caf78e20138109c6247d2e21724751a94acafc5548849533e50da3774d510528ed6f1221bc4
7
+ data.tar.gz: 6aa06ca052045c5f27c8f1a801889cf26ed77bb5e660875e6fe74cd3c7a30eab7c306385b9707842c8a52a8131c797405741a5f34e5397a64b58da2e98c5ae62
@@ -8,9 +8,10 @@ jobs:
8
8
  fail-fast: false
9
9
  matrix:
10
10
  ruby: [2.6, 2.7]
11
+ rails: ['5.0', '5.1', '5.2', '6.0']
11
12
 
12
13
  runs-on: ubuntu-latest
13
- name: Test against Ruby ${{ matrix.ruby }}
14
+ name: Test against Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
14
15
 
15
16
  steps:
16
17
  - uses: actions/checkout@v2
@@ -24,7 +25,10 @@ jobs:
24
25
  ruby-version: ${{ matrix.ruby }}
25
26
 
26
27
  - name: Install gems
28
+ env:
29
+ MATRIX_RAILS_VERSION: ${{ matrix.rails }}
27
30
  run: |
31
+ export BUNDLE_GEMFILE="${GITHUB_WORKSPACE}/gemfiles/rails_${MATRIX_RAILS_VERSION}.gemfile"
28
32
  gem install bundler
29
33
  bundle install --jobs 4 --retry 3
30
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,29 @@ 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.8.0] - 2020-12-08
8
+
9
+ * Add `inertia` route helper feature
10
+
11
+ ## [1.7.1] - 2020-11-24
12
+
13
+ * Fix the definition for InertiaRails::Lazy to avoid an uninitialized constant error when booting an application.
14
+
15
+ ## [1.7.0] - 2020-11-24
16
+
17
+ * 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.
18
+
19
+ ## [1.6.0] - 2020-11-20
20
+
21
+ * 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.
22
+ * Set content type to json for Inertia responses
23
+ * Return the original response status with Inertia responses
24
+
25
+ ## [1.5.0] - 2020-10-07
26
+
27
+ * Test against multiple Rails versions in Github Actions
28
+ * Add the `inertia_location` controller method that forces a full page refresh
29
+
7
30
  ## [1.4.1] - 2020-08-06
8
31
 
9
32
  * Fixed a bug involving threadsafe versions and layouts
@@ -0,0 +1,7 @@
1
+ module InertiaRails
2
+ class StaticController < ::ApplicationController
3
+ def static
4
+ render inertia: params[:component]
5
+ end
6
+ end
7
+ end
@@ -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: "../"
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "rspec-rails", "~> 4.0"
33
33
  spec.add_development_dependency "rails-controller-testing"
34
34
  spec.add_development_dependency "sqlite3"
35
+ spec.add_development_dependency "appraisal"
35
36
  end
@@ -4,6 +4,7 @@ require 'inertia_rails/engine'
4
4
  require 'patches/debug_exceptions'
5
5
  require 'patches/better_errors'
6
6
  require 'patches/request'
7
+ require 'patches/mapper'
7
8
 
8
9
  ActionController::Renderers.add :inertia do |component, options|
9
10
  InertiaRails::Renderer.new(
@@ -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
@@ -12,5 +19,18 @@ module InertiaRails
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,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
- 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
@@ -27,13 +27,13 @@ module InertiaRails
27
27
  private
28
28
 
29
29
  def props
30
- only = (@request.headers['X-Inertia-Partial-Data'] || '').split(',').compact.map(&:to_sym)
31
-
32
- _props = ::InertiaRails.shared_data(@controller).merge(@props)
33
-
34
- _props = (only.any? && @request.headers['X-Inertia-Partial-Component'] == component) ?
35
- _props.select {|key| key.in? only} :
36
- _props
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
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.4.1"
2
+ VERSION = "1.8.0"
3
3
  end
@@ -0,0 +1,8 @@
1
+ ActionDispatch::Routing::Mapper.class_eval do
2
+ def inertia(args, &block)
3
+ route = args.keys.first
4
+ component = args.values.first
5
+
6
+ get(route => 'inertia_rails/static#static', defaults: {component: component})
7
+ end
8
+ 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.1
4
+ version: 1.8.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-08-06 00:00:00.000000000 Z
13
+ date: 2020-12-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -96,6 +96,20 @@ dependencies:
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
98
  version: '0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: appraisal
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ type: :development
107
+ prerelease: false
108
+ version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
99
113
  description:
100
114
  email:
101
115
  - brain@bellawatt.com
@@ -108,20 +122,27 @@ 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
114
129
  - LICENSE.txt
115
130
  - README.md
116
131
  - Rakefile
132
+ - app/controllers/inertia_rails/static_controller.rb
117
133
  - app/views/inertia.html.erb
118
134
  - bin/console
119
135
  - bin/setup
136
+ - gemfiles/rails_5.0.gemfile
137
+ - gemfiles/rails_5.1.gemfile
138
+ - gemfiles/rails_5.2.gemfile
139
+ - gemfiles/rails_6.0.gemfile
120
140
  - inertia_rails.gemspec
121
141
  - lib/inertia_rails.rb
122
142
  - lib/inertia_rails/controller.rb
123
143
  - lib/inertia_rails/engine.rb
124
144
  - lib/inertia_rails/inertia_rails.rb
145
+ - lib/inertia_rails/lazy.rb
125
146
  - lib/inertia_rails/middleware.rb
126
147
  - lib/inertia_rails/renderer.rb
127
148
  - lib/inertia_rails/rspec.rb
@@ -130,6 +151,7 @@ files:
130
151
  - lib/patches/debug_exceptions.rb
131
152
  - lib/patches/debug_exceptions/patch-5-0.rb
132
153
  - lib/patches/debug_exceptions/patch-5-1.rb
154
+ - lib/patches/mapper.rb
133
155
  - lib/patches/request.rb
134
156
  homepage: https://github.com/inertiajs/inertia-rails
135
157
  licenses:
@@ -153,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
175
  - !ruby/object:Gem::Version
154
176
  version: '0'
155
177
  requirements: []
156
- rubygems_version: 3.0.3
178
+ rubygems_version: 3.1.4
157
179
  signing_key:
158
180
  specification_version: 4
159
181
  summary: Inertia adapter for Rails