inertia_rails 1.2.0 → 1.4.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: fdaebfad8721ec7747b79081270577ca65181b95c3351675387ac133645fe392
4
- data.tar.gz: ed0d9bf04ab9af128c4cad90c3e2af57d15ae5a92699f28e5893c9340743e3f3
3
+ metadata.gz: ef2ef33ca24036a92d67ec557bab99829468a7f9546422b68a703624bd52489d
4
+ data.tar.gz: 6beb16df6b7511813b62e42e91fbb7d0a251264b2e802e0e0345b04bd08700ac
5
5
  SHA512:
6
- metadata.gz: 4037238f6ba98d8dc82e8c5d852615a471e9d2396ab292be74740a7b40013c904043e92b4c724fac3d0aa9a47ee4f97f340c567ec2119eacf3fdb748b9097261
7
- data.tar.gz: 6bef52dadbe2c8bfef33f8b4027e81219e58ed1263615b6c3fb97e3bb759166a115110237b80f4916b02133399b4bb8e60442ec5a5b7aca6b992ca24c6cd6b2d
6
+ metadata.gz: 5e2d7527c48047f950207b163fa539b6b4735655d501a573035a782cb623f5973a74789574be6722bbf7bd4088835c5a486d9a8962df02d17473c4c2d42a0012
7
+ data.tar.gz: 96d8302e0fd851874d14ef2cee34c013278924c9e619d7302a2a66ae2a3c5dbd7c3a5e3a7ee2e01f8849adfbd0f54e65b1661cfeeb27320e8f94b415d8ca5567
@@ -0,0 +1,32 @@
1
+ name: Testing
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ ruby: [2.6, 2.7]
11
+
12
+ runs-on: ubuntu-latest
13
+ name: Test against Ruby ${{ matrix.ruby }}
14
+
15
+ steps:
16
+ - uses: actions/checkout@v2
17
+
18
+ - name: Setup System
19
+ run: sudo apt-get install libsqlite3-dev
20
+
21
+ - name: Set up Ruby
22
+ uses: ruby/setup-ruby@v1
23
+ with:
24
+ ruby-version: ${{ matrix.ruby }}
25
+
26
+ - name: Install gems
27
+ run: |
28
+ gem install bundler
29
+ bundle install --jobs 4 --retry 3
30
+
31
+ - name: Run tests
32
+ run: bundle exec rake
data/.gitignore CHANGED
@@ -6,6 +6,7 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
9
10
 
