render_turbo_stream 0.1.0 → 0.1.1

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: 64475d60368f483b3c49c64f73c06d76a7c6775fb5be7321849ce16f76551f84
4
- data.tar.gz: 2faf94105673ad2b35f7bd3aa241c162fd76c1f6077dff4a048f0fe679241f00
3
+ metadata.gz: 5f3c8127d89c8a7f1524e097ebf9114f073cf91d59c63d67e8a25826ed64d7e0
4
+ data.tar.gz: 638b55cee98740414694ebe30f360f9d54b1bb6cf5adfd64e9c6bc449ea50048
5
5
  SHA512:
6
- metadata.gz: 81e3484874866d30e48f3a1f7784d46a4286f04ff670f63a5adf224bc82ce99d999b9a7e2f2c697f626cc5ebcdc1d296129d516544716205e602166a1c2d4775
7
- data.tar.gz: d064295d9acf3a9478cffb35d98f3c2e594168f81e68ec067123238c086cc51566763d77100fc76305640cc9a59a95438e604d26c4d5b95279d42cf47dd60704
6
+ metadata.gz: b3356f7781e94451527047e3ea8f82423309a68c7133dbaf8965fab805f24c4df2e15e7304b66e254e012b1a05c9d08942e3ece3c7bd76c79c1aeb9da6e14294
7
+ data.tar.gz: db7b4e1fef6b047887b193943d529e9902e37f853b7224c581b0f7ea5b0fcaf57826916aab9ffac09cb6906fa231da8b05e6a603902b1f9cdc36e24d6ee513c8
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # RenderTurboStream
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ Working consistently by turbo_stream means: shooting a lot of partials to the html which always needs the same attributes: ID, partial and maybe some locals. This gem is a attempt to serialize that, especially for `update` and `create` action.
4
+
5
+ Logic is better placed inside the controller, instead of the view and it's boring to write always the same `ifs` inside the `update.turbo_stream.haml`. So this gem holds a `.turbo_stream.html.erb` view that loops through all the partials that should be rendered.
6
+
7
+ In 90% of cases you have just define your partials, like `_form.haml` and steer them from the controller directly, without a `.turbo_stream.haml` view.
6
8
 
7
9
  ## Installation
8
10
  Add this line to your application's Gemfile:
@@ -21,8 +23,97 @@ Or install it yourself as:
21
23
  $ gem install render_turbo_stream
