render_turbo_stream 0.1.24 → 0.1.26

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: fefb7da73ce6a177f1def683f0d713544011e11258cfc46c11785847ebda5e2e
4
- data.tar.gz: 18f2719d7ffa9b0e697f8b65d45e40454c16907811ec2ddcd423eace2d3c5b1f
3
+ metadata.gz: 6d128b7674b363238eb61eb3801963559ceced131f779d2461eab167f1900a55
4
+ data.tar.gz: b2ca434807990e05b1adfed8919917bf41a22c7f1578444c5164a23b9c174ad9
5
5
  SHA512:
6
- metadata.gz: a4d2fd93deeeb26a77314a4b4d7ae1c8d7e187f70b4213ce941edc418366b3a347cf950f47afa4872773f33b8cc4769cf8571c14fd20a4b36689b0a83214a98b
7
- data.tar.gz: d8565ef3855a81d5dd379c9ca3a161caa227523856855189779566d079f22fe35570a971aaa0fdc4a368c7de262105ac3eb94b783044832498ddf20b75f24049
6
+ metadata.gz: bacc8838158e95146849e34b7df62d71f5006737e94d036e7389394f10e13cbb7d8b1eecd4ca387d453917e64d902cef1ead32566a12ff75543ac5d07dbbafc1
7
+ data.tar.gz: fea7aec70e2707113e8ffc77d2e12bbd17ea63baa9f62e3dd3fd34487abeabf1d2b96e08806c814b307663c61621a5dfb533dce56385f51d5381839f507bb837
data/README.md CHANGED
@@ -22,6 +22,10 @@ ApplicationController
22
22
  include RenderTurboStream
23
23
  ```
24
24
 
25
+ Redirection
26
+
27
+ For redirection to work, you must follow the installation steps from [turbo_power](https://github.com/marcoroth/turbo_power).
28
+
25
29
  Required Configurations for Flash Partial
26
30
 
27
31
  ```ruby