10
11
  /spec/dummy/db/*.sqlite3
11
12
  /spec/dummy/db/*.sqlite3-journal
@@ -4,6 +4,37 @@ 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.4.0] - 2020-07-09
8
+
9
+ * Fixed Ruby 2.7 deprecation warnings
10
+ * Added `inertia_partial?` method
11
+ * Fixed homepage in the gemspec
12
+ * Make the InertiaRails module data threadsafe
13
+
14
+ ## [1.3.1] - 2020-02-20
15
+
16
+ * Fix a typo in the README (inertia only has 1 t!)
17
+
18
+ ## [1.3.0] - 2020-01-28
19
+
20
+ ### Added
21
+
22
+ * Added request.inertia? method
23
+
24
+ ## [1.2.2] - 2020-01-21
25
+
26
+ ### Fixed
27
+
28
+ * Added patches to allow Rails errors to show properly in the inertia modal
29
+ * Fixed a middleware issue caused by a breaking change in Rack v2.1.*
30
+
31
+ ## [1.2.1] - 2019-12-6
32
+
33
+ ### Fixed
34
+
35
+ * Change page url to use path instead of url
36
+ * Moved Inertia Share logic to a before_action to ensure it runs on every request
37
+
7
38
  ## [1.2.0] - 2019-11-1
8
39
 
9
40
  ### Added
data/README.md CHANGED
@@ -6,7 +6,7 @@ Visit [inertiajs.com](https://inertiajs.com/) to learn more.
6
6
 
7
7
  The initial version of the gem was named `inertia`; however, that name was not available on Rubygems.
8
8
 
9
- The 1.0.0 version release on Rubygems is `intertia_rails`.
9
+ The 1.0.0 version release on Rubygems is `inertia_rails`.
10
10
 
11
11
  The changes required are:
12
12
 
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ["brain@bellawatt.com", "brandon@bellawatt.com", "eugene@bellawatt.com"]
10
10
 
11
11
  spec.summary = %q{Inertia adapter for Rails}
12
- spec.homepage = "https://github.com/inertiajs/inertia-rails/"
12
+ spec.homepage = "https://github.com/inertiajs/inertia-rails"
13
13
  spec.license = "MIT"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
@@ -26,8 +26,8 @@ Gem::Specification.new do |spec|
26
26
  spec.require_paths = ["lib"]
27
27
 
28
28
  spec.add_development_dependency "bundler", "~> 2.0"
29
- spec.add_development_dependency "rake", "~> 10.0"
30
- spec.add_development_dependency "rspec-rails", "~> 3.0"
29
+ spec.add_development_dependency "rake", "~> 13.0"
30
+ spec.add_development_dependency "rspec-rails", "~> 4.0"
31
31
  spec.add_development_dependency "rails"
32
32
  spec.add_development_dependency "rails-controller-testing"
33
33
  spec.add_development_dependency "sqlite3"
@@ -1,6 +1,10 @@
1
1
  require 'inertia_rails/renderer'
2
2
  require 'inertia_rails/engine'
3
3
 
4
+ require 'patches/debug_exceptions'
5
+ require 'patches/better_errors'
6
+ require 'patches/request'
7
+
4
8
  ActionController::Renderers.add :inertia do |component, options|
5
9
  InertiaRails::Renderer.new(
6
10
  component,
@@ -6,8 +6,10 @@ module InertiaRails
6
6
 
7
7
  module ClassMethods
8
8
  def inertia_share(**args, &block)
9
- InertiaRails.share(args) if args
10
- InertiaRails.share_block(block) if block
9
+ before_action do
10
+ InertiaRails.share(**args) if args
11
+ InertiaRails.share_block(block) if block
12
+ end
11
13
  end
12
14
  end
13
15
  end
@@ -1,6 +1,6 @@
1
1
  module InertiaRails
2
- mattr_accessor(:shared_plain_data) { Hash.new }
3
- mattr_accessor(:shared_blocks) { [] }
2
+ thread_mattr_accessor :threadsafe_shared_plain_data
3
+ thread_mattr_accessor :threadsafe_shared_blocks
4
4
 
5
5
  def self.configure
6
6
  yield(Configuration)
@@ -21,11 +21,11 @@ module InertiaRails
21
21
 
22
22
  # "Setters"
23
23
  def self.share(**args)
24
- shared_plain_data.merge!(args)
24
+ self.shared_plain_data = self.shared_plain_data.merge(args)
25
25
  end
26
26
 
27
27
  def self.share_block(block)
28
- shared_blocks.push(block)
28
+ self.shared_blocks = self.shared_blocks + [block]
29
29
  end
30
30
 
31
31
  def self.reset!
@@ -36,12 +36,45 @@ module InertiaRails
36
36
  private
37
37
 
38
38
  module Configuration
39
- mattr_accessor(:layout) { 'application' }
40
- mattr_accessor(:version) { nil }
39
+ thread_mattr_accessor :threadsafe_layout
40
+ thread_mattr_accessor :threadsafe_version
41
41
 
42
42
  def self.evaluated_version
43
43
  self.version.respond_to?(:call) ? self.version.call : self.version
44
44
  end
45
+
46
+ def self.layout
47
+ self.threadsafe_layout || 'application'
48
+ end
49
+
50
+ def self.layout=(val)
51
+ self.threadsafe_layout = val
52
+ end
53
+
54
+ def self.version
55
+ self.threadsafe_version
56
+ end
57
+
58
+ def self.version=(val)
59
+ self.threadsafe_version = val
60
+ end
61
+ end
62
+
63
+ # Getters and setters to provide default values for the threadsafe attributes
64
+ def self.shared_plain_data
65
+ self.threadsafe_shared_plain_data || {}
66
+ end
67
+
68
+ def self.shared_plain_data=(val)
69
+ self.threadsafe_shared_plain_data = val
70
+ end
71
+
72
+ def self.shared_blocks
73
+ self.threadsafe_shared_blocks || []
74
+ end
75
+
76
+ def self.shared_blocks=(val)
77
+ self.threadsafe_shared_blocks = val
45
78
  end
46
79
 
47
80
  def self.evaluated_blocks(controller, blocks)
@@ -40,7 +40,7 @@ module InertiaRails
40
40
 
41
41
  def force_refresh(request)
42
42
  request.flash.keep
43
- Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url})
43
+ Rack::Response.new('', 409, {'X-Inertia-Location' => request.original_url}).finish
44
44
  end
45
45
  end
46
46
  end
@@ -42,7 +42,7 @@ module InertiaRails
42
42
  {
43
43
  component: component,
44
44
  props: props,
45
- url: @request.original_url,
45
+ url: @request.original_fullpath,
46
46
  version: ::InertiaRails.version,
47
47
  }
48
48
  end
@@ -61,7 +61,7 @@ RSpec.configure do |config|
61
61
  config.before(:each, inertia: true) do
62
62
  new_renderer = InertiaRails::Renderer.method(:new)
63
63
  allow(InertiaRails::Renderer).to receive(:new) do |component, controller, request, response, render, named_args|
64
- new_renderer.call(component, controller, request, response, inertia_wrap_render(render), named_args)
64
+ new_renderer.call(component, controller, request, response, inertia_wrap_render(render), **named_args)
65
65
  end
66
66
  end
67
67
  end
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "1.2.0"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -0,0 +1,17 @@
1
+ # Patch BetterErrors::Middleware to render HTML for Inertia requests
2
+ #
3
+ # Original source:
4
+ # https://github.com/BetterErrors/better_errors/blob/v2.5.1/lib/better_errors/middleware.rb
5
+ #
6
+
7
+ if defined?(BetterErrors)
8
+ BetterErrors::Middleware.class_eval do
9
+ prepend(InertiaBetterErrors = Module.new do
10
+ def text?(env)
11
+ return false if env["HTTP_X_INERTIA"]
12
+
13
+ super
14
+ end
15
+ end)
16
+ end
17
+ end
@@ -0,0 +1,23 @@
1
+ # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
+ #
3
+ # Rails has introduced text rendering for XHR requests with Rails 4.1 and
4
+ # changed the implementation in 4.2, 5.0 and 5.1 (unchanged since then).
5
+ #
6
+ # The original source needs to be patched, so that Inertia requests are
7
+ # NOT responded with plain text, but with HTML.
8
+
9
+ if defined?(ActionDispatch::DebugExceptions)
10
+ if ActionPack.version.to_s >= '5.1'
11
+ require 'patches/debug_exceptions/patch-5-1'
12
+ elsif ActionPack.version.to_s >= '5.0'
13
+ require 'patches/debug_exceptions/patch-5-0'
14
+ elsif ActionPack.version.to_s >= '4.2'
15
+ require 'patches/debug_exceptions/patch-4-2'
16
+ elsif ActionPack.version.to_s >= '4.1'
17
+ require 'patches/debug_exceptions/patch-4-1'
18
+ else
19
+ # No patch required, because text rendering for
20
+ # XHR requests was introduced with Rails 4.1:
21
+ # https://github.com/rails/rails/pull/11960
22
+ end
23
+ end
@@ -0,0 +1,41 @@
1
+ # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
+ #
3
+ # Original source:
4
+ # https://github.com/rails/rails/blob/4-1-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5
+ #
6
+
7
+ ActionDispatch::DebugExceptions.class_eval do
8
+ prepend(InertiaDebugExceptions = Module.new do
9
+ def render_exception(env, exception)
10
+ wrapper = ExceptionWrapper.new(env, exception)
11
+ log_error(env, wrapper)
12
+
13
+ if env['action_dispatch.show_detailed_exceptions']
14
+ request = Request.new(env)
15
+ template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
16
+ request: request,
17
+ exception: wrapper.exception,
18
+ application_trace: wrapper.application_trace,
19
+ framework_trace: wrapper.framework_trace,
20
+ full_trace: wrapper.full_trace,
21
+ routes_inspector: routes_inspector(exception),
22
+ source_extract: wrapper.source_extract,
23
+ line_number: wrapper.line_number,
24
+ file: wrapper.file
25
+ )
26
+ file = "rescues/#{wrapper.rescue_template}"
27
+
28
+ if request.xhr? && !request.headers['X-Inertia'] # <<<< this line is changed only
29
+ body = template.render(template: file, layout: false, formats: [:text])
30
+ format = "text/plain"
31
+ else
32
+ body = template.render(template: file, layout: 'rescues/layout')
33
+ format = "text/html"
34
+ end
35
+ render(wrapper.status_code, body, format)
36
+ else
37
+ raise exception
38
+ end
39
+ end
40
+ end)
41
+ end
@@ -0,0 +1,52 @@
1
+ # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
+ #
3
+ # Original source:
4
+ # https://github.com/rails/rails/blob/4-2-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5
+ #
6
+
7
+ ActionDispatch::DebugExceptions.class_eval do
8
+ prepend(InertiaDebugExceptions = Module.new do
9
+ def render_exception(env, exception)
10
+ wrapper = ExceptionWrapper.new(env, exception)
11
+ log_error(env, wrapper)
12
+
13
+ if env['action_dispatch.show_detailed_exceptions']
14
+ request = Request.new(env)
15
+ traces = wrapper.traces
16
+
17
+ trace_to_show = 'Application Trace'
18
+ if traces[trace_to_show].empty? && wrapper.rescue_template != 'routing_error'
19
+ trace_to_show = 'Full Trace'
20
+ end
21
+
22
+ if source_to_show = traces[trace_to_show].first
23
+ source_to_show_id = source_to_show[:id]
24
+ end
25
+
26
+ template = ActionView::Base.new([RESCUES_TEMPLATE_PATH],
27
+ request: request,
28
+ exception: wrapper.exception,
29
+ traces: traces,
30
+ show_source_idx: source_to_show_id,
31
+ trace_to_show: trace_to_show,
32
+ routes_inspector: routes_inspector(exception),
33
+ source_extracts: wrapper.source_extracts,
34
+ line_number: wrapper.line_number,
35
+ file: wrapper.file
36
+ )
37
+ file = "rescues/#{wrapper.rescue_template}"
38
+
39
+ if request.xhr? && !request.headers['X-Inertia'] # <<<< this line is changed only
40
+ body = template.render(template: file, layout: false, formats: [:text])
41
+ format = "text/plain"
42
+ else
43
+ body = template.render(template: file, layout: 'rescues/layout')
44
+ format = "text/html"
45
+ end
46
+ render(wrapper.status_code, body, format)
47
+ else
48
+ raise exception
49
+ end
50
+ end
51
+ end)
52
+ end
@@ -0,0 +1,23 @@
1
+ # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
+ #
3
+ # Original source:
4
+ # https://github.com/rails/rails/blob/5-0-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5
+ #
6
+
7
+ ActionDispatch::DebugExceptions.class_eval do
8
+ prepend(InertiaDebugExceptions = Module.new do
9
+ def render_for_default_application(request, wrapper)
10
+ template = create_template(request, wrapper)
11
+ file = "rescues/#{wrapper.rescue_template}"
12
+
13
+ if request.xhr? && !request.headers['X-Inertia'] # <<<< this line is changed only
14
+ body = template.render(template: file, layout: false, formats: [:text])
15
+ format = "text/plain"
16
+ else
17
+ body = template.render(template: file, layout: 'rescues/layout')
18
+ format = "text/html"
19
+ end
20
+ render(wrapper.status_code, body, format)
21
+ end
22
+ end)
23
+ end
@@ -0,0 +1,26 @@
1
+ # Patch ActionDispatch::DebugExceptions to render HTML for Inertia requests
2
+ #
3
+ # Original source (unchanged since Rails 5.1):
4
+ # https://github.com/rails/rails/blob/5-1-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
5
+ # https://github.com/rails/rails/blob/5-2-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
6
+ # https://github.com/rails/rails/blob/6-0-stable/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb
7
+ #
8
+
9
+ ActionDispatch::DebugExceptions.class_eval do
10
+ prepend(InertiaDebugExceptions = Module.new do
11
+ def render_for_browser_request(request, wrapper)
12
+ template = create_template(request, wrapper)
13
+ file = "rescues/#{wrapper.rescue_template}"
14
+
15
+ if request.xhr? && !request.headers['X-Inertia'] # <<<< this line is changed only
16
+ body = template.render(template: file, layout: false, formats: [:text])
17
+ format = "text/plain"
18
+ else
19
+ body = template.render(template: file, layout: "rescues/layout")
20
+ format = "text/html"
21
+ end
22
+
23
+ render(wrapper.status_code, body, format)
24
+ end
25
+ end)
26
+ end
@@ -0,0 +1,9 @@
1
+ ActionDispatch::Request.class_eval do
2
+ def inertia?
3
+ key? 'HTTP_X_INERTIA'
4
+ end
5
+
6
+ def inertia_partial?
7
+ key? 'HTTP_X_INERTIA_PARTIAL_DATA'
8
+ end
9
+ 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.2.0
4
+ version: 1.4.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: 2019-11-01 00:00:00.000000000 Z
13
+ date: 2020-07-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -32,28 +32,28 @@ dependencies:
32
32
  requirements:
33
33
  - - "~>"
34
34
  - !ruby/object:Gem::Version
35
- version: '10.0'
35
+ version: '13.0'
36
36
  type: :development
37
37
  prerelease: false
38
38
  version_requirements: !ruby/object:Gem::Requirement
39
39
  requirements:
40
40
  - - "~>"
41
41
  - !ruby/object:Gem::Version
42
- version: '10.0'
42
+ version: '13.0'
43
43
  - !ruby/object:Gem::Dependency
44
44
  name: rspec-rails
45
45
  requirement: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '3.0'
49
+ version: '4.0'
50
50
  type: :development
51
51
  prerelease: false
52
52
  version_requirements: !ruby/object:Gem::Requirement
53
53
  requirements:
54
54
  - - "~>"
55
55
  - !ruby/object:Gem::Version
56
- version: '3.0'
56
+ version: '4.0'
57
57
  - !ruby/object:Gem::Dependency
58
58
  name: rails
59
59
  requirement: !ruby/object:Gem::Requirement
@@ -105,9 +105,9 @@ executables: []
105
105
  extensions: []
106
106
  extra_rdoc_files: []
107
107
  files:
108
+ - ".github/workflows/push.yml"
108
109
  - ".gitignore"
109
110
  - ".rspec"
110
- - ".travis.yml"
111
111
  - CHANGELOG.md
112
112
  - CODE_OF_CONDUCT.md
113
113
  - Gemfile
@@ -126,13 +126,20 @@ files:
126
126
  - lib/inertia_rails/renderer.rb
127
127
  - lib/inertia_rails/rspec.rb
128
128
  - lib/inertia_rails/version.rb
129
- homepage: https://github.com/inertiajs/inertia-rails/
129
+ - lib/patches/better_errors.rb
130
+ - lib/patches/debug_exceptions.rb
131
+ - lib/patches/debug_exceptions/patch-4-1.rb
132
+ - lib/patches/debug_exceptions/patch-4-2.rb
133
+ - lib/patches/debug_exceptions/patch-5-0.rb
134
+ - lib/patches/debug_exceptions/patch-5-1.rb
135
+ - lib/patches/request.rb
136
+ homepage: https://github.com/inertiajs/inertia-rails
130
137
  licenses:
131
138
  - MIT
132
139
  metadata:
133
- homepage_uri: https://github.com/inertiajs/inertia-rails/
134
- source_code_uri: https://github.com/inertiajs/inertia-rails/
135
- changelog_uri: https://github.com/inertiajs/inertia-rails//CHANGELOG.md
140
+ homepage_uri: https://github.com/inertiajs/inertia-rails
141
+ source_code_uri: https://github.com/inertiajs/inertia-rails
142
+ changelog_uri: https://github.com/inertiajs/inertia-rails/CHANGELOG.md
136
143
  post_install_message:
137
144
  rdoc_options: []
138
145
  require_paths:
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.3
7
- before_install: gem install bundler -v 2.0.2