inertia_rails 1.11.1 → 2.0.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: 60a2f2a20da36323f792051d3a0c1327b3f085f78e8fed5906064e8892c516d9
4
- data.tar.gz: c9a40fd17509def36b88862a671f98a4ed839f158ffd0959a66aba3023ee6084
3
+ metadata.gz: ac66be3f6a11b3caa5d6fec8057f75e4be257e154b3ce77f3f2c7b8ddb38d4bc
4
+ data.tar.gz: cd9891492a338447f41f10016656c369d8870dd71eacd55ce8d4c6be92f08464
5
5
  SHA512:
6
- metadata.gz: c136e534632a852b4d92ec7303cb5eba67fed88706cf28cc16e8bc541fda5320f9f09aa3e92f481951fe93390e30714813f0950bd67b75389b933634721d8dd5
7
- data.tar.gz: b6f8568840f971249c90142afd6b20adf39b4d99a4864d952ddbedcbfdfb49884a6e35f9c419fd180d7abf50590bee70863f3766b94e6883a171b8995cef5f9e
6
+ metadata.gz: '09beccfde8d305ae45d9afa84d9ace59337e7ae6521984f3047c10723c2ff70dcf01fe66667e51b4ab3bdd52c6d09a915d9edaace24d5aab83db571aa60fca10'
7
+ data.tar.gz: 7f25e813e9bec9bd513f2608bd5892728a504b76b2cab5fb43d2067af78411a12fe02baab6f0dded2591fb095536d6dda5ee7f65d6c4691c1affdce767458c80
@@ -8,7 +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', '6.1']
11
+ rails: ['5.1', '5.2', '6.0', '6.1', '7.0']
12
+ exclude:
13
+ - rails: '7.0'
14
+ ruby: 2.6
12
15
 
13
16
  runs-on: ubuntu-latest
14
17
  name: Test against Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
data/Appraisals CHANGED
@@ -1,3 +1,7 @@
1
+ appraise "rails-7.0" do
2
+ gem "rails", "~> 7.0"
3
+ end
4
+
1
5
  appraise "rails-6.1" do
2
6
  gem "rails", "~> 6.1.0"
3
7
  end
@@ -13,7 +17,3 @@ end
13
17
  appraise "rails-5.1" do
14
18
  gem "rails", "~> 5.1.7"
15
19
  end
16
-
17
- appraise "rails-5.0" do
18
- gem "rails", "~> 5.0.7", '>= 5.0.7.2'
19
- end
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ 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
+ ## [2.0.0] - 2022-06-20
8
+
9
+ * Fix an issue with Rails 7.0. Thanks @0xDing and @aviemet!
10
+ * Drop support for Rails 5.0 (and mentally, though not literally drop support for Rails < 6)
11
+
12
+ ## [1.12.1] - 2022-05-09
13
+
14
+ * Allow inertia to take over after initial pageload when using ssr. Thanks @99monkey!
15
+
16
+ ## [1.12.0] - 2022-05-04
17
+
18
+ * SSR!
19
+
7
20
  ## [1.11.1] - 2021-06-27
8
21
 
9
22
  * Fixed thread safety in the middleware. Thanks @caifara!
data/README.md CHANGED
@@ -3,7 +3,136 @@
3
3
 
4
4
  # Inertia.js Rails Adapter
5
5
 
