render_turbo_stream 0.1.19 → 0.1.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +28 -13
- data/app/views/render_turbo_stream_partials.html.erb +10 -2
- data/lib/render_turbo_stream/test_helpers.rb +22 -10
- data/lib/render_turbo_stream/version.rb +1 -1
- data/lib/render_turbo_stream.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c9a8ce6edc86ce1bf4adb28042784f90665fb915d32c663899fa66fa6f6a764
|
4
|
+
data.tar.gz: cb31e25e686751127e6eb21562ab31bb0a15c25a7760691be2ace27fd32872a2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 073a9bd4038390487978547dfd72e566744bba02032b7fc43989a5914b5dcde55612009774828101f88467ba37b86bb73693d0819441f6ddf7f3bc5fbd0b5736
|
7
|
+
data.tar.gz: 41836ea59255674882a518e861526d9d9460bb786943e5651304f48670e7af2cf7feedc5a6a7c2ae924b45f785f3b2e957afe4cb5bd454ab06869c5781dfa6af
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ ApplicationController
|
|
22
22
|
include RenderTurboStream
|
23
23
|
```
|
24
24
|
|
25
|
-
|
25
|
+
Required Configurations for Flash Partial
|
26
26
|
|
27
27
|
```ruby
|
28
28
|
config.x.render_turbo_stream.flash_partial = 'layouts/flash'
|
@@ -34,7 +34,7 @@ The corresponding partials for flashes could look [like this](https://gitlab.com
|
|
34
34
|
|
35
35
|
## Usage
|
36
36
|
|
37
|
-
`turbo_stream_save` is a special method for
|
37
|
+
`turbo_stream_save` is a special method for streamlining `update` or `create` functions for `turbo_stream`, which might look like this:
|
38
38
|
|
39
39
|
```ruby
|
40
40
|
def create
|
@@ -48,7 +48,7 @@ The corresponding partials for flashes could look [like this](https://gitlab.com
|
|
48
48
|
end
|
49
49
|
```
|
50
50
|
|
51
|
-
This
|
51
|
+
This will set a status, generate a flash message and perform `stream_partials', which could result in something like this:
|
52
52
|
|
53
53
|
```ruby
|
54
54
|
stream_partials(
|
@@ -66,7 +66,7 @@ stream_partials(
|
|
66
66
|
)
|
67
67
|
```
|
68
68
|
|
69
|
-
The `stream_partial
|
69
|
+
The `stream_partial' method is nothing more than a singular of `stream_partials'.
|
70
70
|
|
71
71
|
```ruby
|
72
72
|
stream_partial(
|
@@ -79,11 +79,11 @@ stream_partial(
|
|
79
79
|
|
80
80
|
## Testing
|
81
81
|
|
82
|
-
For system testing there is
|
82
|
+
For system testing, there is Capybara.
|
83
83
|
|
84
|
-
For
|
84
|
+
For request-level tests, there are some helpers:
|
85
85
|
|
86
|
-
If request format is not `turbo_stream`, which is the case on request specs, the method responds in a
|
86
|
+
If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special format html that can be easily checked by included test helpers. So tests could look like this:
|
87
87
|
|
88
88
|
```ruby
|
89
89
|
require 'rails_helper'
|
@@ -91,18 +91,29 @@ include RenderTurboStream::TestHelpers
|
|
91
91
|
|
92
92
|
RSpec.describe "Articles", type: :request do
|
93
93
|
let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
|
94
|
-
it '
|
95
|
-
|
96
|
-
patch article_path(a, params: invalid_params)
|
94
|
+
it 'create failed' do
|
95
|
+
post articles_path(params: invalid_params)
|
97
96
|
expect(response.status).to eq(422)
|
98
97
|
expect(rendered_partials_count).to eq(2)
|
99
|
-
|
100
|
-
|
98
|
+
|
99
|
+
# .._params means: check the params used to generate the partials.
|
100
|
+
expect(rendered_partials_paths).to include('articles/form', 'layouts/flash')
|
101
|
+
expect(rendered_ids).to include('form', 'flash-box')
|
102
|
+
expect(rendered_partials_params_find_id('flash-box')['partial']).to eq('layouts/flash')
|
103
|
+
expect(rendered_partials_params_where_id('flash-box').first['partial']).to eq('layouts/flash')
|
104
|
+
expect(rendered_partials_params_where_id('flash-box').is_a?(Array)).to eq(true)
|
105
|
+
|
106
|
+
# check html (returned as string) resulting from the rendered partials
|
107
|
+
h = rendered_partials_html
|
108
|
+
expect(h.join('')).to include('Article could not be created')
|
109
|
+
h = rendered_partials_html_by_id
|
110
|
+
expect(h['flash-box'].first).to include('Article could not be created')
|
111
|
+
expect(h['flash-box'].length).to eq(1)
|
101
112
|
end
|
102
113
|
end
|
103
114
|
```
|
104
115
|
|
105
|
-
|
116
|
+
The test for a redirection might look something like this:
|
106
117
|
|
107
118
|
```ruby
|
108
119
|
it 'update success' do
|
@@ -112,6 +123,10 @@ it 'update success' do
|
|
112
123
|
end
|
113
124
|
```
|
114
125
|
|
126
|
+
P.S.:
|
127
|
+
|
128
|
+
Testing the plugin itself: There is a [quick-and-dirty app](https://gitlab.com/sedl/renderturbostream_railsapp) which includes the plugin and has tests done by rspec/request and capybara.
|
129
|
+
|
115
130
|
## Parameters
|
116
131
|
|
117
132
|
`save_action` (first parameter, boolean) true if successful
|
@@ -1,11 +1,19 @@
|
|
1
1
|
<% partials_count = 0 %>
|
2
2
|
<% rendered_partials = [] %>
|
3
|
+
<% htmls = [] %>
|
4
|
+
<% htmls_by_id = {} %>
|
3
5
|
<% streams.each do |s| %>
|
4
|
-
<% render s[:partial], locals: s[:locals], formats: [:html ] %>
|
6
|
+
<% html = (render s[:partial], locals: s[:locals], formats: [:html ]) %>
|
7
|
+
<% htmls.push(html) %>
|
8
|
+
<% htmls_by_id[s[:id]] ||= [] %>
|
9
|
+
<% htmls_by_id[s[:id]].push(html) %>
|
5
10
|
<% partials_count += 1 %>
|
6
11
|
<% rendered_partials.push(s[:partial]) %>
|
7
12
|
<% end %>
|
8
13
|
|
9
14
|
|
10
15
|
<%= content_tag :div, check.to_json, id: 'check-json' %>
|
11
|
-
<%= content_tag :div,
|
16
|
+
<%= content_tag :div, htmls.to_json, id: 'htmls' %>
|
17
|
+
<%= content_tag :div, htmls_by_id.to_json, id: 'htmls_by_id' %>
|
18
|
+
<%= content_tag :div, rendered_partials.to_json, id: 'rendered-partials' %>
|
19
|
+
<%= content_tag :div, streams.to_json, id: 'streams' %>
|
@@ -4,28 +4,40 @@ module RenderTurboStream
|
|
4
4
|
e = Nokogiri::HTML(response.body).search('#check-json').first
|
5
5
|
JSON.parse(e.inner_html).keys
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
|
+
# returns array of strings of rendered partials inner-html
|
9
|
+
def rendered_partials_html
|
10
|
+
e = Nokogiri::HTML(response.body).search('#htmls').first
|
11
|
+
JSON.parse(e.inner_html)
|
12
|
+
end
|
13
|
+
|
14
|
+
# returns hash of arrays with responded html(as string) contents by ID
|
15
|
+
def rendered_partials_html_by_id
|
16
|
+
e = Nokogiri::HTML(response.body).search('#htmls_by_id').first
|
17
|
+
JSON.parse(e.inner_html)
|
18
|
+
end
|
19
|
+
|
20
|
+
# array of strings of paths by them rendered partials are called, example: ['articles/form']
|
21
|
+
def rendered_partials_paths
|
8
22
|
e = Nokogiri::HTML(response.body).search('#rendered-partials').first
|
9
23
|
JSON.parse(e.inner_html)
|
10
24
|
end
|
11
25
|
|
12
26
|
# returns all partials that would be streamed to a given ID
|
13
27
|
# 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
|
15
|
-
partials = Nokogiri::HTML(response.body).search('#
|
16
|
-
JSON.parse(
|
17
|
-
|
28
|
+
def rendered_partials_params_where_id(id)
|
29
|
+
partials = Nokogiri::HTML(response.body).search('#streams').first
|
30
|
+
h = JSON.parse(partials.inner_html)
|
31
|
+
h.select{|e| e['id'] == id}
|
18
32
|
end
|
19
33
|
|
20
34
|
# returns the first partial that would be streamed to a given ID
|
21
|
-
def
|
22
|
-
|
23
|
-
JSON.parse(e.inner_html)
|
24
|
-
partials.select{|p| p[id] == id }.first
|
35
|
+
def rendered_partials_params_find_id(id)
|
36
|
+
rendered_partials_params_where_id(id).first
|
25
37
|
end
|
26
38
|
|
27
39
|
def rendered_partials_count
|
28
|
-
|
40
|
+
rendered_partials_paths.length
|
29
41
|
end
|
30
42
|
def turbo_redirected_to
|
31
43
|
expect(response.status).to eq(302)
|
data/lib/render_turbo_stream.rb
CHANGED
@@ -138,7 +138,8 @@ module RenderTurboStream
|
|
138
138
|
def stream_partials(array)
|
139
139
|
|
140
140
|
ary = []
|
141
|
-
array.dup.each do |
|
141
|
+
array.dup.each do |pr|
|
142
|
+
props = pr.symbolize_keys
|
142
143
|
part = (props[:partial].present? ? props[:partial] : props[:id]).gsub('-', '_')
|
143
144
|
partial = (part.to_s.include?('/') ? part : [controller_path, part].join('/'))
|
144
145
|
r = props
|