22
24
  ```
23
25
 
26
+ and follow the instructions of `turbo_power`, not rails side, but npm package and imports on javascript side, for making redirects working.
27
+
28
+ ## Usage
29
+
30
+ ```ruby
31
+ def create
32
+ @customer = Customer.new(customer_params)
33
+
34
+ render_turbo_stream(
35
+ @customer.save,
36
+ redirect_on_success_to: edit_customer_path(@customer),
37
+ frame_id: 'customer-form-wrapper',
38
+ partial: 'customer_form',
39
+ )
40
+
41
+ end
42
+ ```
43
+
44
+ and its done!
45
+
46
+ in 90% use cases it should be enough and not necessary writing any code inside `update.turbo_stream.haml` or `create.turbo_stream.haml`
47
+
48
+ ## Testing
49
+
50
+ Testing by regular `rspec` request specs the controller is rendering classic html templates, not turbo_stream. For that html templates are rendered, but only with hints so that your test can check which action is done.
51
+
52
+ It has 3 cases of output with its resulting status codes which can be checked by rspec:
53
+
54
+ Success and redirection: `302`
55
+
56
+ Success without redirection: `200`
57
+
58
+ Save Action failed: `422` `:unprocessable_entity`
59
+
60
+ ## Parameters
61
+
62
+ `save_action` (first parameter, boolean) true if successful
63
+
64
+ `redirect_on_success_to:` path for redirection in case of success, if nil: no redirection.
65
+
66
+ `success_message:` custom message if action is successful
67
+
68
+ `error_message:` custom message if action has failed
69
+
70
+ `object:` default: object (e.g. @customer) is derived from controller name
71
+
72
+ `id:` html-ID for the object that is replaced by turbo_stream, if nil: no partial is rendered
73
+
74
+ `partial:` default: 'form', assumes `controller_path`/_form
75
+
76
+ `locals:` locals for the partial, as hash
77
+
78
+ `replace_on_success:` array of hashes: `[{ id: nil, partial: '', locals: {} }]`
79
+
80
+ `replace_on_error:` array of hashes: `[{ id: nil, partial: '', locals: {} }]`
81
+
82
+ `add_flash_notices:` additional alerts for the case of failure, example: `["custom-success-message"]`
83
+
84
+ `add_flash_alerts:` additional alerts for the case of success, example: `["custom-error-message"]`
85
+
86
+ ## Requirements
87
+
88
+ gem `turbo_power` (is included, used for redirection)
89
+
90
+ **Translations used**
91
+
92
+ `activerecord.success.successfully_created`
93
+
94
+ `activerecord.success.successfully_updated`
95
+
96
+ `activerecord.errors.messages.could_not_create`
97
+
98
+ `activerecord.errors.messages.could_not_update`
99
+
100
+ .. and Model Name Translations
101
+
102
+ **Flash configuration**
103
+
104
+ Flash messages are pushed as arrays into `flash[:alert]`, `flash.now[:alert]`, `flash[:notice]` or `flash.now[:notice]`
105
+
106
+ The corresponding partial has to be configured by
107
+
108
+ `config.x.render_turbo_stream.flash_partial` #=> example: `layouts/flashes`
109
+
110
+ and a corresponding html-ID for pushing the partial to the right destination inside the layout
111
+
112
+ `config.x.render_turbo_stream.flash_id` #=> example: `flash-wrapper`
113
+
24
114
  ## Contributing
25
- Contribution directions go here.
115
+
116
+ Contribution welcome.
26
117
 
27
118
  ## License
28
119
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,144 @@
1
+ module RenderTurboStream
2
+ module RenderTurboStream
3
+ # extend ActiveSupport::Concern
4
+ #
5
+ # class_methods do
6
+ # def render_turbo_stream(options = {})
7
+ # "doing"
8
+ # end
9
+ # end
10
+ def render_turbo_stream(
11
+ save_action,
12
+ redirect_on_success_to: nil,
13
+ success_message: nil,
14
+ error_message: nil,
15
+ object: nil,
16
+ id: nil, # if no frame-id given: no partial is rendered
17
+ partial: 'form',
18
+ locals: {},
19
+ replace_on_success: [
20
+ {
21
+ id: nil,
22
+ partial: 'form',
23
+ locals: {}
24
+ }
25
+ ],
26
+ replace_on_error: [
27
+ {
28
+ id: nil,
29
+ partial: 'form',
30
+ locals: {}
31
+ }
32
+ ],
33
+ add_flash_alerts: [],
34
+ add_flash_notices: []
35
+ )
36
+
37
+ #== flash partial
38
+
39
+ @render_turbo_flash_partial = Rails.configuration.x.render_turbo_stream.flash_partial
40
+ @render_turbo_flash_id = Rails.configuration.x.render_turbo_stream.flash_id
41
+
42
+ #== object
43
+
44
+ unless object
45
+ object = eval("@#{controller_name.classify.underscore}")
46
+ end
47
+
48
+ #== set status
49
+
50
+ if save_action == false
51
+ response.status = :unprocessable_entity
52
+ end
53
+
54
+ #== generate message
55
+
56
+ if save_action
57
+ _msg = if success_message
58
+ success_message
59
+ elsif action_name == 'create'
60
+ I18n.t('activerecord.success.successfully_created')
61
+ elsif action_name == 'update'
62
+ I18n.t('activerecord.success.successfully_updated')
63
+ end
64
+ messages = [
65
+ [
66
+ object.model_name.human,
67
+ _msg
68
+ ].join(' ')
69
+ ]
70
+ else
71
+ _msg = if error_message
72
+ error_message
73
+ elsif action_name == 'create'
74
+ I18n.t('activerecord.errors.messages.could_not_create')
75
+ elsif action_name == 'update'
76
+ I18n.t('activerecord.errors.messages.could_not_update')
77
+ end
78
+ messages = [
79
+ [
80
+ object.model_name.human,
81
+ _msg
82
+ ].join(' ')
83
+ ]
84
+ end
85
+
86
+ #== replace frames
87
+
88
+ replace_frames = []
89
+ if id
90
+ replace_frames.push({
91
+ id: id,
92
+ partial: partial,
93
+ locals: locals
94
+ })
95
+ end
96
+ if save_action
97
+ replace_on_success.each do |f|
98
+ next unless f[:id]
99
+ replace_frames.push(f)
100
+ end
101
+ else
102
+ replace_on_error.each do |f|
103
+ next unless f[:id]
104
+ replace_frames.push(f)
105
+ end
106
+ end
107
+
108
+ #== render
109
+
110
+ views_folder = Pathname.new(Bundler.rubygems.find_name('render_turbo_stream').first.full_gem_path).join('views')
111
+ suffix = (request.format.to_sym == :html ? '.html.erb' : '.turbo_stream.erb')
112
+
113
+ if save_action && redirect_on_success_to
114
+ flash[:notice] = messages.to_a + add_flash_notices.to_a
115
+ flash[:alert] = add_flash_alerts
116
+ render file: views_folder.join("turbo_stream_redirect#{suffix}"),
117
+ locals: {
118
+ url: redirect_on_success_to
119
+ },
120
+ layout: false,
121
+ status: 302
122
+
123
+ elsif save_action
124
+ flash.now[:notice] = messages.to_a + add_flash_notices.to_a
125
+ flash.now[:alert] = add_flash_alerts
126
+ render file: views_folder.join("turbo_stream_partials#{suffix}"),
127
+ locals: {
128
+ replace_frames: replace_frames
129
+ },
130
+ layout: false
131
+ else
132
+ flash.now[:alert] = messages.to_a + add_flash_alerts.to_a
133
+ flash.now[:notice] = add_flash_notices
134
+ render file: views_folder.join("turbo_stream_partials#{suffix}"),
135
+ locals: {
136
+ replace_frames: replace_frames
137
+ },
138
+ layout: false
139
+
140
+ end
141
+
142
+ end
143
+ end
144
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderTurboStream
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require "render_turbo_stream/version"
2
2
  require "render_turbo_stream/railtie"
3
- require "render_turbo_stream/core_ext"
3
+ require "render_turbo_stream/render_turbo_stream"
4
4
 
5
5
  module RenderTurboStream
6
6
  # Your code goes here...
@@ -1,4 +1,7 @@
1
- # desc "Explaining what the task does"
2
- # task :render_turbo_stream do
3
- # # Task goes here
1
+ # desc "install the turbo_stream"
2
+ # namespace :render_turbo_stream do
3
+ # task install: :environment do
4
+ # gem_views = Bundler.rubygems.find_name('render_turbo_stream').first.full_gem_path + '/turbo_stream'
5
+ # FileUtils.copy_entry gem_views, Rails.application.root.join('app', 'turbo_stream', 'test')
6
+ # end
4
7
  # end
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.0
4
+ version: 0.1.1
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-12 00:00:00.000000000 Z
11
+ date: 2023-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 7.0.4.3
27
- description: NOT USABLE YET
27
+ description: Rendering update and create actions by turbo stream
28
28
  email:
29
29
  - christian@sedlmair.ch
30
30
  executables: []
@@ -34,17 +34,17 @@ files:
34
34
  - README.md
35
35
  - Rakefile
36
36
  - lib/render_turbo_stream.rb
37
- - lib/render_turbo_stream/core_ext.rb
38
37
  - lib/render_turbo_stream/railtie.rb
38
+ - lib/render_turbo_stream/render_turbo_stream.rb
39
39
  - lib/render_turbo_stream/version.rb
40
40
  - lib/tasks/render_turbo_stream_tasks.rake
41
- homepage: https://sedlmair.ch
41
+ homepage: https://gitlab.com/sedl/render-turbo-stream
42
42
  licenses:
43
43
  - MIT
44
44
  metadata:
45
- homepage_uri: https://sedlmair.ch
46
- source_code_uri: https://sedlmair.ch
47
- changelog_uri: https://sedlmair.ch
45
+ homepage_uri: https://gitlab.com/sedl/render-turbo-stream
46
+ source_code_uri: https://gitlab.com/sedl/render-turbo-stream
47
+ changelog_uri: https://gitlab.com/sedl/render-turbo-stream
48
48
  post_install_message:
49
49
  rdoc_options: []
50
50
  require_paths:
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []
63
- rubygems_version: 3.1.6
63
+ rubygems_version: 3.4.12
64
64
  signing_key:
65
65
  specification_version: 4
66
66
  summary: NOT USABLE YET
@@ -1,5 +0,0 @@
1
- class String
2
- def to_squak
3
- "squawk! #{self}".strip
4
- end
5
- end