6
- Visit [inertiajs.com](https://inertiajs.com/) for installation instructions, documentation, and to learn more.
6
+ ## Installation
7
+
8
+ ### Backend
9
+
10
+ Just add the inertia rails gem to your Gemfile
11
+ ```ruby
12
+ gem 'inertia_rails'
13
+ ```
14
+
15
+ ### Frontend
16
+
17
+ Rails 7 specific frontend docs coming soon. For now, check out the official Inertia docs at https://inertiajs.com/
18
+
19
+ ## Usage
20
+
21
+ ### Responses
22
+
23
+ Render Inertia responses is simple, just use the inertia renderer in your controller methods. The renderer accepts two arguments, the first is the name of the component you want to render from within your pages directory (without extension). The second argument is an options hash where you can provide `props` to your components. This options hash also allows you to pass `view_data` to your layout, but this is much less common.
24
+
25
+ ```ruby
26
+ def index
27
+ render inertia: 'Event/Index', props: {
28
+ events: Event.all,
29
+ }
30
+ end
31
+ ```
32
+
33
+ ### Shared Data
34
+
35
+ If you have data that you want to be provided as a prop to every component (a common use-case is informationa about the authenticated user) you can use the `shared_data` controller method.
36
+
37
+ ```ruby
38
+ class EventsController < ApplicationController
39
+ # share syncronously
40
+ inertia_share app_name: env['app.name']
41
+
42
+ # share lazily, evaluated at render time
43
+ inertia_share do
44
+ if logged_in?
45
+ {
46
+ user: logged_in_user,
47
+ }
48
+ end
49
+ end
50
+
51
+ # share lazily alternate syntax
52
+ inertia_share user_count: lambda { User.count }
53
+
54
+ end
55
+ ```
56
+
57
+ ### Routing
58
+
59
+ If you don't need a controller to handle a static component, you can route directly to a component with the inertia route helper
60
+
61
+ ```ruby
62
+ inertia 'about' => 'AboutComponent'
63
+ ```
64
+
65
+ ## Configuration
66
+
67
+ Inertia Rails has a few different configuration options that can be set anywhere, but the most common location is from within an initializer.
68
+
69
+ The default config is shown below
70
+ ```ruby
71
+ InertiaRails.configure do |config|
72
+
73
+ # set the current version for automatic asset refreshing. A string value should be used if any.
74
+ config.version = nil
75
+
76
+ # set the layout you want inertia components to be rendered within. This layout must include any required inertia javascript.
77
+ config.layout = 'application'
78
+
79
+ # ssr specific options
80
+ config.ssr_enabled = false
81
+ config.ssr_url = 'http://localhost:13714'
82
+
83
+ end
84
+ ```
85
+
86
+ ## Testing
87
+
88
+ If you're using Rspec, Inertia Rails comes with some nice test helpers to make things simple.
89
+
90
+ To use these helpers, just add the following require statement to your `spec/rails_helper.rb`
91
+
92
+ ```ruby
93
+ require 'inertia_rails/rspec'
94
+ ```
95
+
96
+ And in any test you want to use the inertia helpers, add the inertia flag to the describe block
97
+
98
+ ```ruby
99
+ RSpec.describe EventController, type: :request do
100
+ describe '#index', inertia: true do
101
+ # ...
102
+ end
103
+ end
104
+ ```
105
+
106
+ ### Assertions
107
+
108
+ ```ruby
109
+ RSpec.describe EventController, type: :request do
110
+ describe '#index', inertia: true do
111
+
112
+ # check the component
113
+ expect_inertia.to render_component 'Event/Index'
114
+
115
+ # access the component name
116
+ expect(inertia.component).to eq 'TestComponent'
117
+
118
+ # props (including shared props)
119
+ expect_inertia.to have_exact_props({name: 'Brandon', sport: 'hockey'})
120
+ expect_inertia.to include_props({sport: 'hockey'})
121
+
122
+ # access props
123
+ expect(inertia.props[:name]).to eq 'Brandon'
124
+
125
+ # view data
126
+ expect_inertia.to have_exact_view_data({name: 'Brian', sport: 'basketball'})
127
+ expect_inertia.to include_view_data({sport: 'basketball'})
128
+
129
+ # access view data
130
+ expect(inertia.view_data[:name]).to eq 'Brian'
131
+
132
+ end
133
+ end
134
+
135
+ ```
7
136
 
8
137
  *Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)*
9
138
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "rails", "~> 5.0.7", ">= 5.0.7.2"
5
+ gem "rails", "~> 7.0"
6
6
 
7
7
  gemspec path: "../"
