inertia_rails 2.0.0 → 3.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: ac66be3f6a11b3caa5d6fec8057f75e4be257e154b3ce77f3f2c7b8ddb38d4bc
4
- data.tar.gz: cd9891492a338447f41f10016656c369d8870dd71eacd55ce8d4c6be92f08464
3
+ metadata.gz: 5626c22ecacdfaa9d46b04fe63027a941af3ee6f9e32716943072a547f12bb8f
4
+ data.tar.gz: 3ad0c5ef9d0f2943a70d76218b1d97dce7cef8079ee0ef0bcc7e337ffeef607c
5
5
  SHA512:
6
- metadata.gz: '09beccfde8d305ae45d9afa84d9ace59337e7ae6521984f3047c10723c2ff70dcf01fe66667e51b4ab3bdd52c6d09a915d9edaace24d5aab83db571aa60fca10'
7
- data.tar.gz: 7f25e813e9bec9bd513f2608bd5892728a504b76b2cab5fb43d2067af78411a12fe02baab6f0dded2591fb095536d6dda5ee7f65d6c4691c1affdce767458c80
6
+ metadata.gz: 1e67ddf363fef24d94688cc0f9f51f46c62aec3265acab8a3e656923cc227b05e812c56d95e2fb5996c2acca909d99ed2af1863f17420ea6ec387695af14cf8c
7
+ data.tar.gz: b412652b57ae50161487667e34f1d35cf396bcfde11f2ac3fe8f574d0670374722c5ff1b31e3a83b5c11dc40687afd9945f45734f0e9549f512480bbf22fb801
data/CHANGELOG.md CHANGED
@@ -4,6 +4,15 @@ 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
+ ## [3.0.0] - 2022-09-22
8
+
9
+ * Allow rails layout to set inertia layout. Thanks @ElMassimo!
10
+ * Add the ability to set inertia props and components via rails conventions (see readme)
11
+
12
+ ## [2.0.1] - 2022-07-12
13
+
14
+ * Fix for a middleware issue where global state could be polluted if an exception occurs in a request. Thanks @ElMassimo!
15
+
7
16
  ## [2.0.0] - 2022-06-20
8
17
 
9
18
  * Fix an issue with Rails 7.0. Thanks @0xDing and @aviemet!
data/README.md CHANGED
@@ -14,7 +14,7 @@ gem 'inertia_rails'
14
14
 
15
15
  ### Frontend
16
16
 
17
- Rails 7 specific frontend docs coming soon. For now, check out the official Inertia docs at https://inertiajs.com/
17
+ Rails 7 specific frontend docs coming soon. For now, check out the official Inertia docs at https://inertiajs.com/ or see an example using React/Vite [here](https://github.com/BrandonShar/inertia-rails-template)
18
18
 
19
19
  ## Usage
20
20
 
@@ -30,6 +30,53 @@ def index
30
30
  end
31
31
  ```
32
32
 
33
+ #### Rails Component and Instance Props
34
+
35
+ Starting in version 3.0, Inertia Rails allows you to provide your component name and props via common rails conventions.
36
+
37
+ ```ruby
38
+ class EventsController < ApplicationController
39
+ use_inertia_instance_props
40
+
41
+ def index
42
+ @events = Event.all
43
+ end
44
+
45
+ end
46
+ ```
47
+
48
+ is the same as
49
+
50
+
51
+ ```ruby
52
+ class EventsController < ApplicationController
53
+ def index
54
+ render inertia: 'events/index', props: {
55
+ events: Event.all
56
+ }
57
+ end
58
+ end
59
+ ```
60
+
61
+ #### Instance Props and Default Render Notes
62
+
63
+ In order to use instance props, you must call `use_inertia_instance_props` on the controller (or a base controller it inherits from). If any props are provided manually, instance props
64
+ are automatically disabled for that response. Instance props are only included if they are defined after the before filter is set from `use_inertia_instance_props`.
65
+
66
+ Automatic component name is also opt in, you must set the `default_render` config value to `true`. Otherwise, you can simply `render inertia: true` for the same behavior explicitly.
67
+
68
+ ### Layout
69
+
70
+ Inertia layouts use the rails layout convention and can be set or changed in the same way. The original `layout` config option is still functional, but will likely be deprecated in the future in favor
71
+ of using rails layouts.
72
+
73
+ ```ruby
74
+ class EventsController < ApplicationController
75
+ layout 'inertia_application'
76
+ end
77
+ ```
78
+
79
+
33
80
  ### Shared Data
34
81
 
35
82
  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.
@@ -54,6 +101,14 @@ class EventsController < ApplicationController
54
101
  end
55
102
  ```