@@ -79,9 +83,9 @@ stream_partial(
79
83
 
80
84
  ## Testing
81
85
 
82
- For system testing we have Capybara. This works great and is necessary, but it is good practice to break tests into smaller pieces.
86
+ For system testing we have Capybara. This works great and is necessary for javascript and backend combined apps, but it is good practice to break tests into smaller pieces.
83
87
 
84
- For request-level tests, there are some helpers:
88
+ For the much faster request-level tests, there are some helpers:
85
89
 
86
90
  If the request format is not `turbo_stream`, which is the case on request specs, the method responds in a special html that contains the medadata that is interesting for our tests and is parsed by included test helpers. So tests could look like this:
87
91
 
@@ -96,30 +100,25 @@ RSpec.describe "Articles", type: :request do
96
100
  it 'create failed' do
97
101
  post articles_path(params: invalid_params)
98
102
 
99
- # singular (partial_*) returns false if this element is cero or multiple times rendered to, otherwise the content
100
- expect(partial_html_by_id('flash-box')).to include('Article could not be created')
101
-
102
- expect(response.status).to eq(422)
103
- expect(partials_count).to eq(2)
104
-
105
- expect(partials_paths).to include('articles/form', 'layouts/flash')
106
- expect(partials_ids).to include('form', 'flash-box')
107
-
108
- # ..partials_params_* are the params by what partials are called
109
- # id is the id where, by example, a turbo_stream.replace points to
110
- expect(partials_params_by_id['flash-box'].length).to eq(1)
111
- expect(partials_params_by_id['flash-box'].first['partial']).to eq('layouts/flash')
112
-
113
- # ..partials_html_* is the html returned from the partial as string
114
- expect(partials_html_by_id['flash-box'].length).to eq(1)
115
- expect(partials_html_by_id['flash-box'].first).to include('Article could not be created')
116
-
117
- # *_path is the controller_path for calling the partial
118
- expect(partials_params_by_path['layouts/flash'].length).to eq(1)
119
- expect(partials_params_by_path['layouts/flash'].first['partial']).to eq('layouts/flash')
120
-
121
- expect(partials_html_by_path['layouts/flash'].length).to eq(1)
122
- expect(partials_html_by_path['layouts/flash'].first).to include('Article could not be created')
103
+ # to which ids turbo-stream responded
104
+ expect(partials_ids).to include('flash-box', 'form')
105
+
106
+ # which partials where rendered
107
+ expect(partials_paths).to include('layouts/flash', 'articles/form')
108
+
109
+ # 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
110
+ r = partial_response(id: 'flash-box')
111
+ expect(r.css('div.text-content').first.inner_html).to include('Article could not be created')
112
+
113
+ # partials_responses (plural) returns a array
114
+ r2 = partials_responses(id: 'flash-box')
115
+ expect(r2.length).to eq(1)
116
+
117
+ # same, based on path by which partial is rendered to
118
+ r = partial_response(partial: 'layouts/flash')
119
+ expect(r.css('div.text-content').first.inner_html).to include('Article could not be created')
120
+ r2 = partials_responses(partial: 'layouts/flash')
121
+ expect(r2.length).to eq(1)
123
122
  end
124
123
  end
125
124
  ```
@@ -130,7 +129,7 @@ The test for a redirection might look something like this:
130
129
  it 'update success' do
131
130
  a = FactoryBot.create(:article)
132
131
  patch article_path(a, params: valid_params)
133
- expect(turbo_redirected_to).to eq(articles_path) #=> turbo_redirected_to includes a «expect(response.status).to eq(302)»
132
+ expect(turbo_redirected_to).to eq(articles_path)
134
133
  end
135
134
  ```
136
135
 
@@ -138,7 +137,7 @@ P.S.:
138
137
 
139
138
  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.
140
139
 
141
- ## Parameters
140
+ ## Parameters for turbo_stream_save
142
141
 
143
142
  `save_action` (first parameter, boolean) true if successful
144
143
 
@@ -1,33 +1,12 @@
1
- <% partials_count = 0 %>
2
1
  <% rendered_partials = [] %>
3
- <% htmls_by_id = {} %>
4
- <% params_by_id = {} %>
5
- <% htmls_by_path = {} %>
6
- <% params_by_path = {} %>
7
2
 
8
3
  <% streams.each do |s| %>
9
4
 
10
- <% html = (render s[:partial], locals: s[:locals], formats: [:html ]) %>
11
- <% htmls_by_id[s[:id]] ||= [] %>
12
- <% htmls_by_id[s[:id]].push(html) %>
13
- <% params_by_id[s[:id]] ||= [] %>
14
- <% params_by_id[s[:id]].push(s) %>
5
+ <% html = (render s[:partial], locals: s[:locals], formats: [:html]) %>
15
6
 
16
- <% htmls_by_path[s[:partial]] ||= [] %>
17
- <% htmls_by_path[s[:partial]].push(html) %>
18
- <% params_by_path[s[:partial]] ||= [] %>
19
- <% params_by_path[s[:partial]].push(s) %>
20
-
21
- <% partials_count += 1 %>
22
- <% rendered_partials.push(s[:partial]) %>
7
+ <% rendered_partials.push({ html_response: html }.merge(s)) %>
23
8
 
24
9
  <% end %>
25
10
 
26
- <%= content_tag :div, htmls_by_id.to_json, id: 'htmls_by_id' %>
27
- <%= content_tag :div, params_by_id.to_json, id: 'params_by_id' %>
28
-
29
- <%= content_tag :div, htmls_by_path.to_json, id: 'htmls_by_path' %>
30
- <%= content_tag :div, params_by_path.to_json, id: 'params_by_path' %>
31
-
32
- <%= content_tag :div, check.to_json, id: 'check-json' %>
33
11
  <%= content_tag :div, rendered_partials.to_json, id: 'rendered-partials' %>
12
+
@@ -1,57 +1,56 @@
1
1
  module RenderTurboStream
2
2
  module TestHelpers
3
3
 
4
- # returns hash of arrays with responded html(as string) contents by ID
5
- def partials_html_by_id
6
- e = Nokogiri::HTML(response.body).search('#htmls_by_id').first
7
- JSON.parse(e.inner_html)
8
- end
9
-
10
- # returns false if id is cero or multiple times rendered to
11
- def partial_html_by_id(id)
12
- p = partials_html_by_id[id]
13
- if p.length != 1
4
+ # returns false if partial is rendered cero or multiple times
5
+ # response can be searched by nokogiri https://nokogiri.org/tutorials/searching_a_xml_html_document.html#basic-searching
6
+ def partial_response(id: nil, partial: nil)
7
+ res = partials_responses(id: id, partial: partial)
8
+ if res.length != 1
14
9
  false
15
10
  else
16
- p.first
11
+ res.first
17
12
  end
18
13
  end
19
14
 
20
- def partials_params_by_id
21
- e = Nokogiri::HTML(response.body).search('#params_by_id').first
22
- JSON.parse(e.inner_html)
23
- end
24
-
25
- # returns hash of arrays with responded html(as string) contents by partial-path
26
- def partials_html_by_path
27
- e = Nokogiri::HTML(response.body).search('#htmls_by_path').first
28
- JSON.parse(e.inner_html)
29
- end
15
+ # like partial_response, but returns an array with length of how many times partial was rendered
30
16
 
31
- # returns hash of arrays with params by them rendered partials are called
32
- def partials_params_by_path
33
- e = Nokogiri::HTML(response.body).search('#params_by_path').first
34
- JSON.parse(e.inner_html)
35
- end
36
-
37
- # count of rendered parials
38
- def partials_count
39
- partials_paths.length
17
+ def partials_responses(id: nil, partial: nil)
18
+ ary = partials_attributes
19
+ if id && partial
20
+ res = ary.select { |a| a['partial'] == partial && a['id'] == id }
21
+ elsif id
22
+ res = ary.select { |a| a['id'] == id }
23
+ elsif partial
24
+ res = ary.select { |a| a['partial'] == partial }
25
+ else
26
+ raise 'missing parameter'
27
+ end
28
+ res.map{|r| Nokogiri::HTML(r['html_response']) }
40
29
  end
41
30
 
42
- # array of strings of rendered partial ids
31
+ # array of strings of ids of rendered partials that turbo_stream pointed to
43
32
  def partials_ids
44
- e = Nokogiri::HTML(response.body).search('#check-json').first
45
- JSON.parse(e.inner_html).keys
33
+ ary = partials_attributes
34
+ ids = []
35
+ ary.each {|a| ids.push(a['id']) unless ids.include?(a['id']) }
36
+ ids
46
37
  end
47
38
 
48
- # array of strings of paths by them rendered partials are called, example: ['articles/form']
39
+ # array of strings of paths by which partials are rendered, for example: ['articles/form'].
49
40
  def partials_paths
41
+ ary = partials_attributes
42
+ paths = []
43
+ ary.each {|a| paths.push(a['partial']) unless paths.include?(a['partial']) }
44
+ paths
45
+ end
46
+
47
+ # returns an array of hashes with the attributes used to call partials and the response (html as string) from the partial.
48
+ def partials_attributes
50
49
  e = Nokogiri::HTML(response.body).search('#rendered-partials').first
51
50
  JSON.parse(e.inner_html)
52
51
  end
53
52
 
54
- # check response status and return redirected_to path
53
+ # Returns the path to which turbo_stream.redirect_to would be applied.
55
54
  def turbo_redirected_to
56
55
  expect(response.status).to eq(302)
57
56
  r = JSON.parse(response.body)
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "0.1.24"
2
+ VERSION = "0.1.26"
3
3
  end
@@ -8,11 +8,11 @@ module RenderTurboStream
8
8
  def turbo_stream_save(
9
9
  save_action,
10
10
  redirect_on_success_to: nil,
11
- object: nil,
11
+ object: nil, # object used in save_action, example: @customer
12
12
  id: 'form', # if nil: no partial is rendered
13
- partial: nil, # default: same like ID
14
- action: 'replace',
15
- locals: {},
13
+ partial: nil, # example: 'customers/form' default: "#{controller_path}/#{id}"
14
+ action: 'replace', # options: append, prepend
15
+ locals: {}, # locals used by the partial
16
16
  streams_on_success: [
17
17
  {
18
18
  id: nil,
@@ -20,7 +20,7 @@ module RenderTurboStream
20
20
  locals: {},
21
21
  action: 'replace'
22
22
  }
23
- ],
23
+ ], # additional partials that should be rendered if save_action succeeded
24
24
  streams_on_error: [
25
25
  {
26
26
  id: nil,
@@ -28,7 +28,7 @@ module RenderTurboStream
28
28
  locals: {},
29
29
  action: 'replace'
30
30
  }
31
- ],
31
+ ], # additional partials that should be rendered if save_action failed
32
32
  add_flash_alerts: [], #=> array of strings
33
33
  add_flash_notices: [], #=> array of strings
34
34
  flashes_on_success: [], #=> array of strings
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.24
4
+ version: 0.1.26
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-18 00:00:00.000000000 Z
11
+ date: 2023-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails