render_turbo_stream 0.1.19 → 0.1.20

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: f816fb8eab0243274dc8b448982358b90e12fe3d7d431f104dd12d3e56d2aacb
4
- data.tar.gz: 1b515e61a40ac0126aba2cc43940b181f3fca19e3305320d13eb0e5848f4fc7e
3
+ metadata.gz: 5c9a8ce6edc86ce1bf4adb28042784f90665fb915d32c663899fa66fa6f6a764
4
+ data.tar.gz: cb31e25e686751127e6eb21562ab31bb0a15c25a7760691be2ace27fd32872a2
5
5
  SHA512:
6
- metadata.gz: a98cc8fff61ac5044c0af6051a59262383b1f41d5b1559bcfc2ccd3b87e46df14a576fd7615286c51617e76b2570046f84c10dade94c71c9757103fe29116a3b
7
- data.tar.gz: c09722e38db120023eb577187ef4f7c1b1e3562a6ab6af6cf9ed3f86f2b8acf36df96d1aa621cbc796ad73cf272d9adbb67d9ebd2f3872cbfcc73447b9041438
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
- necessary configs for flash partials
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 streamline `update` or `create` functions for `turbo_stream`, which could look like so:
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 sets a status, generates a flash message and performs `stream_partials`, which could result like that:
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` method is nothing else than a singular of `stream_partials`
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 capybara.
82
+ For system testing, there is Capybara.
83
83
 
84
- For Tests on request level there are some helpers:
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 html by a special format that can easily be checked by included test-helpers. So, tests could look like this:
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 'update failed' do
95
- a = FactoryBot.create(:article)
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
- expect(rendered_partials).to include('articles/form', 'layouts/flash')
100
- expect(streamed_ids).to include('form', 'flash-box')
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
- Test for a redirection could look like this:
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, rendered_partials.to_json, id: 'rendered-partials' %>
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
- def rendered_partials
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 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 }
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 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
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
- rendered_partials.length
40
+ rendered_partials_paths.length
29
41
  end
30
42
  def turbo_redirected_to
31
43
  expect(response.status).to eq(302)
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "0.1.19"
2
+ VERSION = "0.1.20"
3
3
  end
@@ -138,7 +138,8 @@ module RenderTurboStream
138
138
  def stream_partials(array)
139
139
 
140
140
  ary = []
141
- array.dup.each do |props|
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_turbo_stream
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.19
4
+ version: 0.1.20
5
5
  platform: ruby
6
6
  authors:
7
7
  - christian