inertia_rails 1.9.0 → 1.11.1

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: 4a3b7e29654788cf3c7ac7307d83fd258d47c7ee5daa36745998c944ae31b472
4
- data.tar.gz: 4070f2fb9ae9d253a94c98d7f8705f713248788b63611bfa30f36edaaa15a73e
3
+ metadata.gz: 60a2f2a20da36323f792051d3a0c1327b3f085f78e8fed5906064e8892c516d9
4
+ data.tar.gz: c9a40fd17509def36b88862a671f98a4ed839f158ffd0959a66aba3023ee6084
5
5
  SHA512:
6
- metadata.gz: 2aa986d0e69a1db45d94ca921771e7ff82ec61c53008dbf756b9eed3728837d314ee773e12acd27a98fcb1372d67f44653fa749ef24b2959759849b2be925ae9
7
- data.tar.gz: ab880524135f77d437aeb4c8dfa1e3e23ddc69db5d99a5adea6139d17331db7981f691921ae10b84d41122d06fc1a01a456af902ed3fcde704fb9d4bf1b9f08a
6
+ metadata.gz: c136e534632a852b4d92ec7303cb5eba67fed88706cf28cc16e8bc541fda5320f9f09aa3e92f481951fe93390e30714813f0950bd67b75389b933634721d8dd5
7
+ data.tar.gz: b6f8568840f971249c90142afd6b20adf39b4d99a4864d952ddbedcbfdfb49884a6e35f9c419fd180d7abf50590bee70863f3766b94e6883a171b8995cef5f9e
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.11.1] - 2021-06-27
8
+
9
+ * Fixed thread safety in the middleware. Thanks @caifara!
10
+
11
+ ## [1.11.0] - 2021-03-23
12
+
13
+ * Fixed the install generator. `installable?` was always returning false, preventing it from actually running.
14
+ * Added an install generator for Vue.
15
+
16
+ ## [1.10.0] - 2021-03-22
17
+
18
+ * Added install generator to quickly add Inertia to existing rails apps via `rails inertia_rails:install:react`
19
+
20
+ ## [1.9.2] - 2021-02-23
21
+
22
+ * Improve method for detecting whether a user used the RSpec helpers without adding `inertia: true` to the spec
23
+ * Emit a warning when expecting an Inertia response in RSpec and never reaching a `render inertia:` call
24
+
25
+ ## [1.9.1] - 2021-02-10
26
+
27
+ * Define `redirect_to` and `redirect_back` as public methods for compatibility with other code using them
28
+
7
29
  ## [1.9.0] - 2021-01-17
8
30
 
9
31
  * Added the same inertia awareness that redirect_to has to redirect_back
