render_turbo_stream 0.1.35 → 0.1.37
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 +49 -38
- data/lib/render_turbo_stream/request_test_helpers.rb +21 -25
- data/lib/render_turbo_stream/version.rb +1 -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: 5aec959a7ad9663de11285bba5734246b90b764235464a41da68e80a35dc2609
|
4
|
+
data.tar.gz: d38bc6507be14b63af99e268cf167c567836cd7021d6777e6c31dac02b73c80e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0ab474fc5ea832bf2ea135af3696ea0fd3d032cb17ebe8847262d7fbd53ab86e038fa37e28b497b3b7d9892e2c680aa153ba06d9e059b21cff82d682bb18099
|
7
|
+
data.tar.gz: b9954342146266f214581d689fdbd0b1084fd8bb0d4d64e8e08bdbd07c931cb39e950953f6dc56c946941a922a3e39b3eee8401559548a0164ea2185d3f692d6
|
data/README.md
CHANGED
@@ -2,11 +2,15 @@
|
|
2
2
|
|
3
3
|
Defining templates like `(create|update).turbo_stream.haml` annoyed me.
|
4
4
|
|
5
|
-
Working consistently with turbo_stream means shooting lots of partials from the backend to the frontend, which always
|
5
|
+
Working consistently with turbo_stream means shooting lots of partials from the backend to the frontend, which always
|
6
|
+
needs the same attributes: ID, partial, and maybe some locals. This gem serializes that: Partials can be controlled
|
7
|
+
directly from the controller.
|
6
8
|
|
7
|
-
It sets the status, generates a flash message, handles redirection, pushes it all to the front and comes with predefined
|
9
|
+
It sets the status, generates a flash message, handles redirection, pushes it all to the front and comes with predefined
|
10
|
+
helpers for enabling request-specs.
|
8
11
|
|
9
|
-
An overview of how we design a rails-7 application with turbo
|
12
|
+
An overview of how we design a rails-7 application with turbo
|
13
|
+
is [published on dev.to](https://dev.to/chmich/rails-7-vite-wrapping-up-1pia).
|
10
14
|
|
11
15
|
## Installation
|
12
16
|
|
@@ -34,35 +38,39 @@ end
|
|
34
38
|
|
35
39
|
Redirection
|
36
40
|
|
37
|
-
For redirection to work, you must follow the installation steps
|
41
|
+
For redirection to work, you must follow the installation steps
|
42
|
+
from [turbo_power](https://github.com/marcoroth/turbo_power).
|
38
43
|
|
39
44
|
Required Configurations for Flash Partial
|
40
45
|
|
41
46
|
```ruby
|
42
47
|
config.x.render_turbo_stream.flash_partial = 'layouts/flash'
|
43
|
-
|
44
|
-
|
48
|
+
config.x.render_turbo_stream.flash_id = 'flash-box'
|
49
|
+
config.x.render_turbo_stream.flash_action = 'prepend'
|
45
50
|
```
|
46
51
|
|
47
52
|
The corresponding partials for flashes could look [like this](https://gitlab.com/sedl/renderturbostream/-/wikis/flashes)
|
48
53
|
|
49
54
|
## Usage
|
50
55
|
|
51
|
-
`turbo_stream_save` is a special method for streamlining `update` or `create` functions for `turbo_stream`, which might
|
56
|
+
`turbo_stream_save` is a special method for streamlining `update` or `create` functions for `turbo_stream`, which might
|
57
|
+
look like this:
|
52
58
|
|
53
59
|
```ruby
|
54
|
-
def create
|
55
|
-
@customer = Customer.new(customer_params)
|
56
60
|
|
57
|
-
|
58
|
-
|
59
|
-
redirect_on_success_to: edit_customer_path(@customer)
|
60
|
-
)
|
61
|
+
def create
|
62
|
+
@customer = Customer.new(customer_params)
|
61
63
|
|
62
|
-
|
64
|
+
turbo_stream_save(
|
65
|
+
@customer.save,
|
66
|
+
redirect_on_success_to: edit_customer_path(@customer)
|
67
|
+
)
|
68
|
+
|
69
|
+
end
|
63
70
|
```
|
64
71
|
|
65
|
-
This will set a status, generate a flash message and perform `stream_partials`, which could result in something like
|
72
|
+
This will set a status, generate a flash message and perform `stream_partials`, which could result in something like
|
73
|
+
this:
|
66
74
|
|
67
75
|
```ruby
|
68
76
|
stream_partials(
|
@@ -93,38 +101,37 @@ stream_partial(
|
|
93
101
|
|
94
102
|
## Testing
|
95
103
|
|
96
|
-
For system testing we have Capybara. This works great and is necessary for javascript and backend combined apps, but it
|
104
|
+
For system testing we have Capybara. This works great and is necessary for javascript and backend combined apps, but it
|
105
|
+
is good practice to break tests into smaller pieces.
|
97
106
|
|
98
107
|
For the much faster request-level tests, there are some helpers:
|
99
108
|
|
100
|
-
If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special html
|
109
|
+
If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special html
|
110
|
+
that contains the medadata that is interesting for our tests and is parsed by included test helpers. So tests could look
|
111
|
+
like this:
|
101
112
|
|
102
113
|
```ruby
|
103
114
|
require 'rails_helper'
|
104
115
|
include RenderTurboStream::TestHelpers
|
105
116
|
|
106
117
|
RSpec.describe "Articles", type: :request do
|
107
|
-
|
118
|
+
|
108
119
|
let(:invalid_params) { { article: { title: '', desciption: 'abc' } } }
|
109
120
|
|
110
121
|
it 'create failed' do
|
111
|
-
|
122
|
+
|
112
123
|
post articles_path(params: invalid_params)
|
113
124
|
|
114
|
-
#
|
115
|
-
|
116
|
-
#
|
117
|
-
expect(
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
# --- MORE OPTIONS ---
|
125
|
-
|
126
|
-
# to which html-ids turbo-stream would point
|
127
|
-
expect(partials_ids).to include('flash-box', 'form')
|
125
|
+
# ----- BASIC -----
|
126
|
+
|
127
|
+
# Exactly which partials were rendered to which ids
|
128
|
+
expect(
|
129
|
+
partials_once(
|
130
|
+
{ partial: 'layouts/flash', id: 'flash-box' },
|
131
|
+
{ partial: 'articles/form', id: 'form' })
|
132
|
+
).to be_truthy
|
133
|
+
|
134
|
+
# ----- MORE OPTIONS -----
|
128
135
|
|
129
136
|
# partial_response (singular) returns false unless partial responded exactly one time, otherwise the content in nokogiri format: https://nokogiri.org/tutorials/searching_a_xml_html_document.html#basic-searching
|
130
137
|
r = partial_response(id: 'flash-box')
|
@@ -136,15 +143,17 @@ RSpec.describe "Articles", type: :request do
|
|
136
143
|
|
137
144
|
# COUNTS
|
138
145
|
|
139
|
-
# Check the number of ids responded to
|
140
|
-
expect(partials_ids_count).to eq(2)
|
141
|
-
|
142
146
|
# Check how many times a specific ID has been responded to.
|
143
147
|
expect(partials_responses(id: 'flash-box').length).to eq(1)
|
144
148
|
|
145
149
|
# Same with partials
|
146
|
-
|
150
|
+
|
147
151
|
expect(partials_responses(partial: 'layouts/flash').length).to eq(1)
|
152
|
+
|
153
|
+
# little helper for showing a array of prepared flashes and all what the renderer has done
|
154
|
+
expect(partials_log.join('')).to include('Article could not be created')
|
155
|
+
expect(partials_log.join('')).to include('layouts/flash')
|
156
|
+
|
148
157
|
end
|
149
158
|
end
|
150
159
|
```
|
@@ -161,7 +170,8 @@ end
|
|
161
170
|
|
162
171
|
P.S.:
|
163
172
|
|
164
|
-
Testing the plugin itself: There is a [quick-and-dirty app](https://gitlab.com/sedl/renderturbostream_railsapp) which
|
173
|
+
Testing the plugin itself: There is a [quick-and-dirty app](https://gitlab.com/sedl/renderturbostream_railsapp) which
|
174
|
+
includes the plugin and has tests done by rspec/request and capybara.
|
165
175
|
|
166
176
|
## Parameters for turbo_stream_save
|
167
177
|
|
@@ -212,4 +222,5 @@ example value: `"%<model_name>s successfully created"`
|
|
212
222
|
Contribution welcome.
|
213
223
|
|
214
224
|
## License
|
225
|
+
|
215
226
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -29,30 +29,6 @@ module RenderTurboStream
|
|
29
29
|
res.map { |r| Nokogiri::HTML(r['html_response']) }
|
30
30
|
end
|
31
31
|
|
32
|
-
def partials_ids_count
|
33
|
-
partials_ids.length
|
34
|
-
end
|
35
|
-
|
36
|
-
def partials_count
|
37
|
-
partials.length
|
38
|
-
end
|
39
|
-
|
40
|
-
# array of strings of ids of rendered partials that turbo_stream pointed to
|
41
|
-
def partials_ids
|
42
|
-
ary = partials_attributes
|
43
|
-
ids = []
|
44
|
-
ary.each { |a| ids.push(a['id']) unless ids.include?(a['id']) }
|
45
|
-
ids
|
46
|
-
end
|
47
|
-
|
48
|
-
# array of strings of paths by which partials are rendered, for example: ['articles/form'].
|
49
|
-
def partials
|
50
|
-
ary = partials_attributes
|
51
|
-
paths = []
|
52
|
-
ary.each { |a| paths.push(a['partial']) unless paths.include?(a['partial']) }
|
53
|
-
paths
|
54
|
-
end
|
55
|
-
|
56
32
|
# without arguments: returns array of strings of paths by which partials that are exactly one time rendered, for example: ['articles/form'].
|
57
33
|
# if arguments provided: compares with result and returns boolean
|
58
34
|
# examples:
|
@@ -83,7 +59,7 @@ module RenderTurboStream
|
|
83
59
|
if !paths_once.include?(_a[:partial])
|
84
60
|
res = false
|
85
61
|
elsif _a.key?(:id)
|
86
|
-
r = ary.select{|a|a['partial'] == _a[:partial]}.first
|
62
|
+
r = ary.select { |a| a['partial'] == _a[:partial] }.first
|
87
63
|
res = r['id'] == _a[:id]
|
88
64
|
end
|
89
65
|
end
|
@@ -109,5 +85,25 @@ module RenderTurboStream
|
|
109
85
|
r = JSON.parse(response.body)
|
110
86
|
r['redirected_to']
|
111
87
|
end
|
88
|
+
|
89
|
+
# log as helper for the developer to see which flash is set and which partials are rendered to wich ids
|
90
|
+
def partials_log
|
91
|
+
r = []
|
92
|
+
if flash[:alert].present?
|
93
|
+
r.push("flash[:alert]: «#{flash[:alert]}»")
|
94
|
+
end
|
95
|
+
if flash[:notice].present?
|
96
|
+
r.push("flash[:notice]: «#{flash[:notice]}»")
|
97
|
+
end
|
98
|
+
if response.status == 302
|
99
|
+
r.push("redirect to #{turbo_redirect_to}")
|
100
|
+
else
|
101
|
+
partials_attributes.map do |a|
|
102
|
+
str = [a['action'], 'id', "«#{a['id']}»", 'by partial', "«#{a['partial']}»"].join(' ')
|
103
|
+
r.push(str)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
r
|
107
|
+
end
|
112
108
|
end
|
113
109
|
end
|