56
103
 
104
+ ### Lazy Props
105
+
106
+ On the front end, Inertia supports the concept of "partial reloads" where only the props requested are returned by the server. Sometimes, you may want to use this flow to avoid processing a particularly slow prop on the intial load. In this case, you can use Lazy props. Lazy props aren't evaluated unless they're specifically requested by name in a partial reload.
107
+
108
+ ```ruby
109
+ inertia_share some_data: InertiaRails.lazy(lambda { some_very_slow_method })
110
+ ```
111
+
57
112
  ### Routing
58
113
 
59
114
  If you don't need a controller to handle a static component, you can route directly to a component with the inertia route helper
@@ -62,6 +117,10 @@ If you don't need a controller to handle a static component, you can route direc
62
117
  inertia 'about' => 'AboutComponent'
63
118
  ```
64
119
 
120
+ ### SSR
121
+
122
+ Enable SSR via the config settings for `ssr_enabled` and `ssr_url`
123
+
65
124
  ## Configuration
66
125
 
67
126
  Inertia Rails has a few different configuration options that can be set anywhere, but the most common location is from within an initializer.
@@ -72,10 +131,9 @@ InertiaRails.configure do |config|
72
131
 
73
132
  # set the current version for automatic asset refreshing. A string value should be used if any.
74
133
  config.version = nil
134
+ # enable default inertia rendering (warning! this will override rails default rendering behavior)
135
+ config.default_render = true
75
136
 
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
137
  # ssr specific options
80
138
  config.ssr_enabled = false
81
139
  config.ssr_url = 'http://localhost:13714'
@@ -34,4 +34,5 @@ Gem::Specification.new do |spec|
34
34
  spec.add_development_dependency "sqlite3"
35
35
  spec.add_development_dependency "appraisal"
36
36
  spec.add_development_dependency "responders"
37
+ spec.add_development_dependency "debug"
37
38
  end
@@ -20,6 +20,21 @@ module InertiaRails
20
20
  InertiaRails.share_block(block) if block
21
21
  end
22
22
  end
23
+
24
+ def use_inertia_instance_props
25
+ before_action do
26
+ @_inertia_instance_props = true
27
+ @_inertia_skip_props = view_assigns.keys + ['_inertia_skip_props']
28
+ end
29
+ end
30
+ end
31
+
32
+ def default_render
33
+ if InertiaRails.default_render?
34
+ render(inertia: true)
35
+ else
36
+ super
37
+ end
23
38
  end
24
39
 
25
40
  def redirect_to(options = {}, response_options = {})
@@ -36,8 +51,21 @@ module InertiaRails
36
51
  )
37
52
  end
38
53
 
54
+ def inertia_view_assigns
55
+ return {} unless @_inertia_instance_props
56
+ view_assigns.except(*@_inertia_skip_props)
57
+ end
58
+
39
59
  private
40
60
 
61
+ def inertia_layout
62
+ layout = ::InertiaRails.layout
63
+
64
+ # When the global configuration is not set, let Rails decide which layout
65
+ # should be used based on the controller configuration.
66
+ layout.nil? ? true : layout
67
+ end
68
+
41
69
  def inertia_location(url)
42
70
  headers['X-Inertia-Location'] = url
43
71
  head :conflict
@@ -32,6 +32,10 @@ module InertiaRails
32
32
  Configuration.ssr_url
33
33
  end
34
34
 
35
+ def self.default_render?
36
+ Configuration.default_render
37
+ end
38
+
35
39
  def self.html_headers
36
40
  self.threadsafe_html_headers || []
37
41
  end
@@ -62,10 +66,11 @@ module InertiaRails
62
66
  private
63
67
 
64
68
  module Configuration
65
- mattr_accessor(:layout) { 'application' }
69
+ mattr_accessor(:layout) { nil }
66
70
  mattr_accessor(:version) { nil }
67
71
  mattr_accessor(:ssr_enabled) { false }
68
72
  mattr_accessor(:ssr_url) { 'http://localhost:13714' }
73
+ mattr_accessor(:default_render) { false }
69
74
 
70
75
  def self.evaluated_version
71
76
  self.version.respond_to?(:call) ? self.version.call : self.version
@@ -7,6 +7,8 @@ module InertiaRails
7
7
  def call(env)
8
8
  InertiaRailsRequest.new(@app, env)
9
9
  .response
10
+ ensure
11
+ ::InertiaRails.reset!
10
12
  end
11
13
 
12
14
  class InertiaRailsRequest
@@ -19,8 +21,6 @@ module InertiaRails
19
21
  status, headers, body = @app.call(@env)
20
22
  request = ActionDispatch::Request.new(@env)
21
23
 
22
- ::InertiaRails.reset!
23
-
24
24
  # Inertia errors are added to the session via redirect_to
25
25
  request.session.delete(:inertia_errors) unless keep_inertia_errors?(status)
26
26
 
@@ -7,12 +7,12 @@ module InertiaRails
7
7
  attr_reader :component, :view_data
8
8
 
9
9
  def initialize(component, controller, request, response, render_method, props:, view_data:)
10
- @component = component
10
+ @component = component.is_a?(TrueClass) ? "#{controller.controller_path}/#{controller.action_name}" : component
11
11
  @controller = controller
12
12
  @request = request
13
13
  @response = response
14
14
  @render_method = render_method
15
- @props = props || {}
15
+ @props = props || controller.inertia_view_assigns
16
16
  @view_data = view_data || {}
17
17
  end
18
18
 
@@ -23,7 +23,7 @@ module InertiaRails
23
23
  @render_method.call json: page, status: @response.status, content_type: Mime[:json]
24
24
  else
25
25
  return render_ssr if ::InertiaRails.ssr_enabled? rescue nil
26
- @render_method.call template: 'inertia', layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
26
+ @render_method.call template: 'inertia', layout: layout, locals: (view_data).merge({page: page})
27
27
  end
28
28
  end
29
29
 
@@ -34,7 +34,11 @@ module InertiaRails
34
34
  res = JSON.parse(Net::HTTP.post(uri, page.to_json, 'Content-Type' => 'application/json').body)
35
35
 
36
36
  ::InertiaRails.html_headers = res['head']
37
- @render_method.call html: res['body'].html_safe, layout: ::InertiaRails.layout, locals: (view_data).merge({page: page})
37
+ @render_method.call html: res['body'].html_safe, layout: layout, locals: (view_data).merge({page: page})
38
+ end
39
+
40
+ def layout
41
+ @controller.send(:inertia_layout)
38
42
  end
39
43
 
40
44
  def props
@@ -1,3 +1,3 @@
1
1
  module InertiaRails
2
- VERSION = "2.0.0"
2
+ VERSION = "3.0.0"
3
3
  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: 2.0.0
4
+ version: 3.0.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: 2022-06-20 00:00:00.000000000 Z
13
+ date: 2022-09-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -124,6 +124,20 @@ dependencies:
124
124
  - - ">="
125
125
  - !ruby/object:Gem::Version
126
126
  version: '0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: debug
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
127
141
  description:
128
142
  email:
129
143
  - brain@bellawatt.com