@@ -0,0 +1,11 @@
1
+ <script>
2
+ export let name;
3
+ </script>
4
+
5
+ <style>
6
+ h1 { font-family: sans-serif; text-align: center; }
7
+ </style>
8
+
9
+ <h1>
10
+ Hello {name}!
11
+ </h1>
@@ -0,0 +1,19 @@
1
+ import axios from 'axios'
2
+
3
+ import { createInertiaApp } from '@inertiajs/inertia-svelte'
4
+ import { InertiaProgress } from '@inertiajs/progress'
5
+
6
+ document.addEventListener('DOMContentLoaded', () => {
7
+ const csrfToken = document.querySelector('meta[name=csrf-token]').content
8
+ axios.defaults.headers.common['X-CSRF-Token'] = csrfToken
9
+
10
+ InertiaProgress.init()
11
+
12
+ createInertiaApp({
13
+ id: 'app',
14
+ resolve: name => import(`../Pages/${name}.svelte`),
15
+ setup({ el, App, props }) {
16
+ new App({ target: el, props })
17
+ },
18
+ })
19
+ })
@@ -6,6 +6,7 @@ module InertiaRails
6
6
  FRONT_END_INSTALLERS = [
7
7
  'react',
8
8
  'vue',
9
+ 'svelte',
9
10
  ]
10
11
 
11
12
  def install
@@ -70,5 +71,14 @@ module InertiaRails
70
71
  template "vue/inertia.js", Rails.root.join("app/javascript/packs/inertia.js").to_s
71
72
  say "done!", :green
72
73
  end
74
+
75
+ def install_svelte!
76
+ say "Creating a Svelte page component...", :blue
77
+ run 'yarn add @inertiajs/inertia-svelte'
78
+ template "svelte/InertiaExample.svelte", Rails.root.join("app/javascript/Pages/InertiaExample.svelte").to_s
79
+ say "Copying inertia.js into webpacker's packs folder...", :blue
80
+ template "svelte/inertia.js", Rails.root.join("app/javascript/packs/inertia.js").to_s
81
+ say "done!", :green
82
+ end
73
83
  end
74
84
  end
@@ -1,4 +1,5 @@
1
1
  require_relative "inertia_rails"
2
+ require_relative "helper"
2
3
 
3
4
  module InertiaRails
4
5
  module Controller
@@ -9,6 +10,7 @@ module InertiaRails
9
10
  # :inertia_errors are deleted from the session by the middleware
10
11
  InertiaRails.share(errors: session[:inertia_errors]) if session[:inertia_errors].present?
11
12
  end
13
+ helper ::InertiaRails::Helper
12
14
  end
13
15
 
14
16
  module ClassMethods
@@ -8,7 +8,7 @@ module InertiaRails
8
8
  end
9
9
 
10
10
  initializer "inertia_rails.action_controller" do
11
- ActiveSupport.on_load(:action_controller) do
11
+ ActiveSupport.on_load(:action_controller_base) do
12
12
  include ::InertiaRails::Controller
13
13
  end
14
14
  end
@@ -0,0 +1,7 @@
1
+ require_relative 'inertia_rails'
2
+
3
+ module InertiaRails::Helper
4
+ def inertia_headers
5
+ ::InertiaRails.html_headers.join.html_safe
6
+ end
7
+ end
@@ -5,6 +5,7 @@ require 'inertia_rails/lazy'
5
5
  module InertiaRails
6
6
  thread_mattr_accessor :threadsafe_shared_plain_data
7
7
  thread_mattr_accessor :threadsafe_shared_blocks
8
+ thread_mattr_accessor :threadsafe_html_headers
8
9
 
9
10
  def self.configure
10
11
  yield(Configuration)
@@ -23,6 +24,18 @@ module InertiaRails
23
24
  Configuration.layout
24
25
  end
25
26
 
27
+ def self.ssr_enabled?
28
+ Configuration.ssr_enabled
29
+ end
30
+
31
+ def self.ssr_url
32
+ Configuration.ssr_url
33
+ end
34
+
35
+ def self.html_headers
36
+ self.threadsafe_html_headers || []
37
+ end
38
+
26
39
  # "Setters"
27
40
  def self.share(**args)
28
41
  self.shared_plain_data = self.shared_plain_data.merge(args)
@@ -32,9 +45,14 @@ module InertiaRails
32
45
  self.shared_blocks = self.shared_blocks + [block]
33
46
  end
34
47
 
48
+ def self.html_headers=(headers)
49
+ self.threadsafe_html_headers = headers
50
+ end
51
+
35
52
  def self.reset!
36
53
  self.shared_plain_data = {}
37
54
  self.shared_blocks = []
55
+ self.html_headers = []
38
56
  end
39
57
 
40
58
  def self.lazy(value = nil, &block)
@@ -46,6 +64,8 @@ module InertiaRails
46
64
  module Configuration
47
65
  mattr_accessor(:layout) { 'application' }
