rails_turbo_flash 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +73 -0
- data/Rakefile +10 -0
- data/app/assets/config/rails_turbo_flash_manifest.js +1 -0
- data/app/assets/stylesheets/rails_turbo_flash/application.css +15 -0
- data/app/controllers/rails_turbo_flash/application_controller.rb +6 -0
- data/app/helpers/rails_turbo_flash/application_helper.rb +6 -0
- data/app/jobs/rails_turbo_flash/application_job.rb +6 -0
- data/app/mailers/rails_turbo_flash/application_mailer.rb +8 -0
- data/app/models/rails_turbo_flash/application_record.rb +7 -0
- data/app/views/layouts/rails_turbo_flash/application.html.erb +15 -0
- data/app/views/turbo_flash/_flash_message.html.erb +8 -0
- data/app/views/turbo_flash/_flash_message.html.haml +5 -0
- data/config/routes.rb +4 -0
- data/lib/generators/rails_turbo_flash/views_generator.rb +17 -0
- data/lib/rails_turbo_flash/callbacks.rb +24 -0
- data/lib/rails_turbo_flash/config.rb +54 -0
- data/lib/rails_turbo_flash/engine.rb +7 -0
- data/lib/rails_turbo_flash/railtie.rb +13 -0
- data/lib/rails_turbo_flash/version.rb +5 -0
- data/lib/rails_turbo_flash/view_helpers.rb +18 -0
- data/lib/rails_turbo_flash.rb +11 -0
- data/lib/tasks/rails_turbo_flash_tasks.rake +5 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a0dda702d66c0f063661e512a565bbaadd341b453fd9491a1271f0931d3c0d93
|
4
|
+
data.tar.gz: 3cd165a8e3c4d3a76b367d00c2cbac020f269cf76ba567bf0aad3a5fb2ef0567
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ff1a3ec4952f2f529a093de6e2c378f7d8853704aa5cf3bc71ee563839c693a5fc7a6c4954ecbaa612b6a4a040c3c4d399b216b511191597eb4e369a3662d11c
|
7
|
+
data.tar.gz: 10d2a5d755ba8c0f5330ed66538f89c320d27febed5e0e58142c8acf4568ffccdfd5ce4365c539c914acaeaa396802189b8a003ce33ed5be1eeeb48b2490f094
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Rails Turbo Flash
|
2
|
+
|
3
|
+
Automatic flash message injection for Ruby on Rails Turbo Stream responses.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add `rails_turbo_flash` to your Gemfile, or:
|
8
|
+
|
9
|
+
```bash
|
10
|
+
$ bundle add rails_turbo_flash
|
11
|
+
```
|
12
|
+
|
13
|
+
## Getting Started
|
14
|
+
|
15
|
+
1. Include the controller helper:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class ApplicationController < ActionController::Base
|
19
|
+
include RailsTurboFlash::Callbacks
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
1. Use the view helper to render a turbo stream target into your top-level layouts. For example, in `app/views/layouts/application.html.erb`:
|
24
|
+
|
25
|
+
```erb
|
26
|
+
<%= turbo_flash_tag %>
|
27
|
+
|
28
|
+
<%# Or with some additional attributes: %>
|
29
|
+
|
30
|
+
<%= turbo_flash_tag class: 'fixed flex gap-8 justify-center top-0 right-0 z-50' %>
|
31
|
+
```
|
32
|
+
|
33
|
+
1. Add flash messages in your controller actions with [`flash.now`](https://api.rubyonrails.org/classes/ActionDispatch/Flash/FlashHash.html#method-i-now).
|
34
|
+
|
35
|
+
That's it!
|
36
|
+
|
37
|
+
## Customizing
|
38
|
+
|
39
|
+
To customize Rails Turbo Flash, create a file `config/initializers/rails_turbo_flash.rb`.
|
40
|
+
|
41
|
+
The default values are shown below:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
RailsTurboFlash.configure do |config|
|
45
|
+
config.action = :prepend # The turbo stream action
|
46
|
+
end
|
47
|
+
```
|
48
|
+
|
49
|
+
To make Rails Turbo Flash look like your app, override the bundled views by adding them to your app. You can manually copy the specific views that you need to `app/views/turbo_flash`, or copy them to your application with the included generator:
|
50
|
+
|
51
|
+
```bash
|
52
|
+
$ rails generate rails_turbo_flash:views
|
53
|
+
```
|
54
|
+
|
55
|
+
See [the bundled views](https://github.com/rnevius/rails_turbo_flash/tree/main/app/views/turbo_flash).
|
56
|
+
|
57
|
+
## Why and How
|
58
|
+
|
59
|
+
Unable to find an authoritative solution from the Turbo Rails team or Rails community for adding flash messages to turbo stream responses, I hacked this gem together. It aims to be [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself), simple, and unobtrusive. This solution was tailored specifically to my needs and _works for me_; I make no guarantees it's good for everyone.
|
60
|
+
|
61
|
+
[Basically](https://github.com/rnevius/rails_turbo_flash/blob/main/lib/rails_turbo_flash/callbacks.rb), it checks for a `turbo_stream` request format and appends a turbo stream to the response body via an `after_action` callback. The entire implementation is only a few lines of code.
|
62
|
+
|
63
|
+
## Contributing
|
64
|
+
|
65
|
+
Here are a few ways you can help:
|
66
|
+
|
67
|
+
- [Report bugs or suggest new features](https://github.com/rnevius/rails_turbo_flash/issues)
|
68
|
+
- Fix bugs, add new features, and [submit pull requests](https://github.com/rnevius/rails_turbo_flash/pulls)
|
69
|
+
- Write, clarify, or fix documentation and tests
|
70
|
+
|
71
|
+
## License
|
72
|
+
|
73
|
+
Open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
//= link_directory ../stylesheets/rails_turbo_flash .css
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Rails turbo flash</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
<%= csp_meta_tag %>
|
7
|
+
|
8
|
+
<%= stylesheet_link_tag "rails_turbo_flash/application", media: "all" %>
|
9
|
+
</head>
|
10
|
+
<body>
|
11
|
+
|
12
|
+
<%= yield %>
|
13
|
+
|
14
|
+
</body>
|
15
|
+
</html>
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<% flash.each do |key, message| %>
|
2
|
+
<div role="dialog" aria-labelledby="<%= "turbo-flash-title-#{key}" %>" aria-describedby="<%= "turbo-flash-body-#{key}" %>">
|
3
|
+
<div id="<%= "turbo-flash-title-#{key}" %>"><%= message['title'] || message %></div>
|
4
|
+
<% if message['body'] %>
|
5
|
+
<div id="<%= "turbo-flash-body-#{key}" %>"><%= message['body'] %></div>
|
6
|
+
<% end %>
|
7
|
+
</div>
|
8
|
+
<% end %>
|
@@ -0,0 +1,5 @@
|
|
1
|
+
- flash.each do |key, message|
|
2
|
+
%div{"aria-describedby" => "turbo-flash-body-#{key}", "aria-labelledby" => "turbo-flash-title-#{key}", :role => "dialog"}
|
3
|
+
%div{:id => "turbo-flash-title-#{key}"}= message['title'] || message
|
4
|
+
- if message['body']
|
5
|
+
%div{:id => "turbo-flash-body-#{key}"}= message['body']
|
data/config/routes.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module RailsTurboFlash
|
4
|
+
module Generators
|
5
|
+
class ViewsGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path('../../../app/views/turbo_flash', __dir__)
|
7
|
+
|
8
|
+
def install
|
9
|
+
if defined?(Haml)
|
10
|
+
copy_file('_flash_message.html.haml', 'app/views/turbo_flash/_flash_message.html.haml')
|
11
|
+
else
|
12
|
+
copy_file('_flash_message.html.erb', 'app/views/turbo_flash/_flash_message.html.erb')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsTurboFlash
|
4
|
+
module Callbacks
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
after_action :append_flash_to_turbo_stream_response
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def append_flash_to_turbo_stream_response
|
14
|
+
return unless request.format.turbo_stream? && flash.any?
|
15
|
+
|
16
|
+
response.write turbo_stream.public_send(
|
17
|
+
RailsTurboFlash.config.action,
|
18
|
+
RailsTurboFlash.config.target,
|
19
|
+
partial: 'turbo_flash/flash_message',
|
20
|
+
locals: { flash: }
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsTurboFlash
|
4
|
+
module Options
|
5
|
+
module ClassMethods
|
6
|
+
def option(name, default: nil)
|
7
|
+
attr_accessor(name)
|
8
|
+
|
9
|
+
schema[name] = default
|
10
|
+
end
|
11
|
+
|
12
|
+
def schema
|
13
|
+
@schema ||= {}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_defaults!
|
18
|
+
self.class.schema.each do |name, default|
|
19
|
+
instance_variable_set("@#{name}", default)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.included(cls)
|
24
|
+
cls.extend(ClassMethods)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Configuration
|
29
|
+
include Options
|
30
|
+
|
31
|
+
option :action, default: :prepend
|
32
|
+
option :target, default: 'turbo-flash'
|
33
|
+
|
34
|
+
def initialize
|
35
|
+
set_defaults!
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
module Configurable
|
40
|
+
attr_writer :config
|
41
|
+
|
42
|
+
def config
|
43
|
+
@config ||= Configuration.new
|
44
|
+
end
|
45
|
+
|
46
|
+
def configure
|
47
|
+
yield(config)
|
48
|
+
end
|
49
|
+
|
50
|
+
def reset_config!
|
51
|
+
@config = Configuration.new
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_turbo_flash/view_helpers'
|
4
|
+
|
5
|
+
module RailsTurboFlash
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer 'rails_turbo_flash.view_helpers' do
|
8
|
+
ActiveSupport.on_load(:action_view) do
|
9
|
+
include RailsTurboFlash::ViewHelpers
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RailsTurboFlash
|
4
|
+
module ViewHelpers
|
5
|
+
# Generates the HTML tag for displaying flash messages using Turbo Streams in Ruby on Rails.
|
6
|
+
#
|
7
|
+
# @param options [Hash] The options for customizing the HTML tag.
|
8
|
+
# @option options [String] :id The ID attribute for the HTML tag.
|
9
|
+
#
|
10
|
+
# @return [String] The HTML tag for displaying flash messages.
|
11
|
+
def turbo_flash_tag(options = nil)
|
12
|
+
options ||= {}
|
13
|
+
options[:id] = RailsTurboFlash.config.target
|
14
|
+
|
15
|
+
tag.div nil, **options
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails_turbo_flash/callbacks'
|
4
|
+
require 'rails_turbo_flash/config'
|
5
|
+
require 'rails_turbo_flash/version'
|
6
|
+
require 'rails_turbo_flash/engine'
|
7
|
+
require 'rails_turbo_flash/railtie' if defined?(Rails)
|
8
|
+
|
9
|
+
module RailsTurboFlash
|
10
|
+
extend Configurable
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails_turbo_flash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Nevius
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: turbo-rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.5'
|
41
|
+
description: Flash message injection for Ruby on Rails TurboStream responses
|
42
|
+
email:
|
43
|
+
- ryan@syntarsus.io
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- README.md
|
49
|
+
- Rakefile
|
50
|
+
- app/assets/config/rails_turbo_flash_manifest.js
|
51
|
+
- app/assets/stylesheets/rails_turbo_flash/application.css
|
52
|
+
- app/controllers/rails_turbo_flash/application_controller.rb
|
53
|
+
- app/helpers/rails_turbo_flash/application_helper.rb
|
54
|
+
- app/jobs/rails_turbo_flash/application_job.rb
|
55
|
+
- app/mailers/rails_turbo_flash/application_mailer.rb
|
56
|
+
- app/models/rails_turbo_flash/application_record.rb
|
57
|
+
- app/views/layouts/rails_turbo_flash/application.html.erb
|
58
|
+
- app/views/turbo_flash/_flash_message.html.erb
|
59
|
+
- app/views/turbo_flash/_flash_message.html.haml
|
60
|
+
- config/routes.rb
|
61
|
+
- lib/generators/rails_turbo_flash/views_generator.rb
|
62
|
+
- lib/rails_turbo_flash.rb
|
63
|
+
- lib/rails_turbo_flash/callbacks.rb
|
64
|
+
- lib/rails_turbo_flash/config.rb
|
65
|
+
- lib/rails_turbo_flash/engine.rb
|
66
|
+
- lib/rails_turbo_flash/railtie.rb
|
67
|
+
- lib/rails_turbo_flash/version.rb
|
68
|
+
- lib/rails_turbo_flash/view_helpers.rb
|
69
|
+
- lib/tasks/rails_turbo_flash_tasks.rake
|
70
|
+
homepage: https://github.com/rnevius/rails_turbo_flash
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata:
|
74
|
+
allowed_push_host: https://rubygems.org
|
75
|
+
homepage_uri: https://github.com/rnevius/rails_turbo_flash
|
76
|
+
source_code_uri: https://github.com/rnevius/rails_turbo_flash
|
77
|
+
changelog_uri: https://github.com/rnevius/rails_turbo_flash/blob/main/CHANGELOG.md
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 3.1.0
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.4.22
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Turbo Stream flash messages for Rails
|
97
|
+
test_files: []
|