data/README.md CHANGED
@@ -1,14 +1,10 @@
1
- # Inertia.js Rails Adapter
2
-
3
- Visit [inertiajs.com](https://inertiajs.com/) to learn more.
1
+ ![image](https://user-images.githubusercontent.com/6599653/114456558-032e2200-9bab-11eb-88bc-a19897f417ba.png)
4
2
 
5
- # Note to pre Rubygems release users
6
3
 
7
- The initial version of the gem was named `inertia`; however, that name was not available on Rubygems.
4
+ # Inertia.js Rails Adapter
8
5
 
9
- The 1.0.0 version release on Rubygems is `inertia_rails`.
6
+ Visit [inertiajs.com](https://inertiajs.com/) for installation instructions, documentation, and to learn more.
10
7
 
11
- The changes required are:
8
+ *Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)*
12
9
 
13
- 1. Use `gem 'inertia_rails'` in your Gemfile (or `gem install inertia_rails`)
14
- 2. Change any `Inertia.configure` calls to `InertiaRails.configure`
10
+ [![bellaWatt Logo](https://user-images.githubusercontent.com/6599653/114456832-5607d980-9bab-11eb-99c8-ab39867c384e.png)](https://bellawatt.com/)
@@ -33,4 +33,5 @@ Gem::Specification.new do |spec|
33
33
  spec.add_development_dependency "rails-controller-testing"
34
34
  spec.add_development_dependency "sqlite3"
35
35
  spec.add_development_dependency "appraisal"
36
+ spec.add_development_dependency "responders"
36
37
  end
@@ -0,0 +1,7 @@
1
+ class InertiaExampleController < ApplicationController
2
+ def index
3
+ render inertia: 'InertiaExample', props: {
4
+ name: 'World',
5
+ }
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ import React from 'react';
2
+
3
+ const InertiaExample = ({name}) => (
4
+ <>
5
+ <h1>Hello {name}!</h1>
6
+ </>
7
+ );
8
+
9
+ export default InertiaExample;
@@ -0,0 +1,21 @@
1
+ import { App } from '@inertiajs/inertia-react';
2
+ import React from 'react';
3
+ import { render } from 'react-dom';
4
+ import axios from 'axios';
5
+ import { InertiaProgress } from '@inertiajs/progress';
6
+
7
+ document.addEventListener('DOMContentLoaded', () => {
8
+ InertiaProgress.init();
9
+ const el = document.getElementById('app')
10
+
11
+ const csrfToken = document.querySelector('meta[name=csrf-token]').content;
12
+ axios.defaults.headers.common['X-CSRF-Token'] = csrfToken;
13
+
14
+ render(
15
+ <App
16
+ initialPage={JSON.parse(el.dataset.page)}
17
+ resolveComponent={name => require(`../Pages/${name}`).default}
18
+ />,
19
+ el
20
+ )
21
+ });
@@ -0,0 +1,11 @@
1
+ <template>
2
+ <h1 :style="{ 'text-align': 'center' }">Hello {{name}}!</h1>
3
+ </template>
4
+
5
+ <script>
6
+ export default {
7
+ props: {
8
+ name: { type: String, required: true },
9
+ },
10
+ }
11
+ </script>
@@ -0,0 +1,24 @@
1
+ import axios from 'axios'
2
+ import Vue from 'vue'
3
+
4
+ import { app, plugin } from '@inertiajs/inertia-vue'
5
+ import { InertiaProgress } from '@inertiajs/progress'
6
+
7
+ document.addEventListener('DOMContentLoaded', () => {
8
+ const csrfToken = document.querySelector('meta[name=csrf-token]').content
9
+ axios.defaults.headers.common['X-CSRF-Token'] = csrfToken
10
+
11
+ InertiaProgress.init();
12
+ const el = document.getElementById('app')
13
+
14
+ Vue.use(plugin)
15
+
16
+ new Vue({
17
+ render: h => h(app, {
18
+ props: {
19
+ initialPage: JSON.parse(el.dataset.page),
20
+ resolveComponent: name => require(`../Pages/${name}`).default,
21
+ },
22
+ }),
23
+ }).$mount(el)
24
+ })
@@ -0,0 +1,74 @@
1
+ module InertiaRails
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('./install', __dir__)
4
+ class_option :front_end, type: :string, default: 'react'
5
+
6
+ FRONT_END_INSTALLERS = [
7
+ 'react',
8
+ 'vue',
9
+ ]
10
+
11
+ def install
12
+ exit! unless installable?
13
+
14
+ install_base!
15
+
16
+ send "install_#{options[:front_end]}!"
17
+
18
+ say "You're all set! Run rails s and checkout localhost:3000/inertia-example", :green
19
+ end
20
+
21
+ protected
22
+
23
+ def installable?
24
+ unless run("./bin/rails webpacker:verify_install")
25
+ say "Sorry, you need to have webpacker installed for inertia_rails default setup.", :red
26
+ return false
27
+ end
28
+
29
+ unless options[:front_end].in? FRONT_END_INSTALLERS
30
+ say "Sorry, there is no generator for #{options[:front_end]}!\n\n", :red
31
+ say "If you are a #{options[:front_end]} developer, please help us improve inertia_rails by contributing an installer.\n\n"
32
+ say "https://github.com/inertiajs/inertia-rails/\n\n"
33
+
34
+ return false
35
+ end
36
+
37
+ true
38
+ end
39
+
40
+ def install_base!
41
+ say "Adding inertia pack tag to application layout", :blue
42
+ insert_into_file Rails.root.join("app/views/layouts/application.html.erb").to_s, after: "<%= javascript_pack_tag 'application' %>\n" do
43
+ "\t\t<%= javascript_pack_tag 'inertia' %>\n"
44
+ end
45
+
46
+ say "Installing inertia client packages", :blue
47
+ run "yarn add @inertiajs/inertia @inertiajs/progress"
48
+
49
+ say "Copying example files", :blue
50
+ template "controller.rb", Rails.root.join("app/controllers/inertia_example_controller.rb").to_s
51
+
52
+ say "Adding a route for the example inertia controller...", :blue
53
+ route "get 'inertia-example', to: 'inertia_example#index'"
54
+ end
55
+
56
+ def install_react!
57
+ say "Creating a React page component...", :blue
58
+ run 'yarn add @inertiajs/inertia-react'
59
+ template "react/InertiaExample.jsx", Rails.root.join("app/javascript/Pages/InertiaExample.js").to_s
60
+ say "Copying inertia.jsx into webpacker's packs folder...", :blue
61
+ template "react/inertia.jsx", Rails.root.join("app/javascript/packs/inertia.jsx").to_s
62
+ say "done!", :green
63
+ end
64
+
65
+ def install_vue!
66
+ say "Creating a Vue page component...", :blue
67
+ run 'yarn add @inertiajs/inertia-vue'
68
+ template "vue/InertiaExample.vue", Rails.root.join("app/javascript/Pages/InertiaExample.vue").to_s
69
+ say "Copying inertia.js into webpacker's packs folder...", :blue
70
+ template "vue/inertia.js", Rails.root.join("app/javascript/packs/inertia.js").to_s
71
+ say "done!", :green
72
+ end
73
+ end
74
+ end
@@ -20,13 +20,6 @@ module InertiaRails
20
20
  end
21
21
  end
22
22
 
23
- private
24
-
25
- def inertia_location(url)
26
- headers['X-Inertia-Location'] = url
27
- head :conflict
28
- end
29
-
30
23
  def redirect_to(options = {}, response_options = {})
31
24
  capture_inertia_errors(response_options)
32
25
  super(options, response_options)
@@ -41,6 +34,13 @@ module InertiaRails
41
34
  )
42
35
  end
43
36
 
37
+ private
38
+
39
+ def inertia_location(url)
40
+ headers['X-Inertia-Location'] = url
41
+ head :conflict
42
+ end
43
+
44
44
  def capture_inertia_errors(options)
45
45
  if (inertia_errors = options.dig(:inertia, :errors))
46
46
  session[:inertia_errors] = inertia_errors
@@ -5,79 +5,91 @@ module InertiaRails
5
5
  end
6
6
 
7
7
  def call(env)
8
- @env = env
8
+ InertiaRailsRequest.new(@app, env)
9
+ .response
10
+ end
9
11
 
10
- status, headers, body = @app.call(env)
11
- request = ActionDispatch::Request.new(env)
12
+ class InertiaRailsRequest
13
+ def initialize(app, env)
14
+ @app = app
15
+ @env = env
16
+ end
12
17
 
13
- ::InertiaRails.reset!
18
+ def response
19
+ status, headers, body = @app.call(@env)
20
+ request = ActionDispatch::Request.new(@env)
14
21
 
15
- # Inertia errors are added to the session via redirect_to
16
- request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
22
+ ::InertiaRails.reset!
17
23
 
18
- status = 303 if inertia_non_post_redirect?(status)
24
+ # Inertia errors are added to the session via redirect_to
25
+ request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
19
26
 
20
- return stale_inertia_get? ? force_refresh(request) : [status, headers, body]
21
- end
27
+ status = 303 if inertia_non_post_redirect?(status)
22
28
 
23
- private
29
+ stale_inertia_get? ? force_refresh(request) : [status, headers, body]
30
+ end
24
31
 
25
- def keep_inertia_errors?(status)
26
- redirect_status?(status) || stale_inertia_request?
27
- end
32
+ private
28
33
 
29
- def stale_inertia_request?
30
- inertia_request? && version_stale?
31
- end
34
+ def keep_inertia_errors?(status)
35
+ redirect_status?(status) || stale_inertia_request?
36
+ end
32
37
 
33
- def redirect_status?(status)
34
- [301, 302].include? status
35
- end
38
+ def stale_inertia_request?
39
+ inertia_request? && version_stale?
40
+ end
36
41
 
37
- def non_get_redirectable_method?
38
- ['PUT', 'PATCH', 'DELETE'].include? request_method
39
- end
42
+ def redirect_status?(status)
43
+ [301, 302].include? status
44
+ end
40
45
 
41
- def inertia_non_post_redirect?(status)
42
- inertia_request? && redirect_status?(status) && non_get_redirectable_method?
43
- end
46
+ def non_get_redirectable_method?
47
+ ['PUT', 'PATCH', 'DELETE'].include? request_method
48
+ end
44
49
 
45
- def stale_inertia_get?
46
- get? && stale_inertia_request?
47
- end
50
+ def inertia_non_post_redirect?(status)
51
+ inertia_request? && redirect_status?(status) && non_get_redirectable_method?
52
+ end
48
53
 
49
- def get?
50
- request_method == 'GET'
51
- end
54
+ def stale_inertia_get?
55
+ get? && stale_inertia_request?
56
+ end
52
57
 
53
- def request_method
54
- @env['REQUEST_METHOD']
55
- end
58
+ def get?
59
+ request_method == 'GET'
60
+ end
56
61
 
57
- def inertia_version
58
- @env['HTTP_X_INERTIA_VERSION']
59
- end
62
+ def request_method
63
+ @env['REQUEST_METHOD']
64
+ end
60
65
 
61
- def inertia_request?
62
- @env['HTTP_X_INERTIA'].present?
63
- end
66
+ def inertia_version
67
+ @env['HTTP_X_INERTIA_VERSION']
68
+ end
64
69
 
65
- def version_stale?
66
- sent_version != saved_version
67
- end
70
+ def inertia_request?
71
+ @env['HTTP_X_INERTIA'].present?
72
+ end
68
73
 
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
74
+ def version_stale?
75
+ sent_version != saved_version
76
+ end
73
77
 
74
- def saved_version
75
- InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
76
- end
78
+ def sent_version
79
+ return nil if inertia_version.nil?
80
+
81
+ InertiaRails.version.is_a?(Numeric) ? inertia_version.to_f : inertia_version
82
+ end
77
83
 
78
- def force_refresh(request)
79
- request.flash.keep
80
- Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
84
+ def saved_version
85
+ InertiaRails.version.is_a?(Numeric) ? InertiaRails.version.to_f : InertiaRails.version
86
+ end
87
+
88
+ def force_refresh(request)
89
+ request.flash.keep
90
+ Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
91
+ end
81
92
  end
82
93
  end
83
94
  end
95
+
@@ -35,6 +35,9 @@ module InertiaRails
35
35
  def inertia
36
36
  raise 'Inertia test helpers aren\'t set up! Make sure you add inertia: true to describe blocks using inertia tests.' unless inertia_tests_setup?
37
37
 
38
+ if @_inertia_render_wrapper.nil? && !::RSpec.configuration.inertia[:skip_missing_renderer_warnings]
39
+ warn 'WARNING: the test never created an Inertia renderer. Maybe the code wasn\'t able to reach a `render inertia:` call? If this was intended, or you don\'t want to see this message, set ::RSpec.configuration.inertia[:skip_missing_renderer_warnings] = true'
40
+ end
38
41
  @_inertia_render_wrapper
39
42
  end
40
43
 
@@ -49,7 +52,7 @@ module InertiaRails
49
52
  protected
50
53
 
51
54
  def inertia_tests_setup?
52
- @_inertia_render_wrapper.present?
55
+ ::RSpec.current_example.metadata.fetch(:inertia, false)
53
56
  end
54
57
  end
55
58
  end
@@ -57,6 +60,9 @@ end
57
60
 
58
61
  RSpec.configure do |config|
59
62
  config.include ::InertiaRails::RSpec::Helpers
63
+ config.add_setting :inertia, default: {
64
+ skip_missing_renderer_warnings: false
65
+ }
60
66
 
61
67
  config.before(:each, inertia: true) do
62
68
  new_renderer = InertiaRails::Renderer.method(:new)
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.9.0"
2
+ VERSION = "1.11.1"
3
3
  end
@@ -0,0 +1,16 @@
1
+ namespace :inertia_rails do
2
+ namespace :install do
3
+ desc "Installs inertia_rails packages and configurations for a React based app"
4
+ task :react => :environment do
5
+ system 'rails g inertia_rails:install --front_end react'
6
+ end
7
+ desc "Installs inertia_rails packages and configurations for a Vue based app"
8
+ task vue: :environment do
9
+ system 'rails g inertia_rails:install --front_end vue'
10
+ end
11
+ desc "Installs inertia_rails packages and configurations for a Svelte based app"
12
+ task svelte: :environment do
13
+ system 'rails g inertia_rails:install --front_end svelte'
14
+ end
15
+ end
16
+ 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.9.0
4
+ version: 1.11.1
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: 2021-01-17 00:00:00.000000000 Z
13
+ date: 2021-06-27 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -110,6 +110,20 @@ dependencies:
110
110
  - - ">="
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: responders
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ type: :development
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
113
127
  description:
114
128
  email:
115
129
  - brain@bellawatt.com
@@ -139,6 +153,12 @@ files:
139
153
  - gemfiles/rails_6.0.gemfile
140
154
  - gemfiles/rails_6.1.gemfile
141
155
  - inertia_rails.gemspec
156
+ - lib/generators/inertia_rails/install/controller.rb
157
+ - lib/generators/inertia_rails/install/react/InertiaExample.jsx
158
+ - lib/generators/inertia_rails/install/react/inertia.jsx
159
+ - lib/generators/inertia_rails/install/vue/InertiaExample.vue
160
+ - lib/generators/inertia_rails/install/vue/inertia.js
161
+ - lib/generators/inertia_rails/install_generator.rb
142
162
  - lib/inertia_rails.rb
143
163
  - lib/inertia_rails/controller.rb
144
164
  - lib/inertia_rails/engine.rb
@@ -154,6 +174,7 @@ files:
154
174
  - lib/patches/debug_exceptions/patch-5-1.rb
155
175
  - lib/patches/mapper.rb
156
176
  - lib/patches/request.rb
177
+ - lib/tasks/inertia_rails.rake
157
178
  homepage: https://github.com/inertiajs/inertia-rails
158
179
  licenses:
159
180
  - MIT