48
66
  mattr_accessor(:version) { nil }
67
+ mattr_accessor(:ssr_enabled) { false }
68
+ mattr_accessor(:ssr_url) { 'http://localhost:13714' }
49
69
 
50
70
  def self.evaluated_version
51
71
  self.version.respond_to?(:call) ? self.version.call : self.version
@@ -1,3 +1,5 @@
1
+ require 'net/http'
2
+ require 'json'
1
3
  require_relative "inertia_rails"
2
4
 
3
5
  module InertiaRails
@@ -20,12 +22,21 @@ module InertiaRails
20
22
  @response.set_header('X-Inertia', 'true')
21
23
  @render_method.call json: page, status: @response.status, content_type: Mime[:json]
22
24
  else
25
+ return render_ssr if ::InertiaRails.ssr_enabled? rescue nil
23
26
  @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
24
27
  end
25
28
  end
26
29
 
27
30
  private
28
31
 
32
+ def render_ssr
33
+ uri = URI("#{::InertiaRails.ssr_url}/render")
34
+ res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
35
+
36
+ ::InertiaRails.html_headers = res['head']
37
+ @render_method.call html: res['body'].html_safe, layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
38
+ end
39
+
29
40
  def props
30
41
  _props = ::InertiaRails.shared_data(@controller).merge(@props).select do |key, prop|
31
42
  if rendering_partial_component?
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.11.1"
2
+ VERSION = "2.0.0"
3
3
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inertia_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.11.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Knoles
8
8
  - Brandon Shar
9
9
  - Eugene Granovsky
10
- autorequire:
10
+ autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2021-06-27 00:00:00.000000000 Z
13
+ date: 2022-06-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -124,7 +124,7 @@ dependencies:
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
- description:
127
+ description:
128
128
  email:
129
129
  - brain@bellawatt.com
130
130
  - brandon@bellawatt.com
@@ -147,21 +147,24 @@ files:
147
147
  - app/views/inertia.html.erb
148
148
  - bin/console
149
149
  - bin/setup
150
- - gemfiles/rails_5.0.gemfile
151
150
  - gemfiles/rails_5.1.gemfile
152
151
  - gemfiles/rails_5.2.gemfile
153
152
  - gemfiles/rails_6.0.gemfile
154
153
  - gemfiles/rails_6.1.gemfile
154
+ - gemfiles/rails_7.0.gemfile
155
155
  - inertia_rails.gemspec
156
156
  - lib/generators/inertia_rails/install/controller.rb
157
157
  - lib/generators/inertia_rails/install/react/InertiaExample.jsx
158
158
  - lib/generators/inertia_rails/install/react/inertia.jsx
159
+ - lib/generators/inertia_rails/install/svelte/InertiaExample.svelte
160
+ - lib/generators/inertia_rails/install/svelte/inertia.js
159
161
  - lib/generators/inertia_rails/install/vue/InertiaExample.vue
160
162
  - lib/generators/inertia_rails/install/vue/inertia.js
161
163
  - lib/generators/inertia_rails/install_generator.rb
162
164
  - lib/inertia_rails.rb
163
165
  - lib/inertia_rails/controller.rb
164
166
  - lib/inertia_rails/engine.rb
167
+ - lib/inertia_rails/helper.rb
165
168
  - lib/inertia_rails/inertia_rails.rb
166
169
  - lib/inertia_rails/lazy.rb
167
170
  - lib/inertia_rails/middleware.rb
@@ -182,7 +185,7 @@ metadata:
182
185
  homepage_uri: https://github.com/inertiajs/inertia-rails
183
186
  source_code_uri: https://github.com/inertiajs/inertia-rails
184
187
  changelog_uri: https://github.com/inertiajs/inertia-rails/CHANGELOG.md
185
- post_install_message:
188
+ post_install_message:
186
189
  rdoc_options: []
187
190
  require_paths:
188
191
  - lib
@@ -197,8 +200,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
200
  - !ruby/object:Gem::Version
198
201
  version: '0'
199
202
  requirements: []
200
- rubygems_version: 3.1.4
201
- signing_key:
203
+ rubygems_version: 3.3.3
204
+ signing_key:
202
205
  specification_version: 4
203
206
  summary: Inertia adapter for Rails
204
207
  test_files: []