hot_flash 1.0.0
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 +7 -0
- data/CHANGELOG.md +17 -0
- data/MIT-LICENSE +20 -0
- data/README.md +139 -0
- data/Rakefile +3 -0
- data/lib/generators/hot_flash/install_generator.rb +13 -0
- data/lib/generators/hot_flash/templates/config.rb.tt +13 -0
- data/lib/hot_flash/action.rb +59 -0
- data/lib/hot_flash/controller.rb +42 -0
- data/lib/hot_flash/railtie.rb +10 -0
- data/lib/hot_flash/version.rb +3 -0
- data/lib/hot_flash.rb +25 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 1064d37097d0d1e1a3e2425ab5d6281d37f5f22cc5d3b2a560d7eb5b1483ee83
|
4
|
+
data.tar.gz: 30e970f56bae0f5bd33949f44f4597d399a6e70f528cc3fb1a23ef4ec155715f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a654e05d1f161ecd25a550cd0dec69045d4c600c50ab407e9f34ca3cfa8e3abf87578322b3cb6e13ac3cbf9d35bebd64fe73867fd1a192a2e5584a6c4bd82794
|
7
|
+
data.tar.gz: 014fe81e8df010bb28be02d22f5ed0e43b91e4ef30b6c63af6ac4720af7279c50630a5507fc80a21ca43db94e26faeda50b97ccbef550b1be9d47f6a6c793ec5
|
data/CHANGELOG.md
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2023 Partytray
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
# <img src="./logo.png" style="max-height:100px;" /> HotFlash
|
2
|
+
|
3
|
+
Automagically inject flash messages into Ruby on Rails TurboStream responses using [Hotwire](https://github.com/hotwired/turbo-rails).
|
4
|
+
|
5
|
+
<img src="./demo.gif" alt="Example of automatically injecting flash messages into Ruby on Rails TurboStream responses using Hotwire." />
|
6
|
+
|
7
|
+
## Why
|
8
|
+
|
9
|
+
Because it's weird? I don't know.
|
10
|
+
|
11
|
+
I originally developed [TurboFlash](https://github.com/joshmn/turbo_flash) as a proof of concept, but people ended up using it, and, even scarier, contributing back to it. This terrified me because...
|
12
|
+
|
13
|
+
I truthfully hate the implementation. It felt like one big hack. I knew a better way to do it. So, here it is, in all its glory, "better", but I still don't like it.
|
14
|
+
|
15
|
+
Granted, this implementation is arguably just as "bad" because it intercepts `render`, but it's clean, _should_ be relatively safe, and works.
|
16
|
+
|
17
|
+
## How it works
|
18
|
+
|
19
|
+
HotFlash is about 100 lines of code, mostly of convenience methods and developer happiness code. It's not scary. This code effectively does the following, so you don't have to:
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
def create
|
23
|
+
@post = Post.new(post_params)
|
24
|
+
if @post.save
|
25
|
+
redirect_to @post
|
26
|
+
else
|
27
|
+
render turbo_stream: [
|
28
|
+
turbo_stream.replace(:form, "form", locals: { post: @post }),
|
29
|
+
turbo_stream.replace(:flashes, "shared/flashes") # <-- this
|
30
|
+
]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### 1. Add it to your application:
|
38
|
+
|
39
|
+
```shell
|
40
|
+
bundle add hot_flash
|
41
|
+
```
|
42
|
+
|
43
|
+
### 2. Install it
|
44
|
+
|
45
|
+
```shell
|
46
|
+
rails g hot_flash:install
|
47
|
+
```
|
48
|
+
|
49
|
+
Will copy over an initializer.
|
50
|
+
|
51
|
+
### 3. Make it work with your application
|
52
|
+
|
53
|
+
It's not enabled out-of-the-box, so you'll need to enable it:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class ApplicationController < ActionController::Base
|
57
|
+
before_action :enable_hotflash
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
HotFlashes expects you to have a target of `#flashes` available, and a method called `#render_flash` available as a helper.
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
module ApplicationHelper
|
65
|
+
def render_flash
|
66
|
+
return if @_flash_rendered
|
67
|
+
|
68
|
+
render partial: "shared/flash"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Where `app/views/shared/flash.html.erb` renders the flash contents and includes the target selector:
|
74
|
+
|
75
|
+
```html
|
76
|
+
<div id="flashes">
|
77
|
+
<% flash.each do |key, value| %>
|
78
|
+
<div><%= key %>: <%= value %></div>
|
79
|
+
<% end %>
|
80
|
+
</div>
|
81
|
+
```
|
82
|
+
|
83
|
+
### 4. Get on with your day because you're done
|
84
|
+
|
85
|
+
All flashes are rendered just like they would be in the default request/response cycle; no need to do any of the hack-y junk that TurboFlash did.
|
86
|
+
|
87
|
+
### 5. But you can get more granular:
|
88
|
+
|
89
|
+
#### Disable or enable:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
class PostsController < ApplicationController
|
93
|
+
disable_hotflash
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
or,
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class PostsController < ApplicationController
|
101
|
+
disable_hotflash only: [:new]
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
or,
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
class PostsController < ApplicationController
|
109
|
+
enable_hotflash only: [:new]
|
110
|
+
end
|
111
|
+
```
|
112
|
+
|
113
|
+
#### Fiddle with HotFlashes on yourself
|
114
|
+
|
115
|
+
HotFlashes has a `hotflash` method that you can interact with if you want:
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
class PostsController < ApplicationController
|
119
|
+
before_action :disable_hotflash
|
120
|
+
before_action :interact_with_hotflash
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
def interact_with_hotflash
|
125
|
+
hotflash.enable!
|
126
|
+
hotflash.turbo_action = "update"
|
127
|
+
hotflash.flash_message = :some_other_way_to_show_the_message
|
128
|
+
hotflash.turbo_target = '#some_other_target'
|
129
|
+
end
|
130
|
+
end
|
131
|
+
```
|
132
|
+
|
133
|
+
## Contributing
|
134
|
+
|
135
|
+
`flash_method` could probably use some options.
|
136
|
+
|
137
|
+
## License
|
138
|
+
|
139
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module HotFlash
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.join(__dir__, "templates")
|
7
|
+
|
8
|
+
def copy_templates
|
9
|
+
template "config.rb", "config/initializers/hotflash.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
HotFlash.configure do |config|
|
2
|
+
# ==> Turbo Target
|
3
|
+
# config.turbo_target = "#flashes"
|
4
|
+
#
|
5
|
+
# ==> Turbo Action
|
6
|
+
# config.turbo_action = "replace"
|
7
|
+
#
|
8
|
+
# ==> Flash Method
|
9
|
+
# This should be exposed either on the controller-level, either via an instance method on the controller or exposed
|
10
|
+
# to the controller via a traditional helper module
|
11
|
+
#
|
12
|
+
# config.flash_method = :render_flash
|
13
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module HotFlash
|
2
|
+
class Action
|
3
|
+
def initialize(controller)
|
4
|
+
@controller = controller
|
5
|
+
@enabled = false
|
6
|
+
@turbo_action = HotFlash.config.turbo_action
|
7
|
+
@turbo_target = HotFlash.config.turbo_target
|
8
|
+
@flash_method = HotFlash.config.flash_method
|
9
|
+
end
|
10
|
+
|
11
|
+
def enable!
|
12
|
+
@enabled = true
|
13
|
+
end
|
14
|
+
|
15
|
+
def disable!
|
16
|
+
@enabled = false
|
17
|
+
end
|
18
|
+
|
19
|
+
def enabled?
|
20
|
+
@enabled
|
21
|
+
end
|
22
|
+
|
23
|
+
def render
|
24
|
+
@controller.view_context.turbo_stream_action_tag(turbo_action, targets: turbo_target, template: content)
|
25
|
+
end
|
26
|
+
|
27
|
+
def turbo_action
|
28
|
+
@turbo_action
|
29
|
+
end
|
30
|
+
|
31
|
+
def turbo_target
|
32
|
+
@turbo_target
|
33
|
+
end
|
34
|
+
|
35
|
+
def render_into(render_args)
|
36
|
+
return render_args unless render?(render_args)
|
37
|
+
|
38
|
+
render_args[0][:turbo_stream] << render
|
39
|
+
render_args
|
40
|
+
end
|
41
|
+
|
42
|
+
def render?(render_args)
|
43
|
+
return false unless enabled?
|
44
|
+
return false unless render_args[0] && render_args[0][:turbo_stream]
|
45
|
+
|
46
|
+
true
|
47
|
+
end
|
48
|
+
|
49
|
+
def content
|
50
|
+
if @controller.respond_to?(@flash_method)
|
51
|
+
return @controller.send(@flash_method)
|
52
|
+
elsif @controller.helpers.respond_to?(@flash_method)
|
53
|
+
return @controller.helpers.send(@flash_method)
|
54
|
+
else
|
55
|
+
raise NoMethodError, "#{@controller.class.name} doesn't respond to #{@flash_method}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'hot_flash/action'
|
2
|
+
|
3
|
+
module HotFlash
|
4
|
+
module Controller
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def render(*args)
|
10
|
+
args = hotflash.render_into(args)
|
11
|
+
|
12
|
+
super(*args)
|
13
|
+
end
|
14
|
+
|
15
|
+
class_methods do
|
16
|
+
private
|
17
|
+
|
18
|
+
def disable_hotflash(**options)
|
19
|
+
before_action :disable_hotflash, **options
|
20
|
+
end
|
21
|
+
|
22
|
+
def enable_hotflash(**options)
|
23
|
+
before_action :enable_hotflash, **options
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def enable_hotflash
|
30
|
+
hotflash.enable!
|
31
|
+
end
|
32
|
+
|
33
|
+
def disable_hotflash
|
34
|
+
hotflash.disable!
|
35
|
+
end
|
36
|
+
|
37
|
+
def hotflash
|
38
|
+
@hotflash ||= HotFlash::Action.new(self)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
data/lib/hot_flash.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require "hot_flash/version"
|
2
|
+
require "hot_flash/railtie"
|
3
|
+
require 'hot_flash/controller'
|
4
|
+
|
5
|
+
module HotFlash
|
6
|
+
class Configuration
|
7
|
+
attr_accessor :turbo_action
|
8
|
+
attr_accessor :turbo_target
|
9
|
+
attr_accessor :flash_method
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@turbo_action = "replace"
|
13
|
+
@turbo_target = "#flashes"
|
14
|
+
@flash_method = :render_flash
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.config
|
19
|
+
@config ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.configure
|
23
|
+
yield config
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hot_flash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- joshmn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-08-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec-rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Automatically inject flash messages into TurboStream responses.
|
28
|
+
email:
|
29
|
+
- josh@josh.mn
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- CHANGELOG.md
|
35
|
+
- MIT-LICENSE
|
36
|
+
- README.md
|
37
|
+
- Rakefile
|
38
|
+
- lib/generators/hot_flash/install_generator.rb
|
39
|
+
- lib/generators/hot_flash/templates/config.rb.tt
|
40
|
+
- lib/hot_flash.rb
|
41
|
+
- lib/hot_flash/action.rb
|
42
|
+
- lib/hot_flash/controller.rb
|
43
|
+
- lib/hot_flash/railtie.rb
|
44
|
+
- lib/hot_flash/version.rb
|
45
|
+
homepage: https://github.com/joshmn/hot_flashes
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata:
|
49
|
+
homepage_uri: https://github.com/joshmn/hot_flashes
|
50
|
+
source_code_uri: https://github.com/joshmn/hot_flashes
|
51
|
+
changelog_uri: https://github.com/joshmn/hot_flashes
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.3.11
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: Automatically inject flash messages into TurboStream responses.
|
71
|
+
test_files: []
|