Signum
Short description and motivation.
Usage
Send a notification to:
Signum.info(user, text: "Your export is ready") # a user: theirs alone
Signum.info(account, text: "Maintenance tonight") # a group: the users whose pages have it among config.groups
Signum.info(:all, text: "New version") # every page
Signum.error(request.session.id, text: "Not found") # a key: the pages that pass it to the componentAlso success, warning and signal. Each goes to the Turbo stream of whom it is for, [user, :signals],
[account, :signals] or [:signals, key], and a page subscribes only to its own: its user's, every page's, its user's
groups' and the keys it passes:
<%= render Signum::Notifications::Component.new %> <%# signed in %>
<%= render Signum::Notifications::Component.new([request.session.id]) %> <%# nobody signed in yet %>Signum.setup do |config|
config.current_user = -> { Current.user }
config.groups = -> { [Current.account].compact } # none without a user
endOnly the current user's own notification is theirs to show or close (Signum::SignalController).
Before 0.10, every notification was broadcast to one stream, :signals, that every page subscribed to, so each
browser received every user's (and only didn't show them).
Installation
Add this line to your application's Gemfile:
gem 'signum'Add signum to your package.json:
yarn add signum
Add signum to your application.js:
import { Signum } from "@entdec/signum"
// application is a Stimulus application
Signum.start(application)
Use
In controllers, or from background jobs, basically anywhere you want you can use the following methods:
Signum.signal(Current.user, text: "Hello world!")
Signum.info(Current.user, text: "We're still here!")
Signum.error(Current.user, text: "Houston, we have a problem!")
Signum.success(Current.user, text: "The Eagle has landed!")You can make a signal sticky, keeping it on the screen until you click it away:
s = Signum.signal(Current.user, text: "Hello World New", sticky: true, title: "Sticky")You can add icons:
s = Signum.signal(Current.user, text: "Hello World New", title: "Icon", icon:'fa-regular fa-t-rex')Buttons and links:
s = Signum.success(Current.user, text: "Hello World New", title: "Link1", buttons:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}], links:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}, {title: "Amazon", url: "http://www.amazon.com"}])
s = Signum.signal(Current.user, text: "Hello World New", title: "Link2", buttons:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}], links:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}, {title: "Amazon", url: "http://www.amazon.com"}], icon: 'fa-regular fa-t-rex')
s = Signum.signal(Current.user, text: "Hello World New", title: "Link2", buttons:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}], links:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}, {title: "Amazon", url: "http://www.amazon.com"},{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}, {title: "Amazon", url: "http://www.amazon.com"},{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}, {title: "Amazon", url: "http://www.amazon.com"}])You can also show a progressbar, this is based on count and total. Setting the count to total completes the progressbar.
s = Signum.success(Current.user, text: "Hello World New", title: "this is test title", buttons:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}], links:[{title: "Google", url:"http://www.google.com"}, {title: "Apple", url: "http://www.apple.com"}, {title: "Amazon", url: "http://www.amazon.com"}], count: 75, total: 150)You can also attach attachments
attachment = {io: File.open("notes.txt"),
content_type: "text/plain", filename: "notes.txt"}
s = Signum.success(Current.user, text: "Hello World New", title: "this is test title", attachments: [attachment])Signum returns you the signal object. You can update these signals and they will be rebroadcasted again.
Keys
Next to using a User (or any other model) another option is to use keys instead. You could signal all users using the following:
s = Signum.signal(:all, text: "Hello World New", title: "Icon", icon:'fa-regular fa-t-rex')The key all is supported out of the box, any other key needs you to pass it in the Notifications::Component, here we're using the session.id:
= render(Signum::Notifications::Component.new([request.session.id]))Contributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.