turbo_flash 1.0.0 → 2.0.0

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: b64991ca74590443beba07f47dad71373ef249fed3ddc1000dfbf94ffd559cd3
4
- data.tar.gz: 6313bd6233fb417fcdc1c4271db595d7752fbabc4df4740debc71417ff386411
3
+ metadata.gz: f918c2e85cac1ba1b597b7cd82cf991b367cb066d5067eca31d62b1bfd0fb020
4
+ data.tar.gz: 4a87182a3f842d5d185c03b8a2810ce772f858df0e79fdd007c08d3d2269bb46
5
5
  SHA512:
6
- metadata.gz: 7401f202da75e70870a25d26a667d6074e21390bca980ea8006d7651af41177f99ab2bbda5a0d309c37aa7de8a3259a6550c3c6fb7d43c2368f26404b6daf4ee
7
- data.tar.gz: 6da6ba2930f45f5606d2e7d2945838cf1dc7ab510476aef2d730c55e21cff28c0d73da5df729e791134cd1139c6296bea1f572b6626a53525da25e9540394dcb
6
+ metadata.gz: 24be5d4a002ba202f00808efe47256fd4ff39a861b3001372325adf3c1dec612ed91bf5a7ff71f7b02aa92805e5ee6b4f15332aafdc0c15240cc75a862b1e6f2
7
+ data.tar.gz: 828855342363d5bb5e1007555b57084329ba6d74bc673e99ba202eaf4fef4109a598ea911225cded5a501158b207fcbc5174e3f4e92a31ff7f7acea0d7a45787
data/README.md CHANGED
@@ -2,11 +2,14 @@
2
2
 
