inertiax_rails 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/push.yml +33 -0
- data/.gitignore +22 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +173 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +273 -0
- data/Rakefile +6 -0
- data/app/controllers/inertia_rails/static_controller.rb +7 -0
- data/app/views/inertia.html.erb +1 -0
- data/app/views/inertia_ssr.html.erb +1 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/inertiax_rails.gemspec +37 -0
- data/lib/generators/inertia_rails/install/controller.rb +7 -0
- data/lib/generators/inertia_rails/install/react/InertiaExample.jsx +9 -0
- data/lib/generators/inertia_rails/install/react/inertia.jsx +17 -0
- data/lib/generators/inertia_rails/install/svelte/InertiaExample.svelte +11 -0
- data/lib/generators/inertia_rails/install/svelte/inertia.js +14 -0
- data/lib/generators/inertia_rails/install/vue/InertiaExample.vue +11 -0
- data/lib/generators/inertia_rails/install/vue/inertia.js +20 -0
- data/lib/generators/inertia_rails/install_generator.rb +84 -0
- data/lib/inertia_rails/controller.rb +110 -0
- data/lib/inertia_rails/engine.rb +16 -0
- data/lib/inertia_rails/inertia_rails.rb +52 -0
- data/lib/inertia_rails/lazy.rb +28 -0
- data/lib/inertia_rails/middleware.rb +97 -0
- data/lib/inertia_rails/renderer.rb +92 -0
- data/lib/inertia_rails/rspec.rb +125 -0
- data/lib/inertia_rails/version.rb +3 -0
- data/lib/inertia_rails.rb +24 -0
- data/lib/patches/better_errors.rb +17 -0
- data/lib/patches/debug_exceptions/patch-5-0.rb +23 -0
- data/lib/patches/debug_exceptions/patch-5-1.rb +26 -0
- data/lib/patches/debug_exceptions.rb +17 -0
- data/lib/patches/mapper.rb +8 -0
- data/lib/patches/request.rb +9 -0
- data/lib/tasks/inertia_rails.rake +16 -0
- metadata +203 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 15ef3b0b927cacf0828a1e26cfaf7df2cf2532a2919138875887890e92e38599
|
4
|
+
data.tar.gz: 5f0900f8a7fef40ea415011b92d3e7ff40874575c6c9b1432e9d80395a63c7f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a37da9a54c63bd336a027345f9fb19d8ecedda372d620abaadd654657126421be712cf5494acb291bffb7935c92e5f8dc7672aee109ba0f51620e1f37658732a
|
7
|
+
data.tar.gz: 2a21636d441f491263f1bd8a742ab802bafa259cdb6eef48ed453c1a33d29b8740d42dcea2560cad769d2ca75d931202a1e4f4f9a3015bbf69cd9390c6936d02
|
@@ -0,0 +1,33 @@
|
|
1
|
+
name: Testing
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
strategy:
|
8
|
+
fail-fast: false
|
9
|
+
matrix:
|
10
|
+
ruby: ['3.1', '3.2', '3.3']
|
11
|
+
rails: ['6.1', '7.0', '7.1']
|
12
|
+
|
13
|
+
runs-on: ubuntu-latest
|
14
|
+
name: Test against Ruby ${{ matrix.ruby }} / Rails ${{ matrix.rails }}
|
15
|
+
|
16
|
+
steps:
|
17
|
+
- uses: actions/checkout@v4
|
18
|
+
|
19
|
+
- name: Setup System
|
20
|
+
run: sudo apt-get install libsqlite3-dev
|
21
|
+
|
22
|
+
- name: Set up Ruby
|
23
|
+
uses: ruby/setup-ruby@v1
|
24
|
+
with:
|
25
|
+
ruby-version: ${{ matrix.ruby }}
|
26
|
+
bundler-cache: true
|
27
|
+
env:
|
28
|
+
RAILS_VERSION: ${{ matrix.rails }}
|
29
|
+
|
30
|
+
- name: Run tests
|
31
|
+
run: bundle exec rake
|
32
|
+
env:
|
33
|
+
RAILS_VERSION: ${{ matrix.rails }}
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
/Gemfile.lock
|
10
|
+
|
11
|
+
/spec/dummy/db/*.sqlite3
|
12
|
+
/spec/dummy/db/*.sqlite3-journal
|
13
|
+
/spec/dummy/db/log/*.log
|
14
|
+
/spec/dummy/tmp/
|
15
|
+
/spec/dummy/.sass-cache
|
16
|
+
/spec/dummy/log/
|
17
|
+
|
18
|
+
# rspec failure tracking
|
19
|
+
.rspec_status
|
20
|
+
|
21
|
+
# Appraisal
|
22
|
+
gemfiles/*.gemfile.lock
|
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [3.2.0] - 2024-06-19
|
8
|
+
|
9
|
+
* Refactor the internals of shared Inertia data to use controller instance variables instead of module level variables that run a higher risk of being leaked between requests. Big thanks to @ledermann for the initial work many years ago and to @PedroAugustoRamalhoDuarte for finishing it up!
|
10
|
+
* Change the Inertia response to set the `Vary` header to `X-Inertia` instead of `Accept`, Thanks @osbre!
|
11
|
+
* Always set the `XSRF-TOKEN` in an `after_action` request instead of only on non-Inertia requests. This fixes a bug where logging out (and resetting the session) via Inertia would create a CSRF token mismatch on a subsequent Inertia request (until you manually hard refreshed the page). Thanks @jordanhiltunen!
|
12
|
+
|
13
|
+
## [3.1.4] - 2024-04-28
|
14
|
+
|
15
|
+
* Reset Inertia shared data after each RSpec example where `inertia: true` is used. Thanks @coreyaus!
|
16
|
+
* Update Github Actions workflows to use currently supported Ruby/Rails versions. Thanks @PedroAugustoRamalhoDuarte!
|
17
|
+
|
18
|
+
## [3.1.3] - 2023-11-03
|
19
|
+
|
20
|
+
* Depend on railties instead of rails so that applications which only use pieces of Rails can avoid a full Rails installation. Thanks @BenMorganMY!
|
21
|
+
|
22
|
+
## [3.1.2] - 2023-09-26
|
23
|
+
|
24
|
+
* Fix `have_exact_props` RSpec matcher in the situation where shared props are defined in a lambda that outputs a hash with symbolized keys
|
25
|
+
|
26
|
+
## [3.1.1] - 2023-08-21
|
27
|
+
|
28
|
+
* Fix broken partial reloads caused by comparing a list of symbolized keys with string keys from HashWithIndifferentAccess
|
29
|
+
|
30
|
+
## [3.1.0] - 2023-08-21
|
31
|
+
|
32
|
+
### Features
|
33
|
+
|
34
|
+
* CSRF protection works without additional configuration now.
|
35
|
+
* Optional deep merging of shared props.
|
36
|
+
|
37
|
+
### Fixes
|
38
|
+
|
39
|
+
* Document Inertia headers. @buhrmi
|
40
|
+
* Documentation typo fix. @lujanfernaud
|
41
|
+
* Changelog URI fix. @PedroAugustoRamalhoDuarte
|
42
|
+
|
43
|
+
## [3.0.0] - 2022-09-22
|
44
|
+
|
45
|
+
* Allow rails layout to set inertia layout. Thanks @ElMassimo!
|
46
|
+
* Add the ability to set inertia props and components via rails conventions (see readme)
|
47
|
+
|
48
|
+
## [2.0.1] - 2022-07-12
|
49
|
+
|
50
|
+
* Fix for a middleware issue where global state could be polluted if an exception occurs in a request. Thanks @ElMassimo!
|
51
|
+
|
52
|
+
## [2.0.0] - 2022-06-20
|
53
|
+
|
54
|
+
* Fix an issue with Rails 7.0. Thanks @0xDing and @aviemet!
|
55
|
+
* Drop support for Rails 5.0 (and mentally, though not literally drop support for Rails < 6)
|
56
|
+
|
57
|
+
## [1.12.1] - 2022-05-09
|
58
|
+
|
59
|
+
* Allow inertia to take over after initial pageload when using ssr. Thanks @99monkey!
|
60
|
+
|
61
|
+
## [1.12.0] - 2022-05-04
|
62
|
+
|
63
|
+
* SSR!
|
64
|
+
|
65
|
+
## [1.11.1] - 2021-06-27
|
66
|
+
|
67
|
+
* Fixed thread safety in the middleware. Thanks @caifara!
|
68
|
+
|
69
|
+
## [1.11.0] - 2021-03-23
|
70
|
+
|
71
|
+
* Fixed the install generator. `installable?` was always returning false, preventing it from actually running.
|
72
|
+
* Added an install generator for Vue.
|
73
|
+
|
74
|
+
## [1.10.0] - 2021-03-22
|
75
|
+
|
76
|
+
* Added install generator to quickly add Inertia to existing rails apps via `rails inertia_rails:install:react`
|
77
|
+
|
78
|
+
## [1.9.2] - 2021-02-23
|
79
|
+
|
80
|
+
* Improve method for detecting whether a user used the RSpec helpers without adding `inertia: true` to the spec
|
81
|
+
* Emit a warning when expecting an Inertia response in RSpec and never reaching a `render inertia:` call
|
82
|
+
|
83
|
+
## [1.9.1] - 2021-02-10
|
84
|
+
|
85
|
+
* Define `redirect_to` and `redirect_back` as public methods for compatibility with other code using them
|
86
|
+
|
87
|
+
## [1.9.0] - 2021-01-17
|
88
|
+
|
89
|
+
* Added the same inertia awareness that redirect_to has to redirect_back
|
90
|
+
|
91
|
+
## [1.8.0] - 2020-12-08
|
92
|
+
|
93
|
+
* Add `inertia` route helper feature
|
94
|
+
|
95
|
+
## [1.7.1] - 2020-11-24
|
96
|
+
|
97
|
+
* Fix the definition for InertiaRails::Lazy to avoid an uninitialized constant error when booting an application.
|
98
|
+
|
99
|
+
## [1.7.0] - 2020-11-24
|
100
|
+
|
101
|
+
* 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.
|
102
|
+
|
103
|
+
## [1.6.0] - 2020-11-20
|
104
|
+
|
105
|
+
* 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.
|
106
|
+
* Set content type to json for Inertia responses
|
107
|
+
* Return the original response status with Inertia responses
|
108
|
+
|
109
|
+
## [1.5.0] - 2020-10-07
|
110
|
+
|
111
|
+
* Test against multiple Rails versions in Github Actions
|
112
|
+
* Add the `inertia_location` controller method that forces a full page refresh
|
113
|
+
|
114
|
+
## [1.4.1] - 2020-08-06
|
115
|
+
|
116
|
+
* Fixed a bug involving threadsafe versions and layouts
|
117
|
+
|
118
|
+
## [1.4.0] - 2020-07-09
|
119
|
+
|
120
|
+
* Fixed Ruby 2.7 deprecation warnings
|
121
|
+
* Added `inertia_partial?` method
|
122
|
+
* Fixed homepage in the gemspec
|
123
|
+
* Make the InertiaRails module data threadsafe
|
124
|
+
|
125
|
+
## [1.3.1] - 2020-02-20
|
126
|
+
|
127
|
+
* Fix a typo in the README (inertia only has 1 t!)
|
128
|
+
|
129
|
+
## [1.3.0] - 2020-01-28
|
130
|
+
|
131
|
+
### Added
|
132
|
+
|
133
|
+
* Added request.inertia? method
|
134
|
+
|
135
|
+
## [1.2.2] - 2020-01-21
|
136
|
+
|
137
|
+
### Fixed
|
138
|
+
|
139
|
+
* Added patches to allow Rails errors to show properly in the inertia modal
|
140
|
+
* Fixed a middleware issue caused by a breaking change in Rack v2.1.*
|
141
|
+
|
142
|
+
## [1.2.1] - 2019-12-6
|
143
|
+
|
144
|
+
### Fixed
|
145
|
+
|
146
|
+
* Change page url to use path instead of url
|
147
|
+
* Moved Inertia Share logic to a before_action to ensure it runs on every request
|
148
|
+
|
149
|
+
## [1.2.0] - 2019-11-1
|
150
|
+
|
151
|
+
### Added
|
152
|
+
|
153
|
+
* Added rspec helpers
|
154
|
+
|
155
|
+
### Fixed
|
156
|
+
|
157
|
+
* Make sure that `inertia_share` properties are reset before each request
|
158
|
+
|
159
|
+
## [1.1.0] - 2019-10-24
|
160
|
+
|
161
|
+
### Changed
|
162
|
+
|
163
|
+
* Switches mattr_accessor defaults to block syntax to allow pre Rails 5.2 compatibility
|
164
|
+
|
165
|
+
## [1.0.1] - 2019-10-23
|
166
|
+
|
167
|
+
### Fixed
|
168
|
+
|
169
|
+
* Allow `Intertia.share` within a controller to access controller methods
|
170
|
+
|
171
|
+
## [1.0.0] - 2019-10-09
|
172
|
+
|
173
|
+
* Initial release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 TODO: Bellawatt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,273 @@
|
|
1
|
+
![image](https://user-images.githubusercontent.com/6599653/114456558-032e2200-9bab-11eb-88bc-a19897f417ba.png)
|
2
|
+
|
3
|
+
|
4
|
+
# Inertia X Rails Adapter
|
5
|
+
|
6
|
+
This is a fork of the original [Inertia Rails](https://github.com/inertiajs/inertia-rails) adapter. It is designed to be compatible with buhrmi's [Inertia X](https://github.com/buhrmi/inertia) branch.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
### Backend
|
11
|
+
|
12
|
+
Just add the inertia rails gem to your Gemfile
|
13
|
+
```ruby
|
14
|
+
gem 'inertiax_rails'
|
15
|
+
```
|
16
|
+
|
17
|
+
### Frontend
|
18
|
+
|
19
|
+
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)
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Responses
|
24
|
+
|
25
|
+
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.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
def index
|
29
|
+
render inertia: 'Event/Index', props: {
|
30
|
+
events: Event.all,
|
31
|
+
}
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
#### Rails Component and Instance Props
|
36
|
+
|
37
|
+
Starting in version 3.0, Inertia Rails allows you to provide your component name and props via common rails conventions.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
class EventsController < ApplicationController
|
41
|
+
use_inertia_instance_props
|
42
|
+
|
43
|
+
def index
|
44
|
+
@events = Event.all
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
is the same as
|
51
|
+
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
class EventsController < ApplicationController
|
55
|
+
def index
|
56
|
+
render inertia: 'events/index', props: {
|
57
|
+
events: Event.all
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
#### Instance Props and Default Render Notes
|
64
|
+
|
65
|
+
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
|
66
|
+
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`.
|
67
|
+
|
68
|
+
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.
|
69
|
+
|
70
|
+
### Layout
|
71
|
+
|
72
|
+
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
|
73
|
+
of using rails layouts.
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class EventsController < ApplicationController
|
77
|
+
layout 'inertia_application'
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
|
82
|
+
### Shared Data
|
83
|
+
|
84
|
+
If you have data that you want to be provided as a prop to every component (a common use-case is information about the authenticated user) you can use the `shared_data` controller method.
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class EventsController < ApplicationController
|
88
|
+
# share syncronously
|
89
|
+
inertia_share app_name: env['app.name']
|
90
|
+
|
91
|
+
# share lazily, evaluated at render time
|
92
|
+
inertia_share do
|
93
|
+
if logged_in?
|
94
|
+
{
|
95
|
+
user: logged_in_user,
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# share lazily alternate syntax
|
101
|
+
inertia_share user_count: lambda { User.count }
|
102
|
+
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
106
|
+
#### Deep Merging Shared Data
|
107
|
+
|
108
|
+
By default, Inertia will shallow merge data defined in an action with the shared data. You might want a deep merge. Imagine using shared data to represent defaults you'll override sometimes.
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class ApplicationController
|
112
|
+
inertia_share do
|
113
|
+
{ basketball_data: { points: 50, rebounds: 100 } }
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
Let's say we want a particular action to change only part of that data structure. The renderer accepts a `deep_merge` option:
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
class CrazyScorersController < ApplicationController
|
122
|
+
def index
|
123
|
+
render inertia: 'CrazyScorersComponent',
|
124
|
+
props: { basketball_data: { points: 100 } },
|
125
|
+
deep_merge: true
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
# The renderer will send this to the frontend:
|
130
|
+
{
|
131
|
+
basketball_data: {
|
132
|
+
points: 100,
|
133
|
+
rebounds: 100,
|
134
|
+
}
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
Deep merging can be set as the project wide default via the InertiaRails configuration:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
# config/initializers/some_initializer.rb
|
142
|
+
InertiaRails.configure do |config|
|
143
|
+
config.deep_merge_shared_data = true
|
144
|
+
end
|
145
|
+
|
146
|
+
```
|
147
|
+
|
148
|
+
If deep merging is enabled by default, it's possible to opt out within the action:
|
149
|
+
|
150
|
+
```ruby
|
151
|
+
class CrazyScorersController < ApplicationController
|
152
|
+
inertia_share do
|
153
|
+
{
|
154
|
+
basketball_data: {
|
155
|
+
points: 50,
|
156
|
+
rebounds: 10,
|
157
|
+
}
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
def index
|
162
|
+
render inertia: 'CrazyScorersComponent',
|
163
|
+
props: { basketball_data: { points: 100 } },
|
164
|
+
deep_merge: false
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
# Even if deep merging is set by default, since the renderer has `deep_merge: false`, it will send a shallow merge to the frontend:
|
169
|
+
{
|
170
|
+
basketball_data: {
|
171
|
+
points: 100,
|
172
|
+
}
|
173
|
+
}
|
174
|
+
```
|
175
|
+
|
176
|
+
### Lazy Props
|
177
|
+
|
178
|
+
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.
|
179
|
+
|
180
|
+
```ruby
|
181
|
+
inertia_share some_data: InertiaRails.lazy(lambda { some_very_slow_method })
|
182
|
+
```
|
183
|
+
|
184
|
+
### Routing
|
185
|
+
|
186
|
+
If you don't need a controller to handle a static component, you can route directly to a component with the inertia route helper
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
inertia 'about' => 'AboutComponent'
|
190
|
+
```
|
191
|
+
|
192
|
+
### SSR
|
193
|
+
|
194
|
+
Enable SSR via the config settings for `ssr_enabled` and `ssr_url`.
|
195
|
+
|
196
|
+
When using SSR, don't forget to add `<%= inertia_headers %>` to the `<head>` of your `application.html.erb`.
|
197
|
+
|
198
|
+
## Configuration
|
199
|
+
|
200
|
+
Inertia Rails has a few different configuration options that can be set anywhere, but the most common location is from within an initializer.
|
201
|
+
|
202
|
+
The default config is shown below
|
203
|
+
```ruby
|
204
|
+
InertiaRails.configure do |config|
|
205
|
+
|
206
|
+
# set the current version for automatic asset refreshing. A string value should be used if any.
|
207
|
+
config.version = nil
|
208
|
+
# enable default inertia rendering (warning! this will override rails default rendering behavior)
|
209
|
+
config.default_render = true
|
210
|
+
|
211
|
+
# ssr specific options
|
212
|
+
config.ssr_enabled = false
|
213
|
+
config.ssr_url = 'http://localhost:13714'
|
214
|
+
|
215
|
+
config.deep_merge_shared_data = false
|
216
|
+
|
217
|
+
end
|
218
|
+
```
|
219
|
+
|
220
|
+
## Testing
|
221
|
+
|
222
|
+
If you're using Rspec, Inertia Rails comes with some nice test helpers to make things simple.
|
223
|
+
|
224
|
+
To use these helpers, just add the following require statement to your `spec/rails_helper.rb`
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
require 'inertia_rails/rspec'
|
228
|
+
```
|
229
|
+
|
230
|
+
And in any test you want to use the inertia helpers, add the inertia flag to the describe block
|
231
|
+
|
232
|
+
```ruby
|
233
|
+
RSpec.describe EventController, type: :request do
|
234
|
+
describe '#index', inertia: true do
|
235
|
+
# ...
|
236
|
+
end
|
237
|
+
end
|
238
|
+
```
|
239
|
+
|
240
|
+
### Assertions
|
241
|
+
|
242
|
+
```ruby
|
243
|
+
RSpec.describe EventController, type: :request do
|
244
|
+
describe '#index', inertia: true do
|
245
|
+
|
246
|
+
# check the component
|
247
|
+
expect_inertia.to render_component 'Event/Index'
|
248
|
+
|
249
|
+
# access the component name
|
250
|
+
expect(inertia.component).to eq 'TestComponent'
|
251
|
+
|
252
|
+
# props (including shared props)
|
253
|
+
expect_inertia.to have_exact_props({name: 'Brandon', sport: 'hockey'})
|
254
|
+
expect_inertia.to include_props({sport: 'hockey'})
|
255
|
+
|
256
|
+
# access props
|
257
|
+
expect(inertia.props[:name]).to eq 'Brandon'
|
258
|
+
|
259
|
+
# view data
|
260
|
+
expect_inertia.to have_exact_view_data({name: 'Brian', sport: 'basketball'})
|
261
|
+
expect_inertia.to include_view_data({sport: 'basketball'})
|
262
|
+
|
263
|
+
# access view data
|
264
|
+
expect(inertia.view_data[:name]).to eq 'Brian'
|
265
|
+
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
```
|
270
|
+
|
271
|
+
*Maintained and sponsored by the team at [bellaWatt](https://bellawatt.com/)*
|
272
|
+
|
273
|
+
[![bellaWatt Logo](https://user-images.githubusercontent.com/6599653/114456832-5607d980-9bab-11eb-99c8-ab39867c384e.png)](https://bellawatt.com/)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
<div id="app" data-page="<%= page.to_json %>"></div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<div id="app" data-server-rendered="true" data-page="<%= page.to_json %>"><%= html %></div>
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "inertia_rails/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|