render_turbo_stream 0.1.16 → 0.1.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +20 -18
- data/app/views/render_turbo_stream_partials.html.erb +11 -0
- data/lib/render_turbo_stream/test_helpers.rb +36 -0
- data/lib/render_turbo_stream/version.rb +1 -1
- data/lib/render_turbo_stream.rb +13 -1
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fd4172769edde2afebf684b4a53c6497e255b1acf0e03b5ca8fe2ba93b64437d
|
4
|
+
data.tar.gz: 1c99586db62a402be30535c39bf3d60b279fdb269000b2b362b49e1fa935308d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 510284ee5f2b48df8161f0dc7a4552b5f9785b8c6d49a6b71dcdd98f37804bd22874a63e2f8f2f3b4967454fedfed8d3e2eb98bbfb85645a7e4cf83f4311c4d8
|
7
|
+
data.tar.gz: e0336ad09c42899b3c1b9d6d717cd1ec7b6a5781df932bf52956b643304170418dabd2334f4b99509e0059fa37f7a81f6a6672e22b9705890d400cd02a812eb0
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@ Defining templates like `(create|update).turbo_stream.haml` annoyed me.
|
|
4
4
|
|
5
5
|
Working consistently with turbo_stream means shooting lots of partials from the backend to the frontend, which always needs the same attributes: ID, partial, and maybe some locals. This gem serializes that: Partials can be controlled directly from the controller.
|
6
6
|
|
7
|
-
It sets the status, generates a flash message, handles redirection, pushes it all to the front and
|
7
|
+
It sets the status, generates a flash message, handles redirection, pushes it all to the front and comes with predefined helpers for enabling request-specs.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -79,22 +79,26 @@ stream_partial(
|
|
79
79
|
|
80
80
|
## Testing
|
81
81
|
|
82
|
-
For system testing
|
82
|
+
For system testing there is capybara.
|
83
83
|
|
84
|
-
For
|
84
|
+
For Tests on request level there are some helpers:
|
85
85
|
|
86
|
-
If
|
87
|
-
`response.body` returns a `json` where the key is the `html-ID` that is used by turbo-stream with the properties of the partials. Testing by rspec could look like this:
|
86
|
+
If request format is not `turbo_stream`, which is the case on request specs, the method responds in a html by a special format that can easily be checked by included test-helpers. So, tests could look like this:
|
88
87
|
|
89
88
|
```ruby
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
89
|
+
require 'rails_helper'
|
90
|
+
include RenderTurboStream::TestHelpers
|
91
|
+
|
92
|
+
RSpec.describe "Articles", type: :request do
|
93
|
+
let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
|
94
|
+
it 'update failed' do
|
95
|
+
a = FactoryBot.create(:article)
|
96
|
+
patch article_path(a, params: invalid_params)
|
97
|
+
expect(response.status).to eq(422)
|
98
|
+
expect(rendered_partials_count).to eq(2)
|
99
|
+
expect(rendered_partials).to include('articles/form', 'layouts/flash')
|
100
|
+
expect(streamed_ids).to include('form', 'flash-box')
|
101
|
+
end
|
98
102
|
end
|
99
103
|
```
|
100
104
|
|
@@ -102,11 +106,9 @@ Test for a redirection could look like this:
|
|
102
106
|
|
103
107
|
```ruby
|
104
108
|
it 'update success' do
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
r = JSON.parse(response.body)
|
109
|
-
expect(r['redirected_to']).to eq(articles_path)
|
109
|
+
a = FactoryBot.create(:article)
|
110
|
+
patch article_path(a, params: valid_params)
|
111
|
+
expect(turbo_redirected_to).to eq(articles_path) #=> turbo_redirected_to includes a «expect(response.status).to eq(302)»
|
110
112
|
end
|
111
113
|
```
|
112
114
|
|
@@ -0,0 +1,11 @@
|
|
1
|
+
<% partials_count = 0 %>
|
2
|
+
<% rendered_partials = [] %>
|
3
|
+
<% streams.each do |s| %>
|
4
|
+
<% render s[:partial], locals: s[:locals], formats: [:html ] %>
|
5
|
+
<% partials_count += 1 %>
|
6
|
+
<% rendered_partials.push(s[:partial]) %>
|
7
|
+
<% end %>
|
8
|
+
|
9
|
+
|
10
|
+
<%= content_tag :div, check.to_json, id: 'check-json' %>
|
11
|
+
<%= content_tag :div, rendered_partials.to_json, id: 'rendered-partials' %>
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module RenderTurboStream
|
2
|
+
module TestHelpers
|
3
|
+
def rendered_ids
|
4
|
+
e = Nokogiri::HTML(response.body).search('#check-json').first
|
5
|
+
JSON.parse(e.inner_html).keys
|
6
|
+
end
|
7
|
+
def rendered_partials
|
8
|
+
e = Nokogiri::HTML(response.body).search('#rendered-partials').first
|
9
|
+
JSON.parse(e.inner_html)
|
10
|
+
end
|
11
|
+
|
12
|
+
# returns all partials that would be streamed to a given ID
|
13
|
+
# note: if more than one partial by turbo_stream.replace are streamed to same ID, render_turbo_stream, in test mode already throws a error
|
14
|
+
def rendered_partials_where_id(id)
|
15
|
+
partials = Nokogiri::HTML(response.body).search('#rendered-partials').first
|
16
|
+
JSON.parse(e.inner_html)
|
17
|
+
partials.select{|p| p[id] == id }
|
18
|
+
end
|
19
|
+
|
20
|
+
# returns the first partial that would be streamed to a given ID
|
21
|
+
def rendered_partials_find_id(id)
|
22
|
+
partials = Nokogiri::HTML(response.body).search('#rendered-partials').first
|
23
|
+
JSON.parse(e.inner_html)
|
24
|
+
partials.select{|p| p[id] == id }.first
|
25
|
+
end
|
26
|
+
|
27
|
+
def rendered_partials_count
|
28
|
+
rendered_partials.length
|
29
|
+
end
|
30
|
+
def turbo_redirected_to
|
31
|
+
expect(response.status).to eq(302)
|
32
|
+
r = JSON.parse(response.body)
|
33
|
+
r['redirected_to']
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/render_turbo_stream.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "render_turbo_stream/version"
|
2
2
|
require "render_turbo_stream/railtie"
|
3
3
|
require 'render_turbo_stream/engine'
|
4
|
+
require 'render_turbo_stream/test_helpers'
|
4
5
|
|
5
6
|
module RenderTurboStream
|
6
7
|
|
@@ -149,13 +150,24 @@ module RenderTurboStream
|
|
149
150
|
if request.format.to_sym == :turbo_stream
|
150
151
|
render template: 'render_turbo_stream_partials', locals: { streams: ary }, layout: false
|
151
152
|
else
|
153
|
+
if Rails.env.test?
|
154
|
+
check_ids = {}
|
155
|
+
ary.each do |a|
|
156
|
+
if a[:action] == 'replace'
|
157
|
+
check_ids[a[:id]] ||= 0
|
158
|
+
check_ids[a[:id]] += 1
|
159
|
+
throw "More than one partial rendered to ID «#{a[:id]}» by replace" if check_ids[a[:id]] >= 2
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
152
163
|
check = {}
|
153
164
|
ary.each do |a|
|
154
165
|
b = a.dup
|
155
166
|
b.delete(:id)
|
156
167
|
check[a[:id]] = b
|
157
168
|
end
|
158
|
-
render json: check
|
169
|
+
# render json: check
|
170
|
+
render template: 'render_turbo_stream_partials', locals: { check: check, streams: ary }, layout: false, formats: :html
|
159
171
|
end
|
160
172
|
end
|
161
173
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_turbo_stream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.18
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- christian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-04-
|
11
|
+
date: 2023-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 7.0.4.3
|
27
|
-
description:
|
28
|
-
gem handles translated flash messages, sets status, and renders partials by turbo-stream.
|
29
|
-
|
30
|
-
|
27
|
+
description: |-
|
28
|
+
Writing views like (create|update).turbo_stream.haml annoyed me. This gem handles translated flash messages, sets status, and renders partials by turbo-stream. You can control them directly from the controller. It also handles redirection: turbo_power is included.
|
29
|
+
|
30
|
+
Comes with predefined helpers for enabling request-specs.
|
31
31
|
email:
|
32
32
|
- christian@sedlmair.ch
|
33
33
|
executables: []
|
@@ -37,11 +37,13 @@ files:
|
|
37
37
|
- README.md
|
38
38
|
- Rakefile
|
39
39
|
- app/controllers/render_turbo_stream/application_controller.rb
|
40
|
+
- app/views/render_turbo_stream_partials.html.erb
|
40
41
|
- app/views/render_turbo_stream_partials.turbo_stream.erb
|
41
42
|
- app/views/render_turbo_stream_redirect.turbo_stream.erb
|
42
43
|
- lib/render_turbo_stream.rb
|
43
44
|
- lib/render_turbo_stream/engine.rb
|
44
45
|
- lib/render_turbo_stream/railtie.rb
|
46
|
+
- lib/render_turbo_stream/test_helpers.rb
|
45
47
|
- lib/render_turbo_stream/version.rb
|
46
48
|
- lib/tasks/render_turbo_stream_tasks.rake
|
47
49
|
homepage: https://gitlab.com/sedl/renderturbostream
|