3
3
  Automagically include your flash messages in your Ruby on Rails [TurboStream](https://github.com/hotwired/turbo-rails) responses.
4
4
 
5
- ![Video demo](https://i.imgur.com/vOmf6FV.mp4)
5
+ ![Video demo](https://i.imgur.com/pVqX9ZV.gif)
6
6
 
7
7
  ## Usage
8
8
 
9
- TurboFlash exposes a `flash.turbo` method that's similar to `flash.now`:
9
+ By default, TurboFlash will inherit all flashes that you normally set. This can be turned off with the `inherit_flashes`
10
+ configuration flag.
11
+
12
+ To explicitly set flashes, TurboFlash exposes a `flash.turbo` method that's similar to `flash`:
10
13
 
11
14
  ```ruby
12
15
  class UsersController < ApplicationController
@@ -37,8 +40,6 @@ class UsersController < ApplicationController
37
40
  end
38
41
  ```
39
42
 
40
- Note: You must explicitly call `flash.turbo` (for now). This will not use the original flash hash.
41
-
42
43
  Of course, the response will be what you expect:
43
44
 
44
45
  ```ruby
@@ -112,10 +113,16 @@ Ensure that the TurboStream target — a tag with an `id` of `flash` exists in y
112
113
 
113
114
  ## Configuration
114
115
 
115
- In an initializer:
116
+ In an initializer (defaults are shown):
116
117
 
117
118
  ```
118
119
  TurboFlash.configure do |config|
120
+ # make all flashes TurboFlash-flashes
121
+ # config.inherit_flashes = true
122
+
123
+ # clear the TurboFlash target if there are no flashes in a TurboStream response
124
+ # config.clear_target_unless_flashed = true
125
+
119
126
  # the default TurboStream target element ID
120
127
  # config.target = "flash"
121
128
 
@@ -133,6 +140,16 @@ TurboFlash.configure do |config|
133
140
  end
134
141
  ```
135
142
 
143
+ ## Gotchas
144
+
145
+ If `TurboFlash.configuration.inherit_flashes` is `false`, and you want to copy over the regular flashes,
146
+ you can invoke `flash.turbo!(options = {})` to copy over the flashes that are currently stored in the session.
147
+
148
+ If `TurboFlash.configuration.clear_target_unless_flashed` is `false`, and you would like to clear flashes in the TurboStream
149
+ response, you can invoke `flash.turbo.clear_target!` to clear the TurboStream target if there are no flashes.
150
+
151
+ If you want to clear all potential TurboFlashes, call `flash.turbo.clear!`
152
+
136
153
  ## Contributing
137
154
 
138
155
  There wasn't much thought put into this, but it works for me, so it might work for you!
@@ -2,14 +2,24 @@
2
2
 
3
3
  module TurboFlash
4
4
  class Configuration
5
- attr_accessor :target, :action, :partial, :key, :value
5
+ attr_accessor :target, :action, :partial, :key, :value, :clear_unless_flashed, :inherit_flashes
6
6
 
7
7
  def initialize
8
8
  @target = 'flash'
9
- @action = :append
9
+ @action = :update
10
10
  @partial = 'shared/flash'
11
11
  @key = :role
12
12
  @value = :message
13
+ @clear_target_unless_flashed = true
14
+ @inherit_flashes = true
15
+ end
16
+
17
+ def inherit_flashes?
18
+ @inherit_flashes
19
+ end
20
+
21
+ def clear_target_unless_flashed?
22
+ @clear_target_unless_flashed
13
23
  end
14
24
  end
15
25
  end
@@ -4,8 +4,20 @@ require 'turbo_flash/flash_turbo'
4
4
 
5
5
  module TurboFlash
6
6
  module FlashHash
7
+ # create a Flash::Turbo instance.
8
+ #
9
+ # Behaves just like `flash.now`, but gets injected into your TurboStream response.
10
+ #
11
+ # flash.turbo[:notice] = "There was an error"
7
12
  def turbo
8
13
  @turbo ||= ::TurboFlash::FlashTurbo.new(self)
9
14
  end
15
+
16
+ # Copy over flashes
17
+ def turbo!(options = {})
18
+ turbo.from_flashes(@flashes, options)
19
+
20
+ true
21
+ end
10
22
  end
11
23
  end
@@ -7,12 +7,62 @@ module TurboFlash
7
7
 
8
8
  def initialize(flash)
9
9
  super
10
+ @copied_flashes = false
10
11
  @temp_options = nil
11
12
  @default_render_target = TurboFlash.configuration.target
12
13
  @default_render_options = { action: TurboFlash.configuration.action, partial: TurboFlash.configuration.partial }
13
14
  @locals_key = TurboFlash.configuration.key
14
15
  @locals_value = TurboFlash.configuration.value
15
16
  @options = {}
17
+ @clear_target = false
18
+ @cleared = false
19
+ end
20
+
21
+ def copied_flashes?
22
+ @copied_flashes
23
+ end
24
+
25
+ def from_flashes(other_flashes, options)
26
+ other_flashes.each do |k,v|
27
+ from_flash(k, v, options)
28
+ end
29
+ @copied_flashes = true
30
+
31
+ true
32
+ end
33
+
34
+ def cleared?
35
+ @cleared
36
+ end
37
+
38
+ def unclear!
39
+ @cleared = false
40
+ end
41
+
42
+ def clear!
43
+ @cleared = true
44
+ end
45
+
46
+ def from_flash(k, v, options = {})
47
+ set_options(options)[k] = v
48
+
49
+ true
50
+ end
51
+
52
+ def clear_target?
53
+ @clear_target
54
+ end
55
+
56
+ def clear_target!
57
+ @clear_target = true
58
+ end
59
+
60
+ def reset_clear_target!
61
+ @clear_target = false
62
+ end
63
+
64
+ def flashes
65
+ @flash.instance_variable_get(:@flashes)
16
66
  end
17
67
 
18
68
  def []=(k, v)
@@ -5,10 +5,37 @@ module TurboFlash
5
5
  def render(*args)
6
6
  result = super
7
7
  if request.format.turbo_stream?
8
+ return result if flash.turbo.cleared?
9
+
10
+ flashed = false
8
11
  flash_turbo_options = flash.turbo.options
9
- flash.turbo.instance_variable_get(:@flash).instance_variable_get(:@flashes).each do |key, _value|
12
+ flash.turbo.flashes.each do |key, value|
10
13
  flash_options = flash_turbo_options[key]
14
+ if TurboFlash.configuration.inherit_flashes?
15
+ flash.turbo.from_flash(key, value)
16
+ flash_options = flash_turbo_options[key]
17
+ end
18
+
19
+ if flash_options.nil?
20
+ flash.discard(key)
21
+ next
22
+ end
23
+
11
24
  result << turbo_stream.send(flash_options.delete(:action), flash_options.delete(:target), **flash_options)
25
+ flashed = true
26
+ flash.turbo.flash.discard(key)
27
+ end
28
+
29
+ flash.clear
30
+
31
+ puts "flashed? #{flashed}"
32
+ puts "Clear unless flashed: #{TurboFlash.configuration.clear_target_unless_flashed?}"
33
+ puts "flash.turbo.clear_target : #{flash.turbo.clear_target?}"
34
+ if (!flashed && TurboFlash.configuration.clear_target_unless_flashed?) || flash.turbo.clear_target?
35
+ result << turbo_stream.send(:update, TurboFlash.configuration.target) do
36
+ ""
37
+ end
38
+ flash.turbo.reset_clear_target!
12
39
  end
13
40
  end
14
41
  result
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TurboFlash
4
- VERSION = '1.0.0'
4
+ VERSION = '2.0.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: turbo_flash
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Brody