ruby
31 lines · 6 steps
Multi-channel notifications with Noticed in Rails
A single notifier class fans one comment event out to the database, email, and a live WebSocket channel.
Explained by
highlit
1class NewCommentNotifier < ApplicationNotifier
2 deliver_by :database
3
4 deliver_by :email do |config|
5 config.mailer = "CommentMailer"
6 config.method = :new_comment
7 config.if = -> { recipient.email_notifications? }
8 end
9
10 deliver_by :action_cable do |config|
11 config.channel = "NotificationsChannel"
12 config.stream = -> { recipient }
13 config.message = -> { { unread_count: recipient.notifications.unread.count } }
14 end
15
16 notification_methods do
17 def message
18 t(".message", commenter: comment.author.name, post: comment.post.title)
19 end
20
21 def url
22 post_path(comment.post, anchor: "comment-#{comment.id}")
23 end
24 end
25
26 required_param :comment
27
28 def comment
29 params[:comment]
30 end
31end
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Declaring multiple deliver_by methods lets one event reach several channels without duplicating logic.
- 2Lambdas evaluated in the notifier context let per-delivery config react to the recipient and payload.
- 3Centralizing message and url in the notifier keeps every channel's copy consistent.
Related explainers
ruby
class Order class InvalidTransition < StandardError; end TRANSITIONS = {
A state machine for order transitions in Ruby
state-machine
data-driven
error-handling
Intermediate
8 steps
ruby
class CreateOrderItems < ActiveRecord::Migration[7.1] def change create_table :order_items do |t| t.references :order, null: false, foreign_key: { on_delete: :cascade }
Enforcing order-item integrity in Rails
migrations
foreign-keys
validations
Intermediate
7 steps
ruby
class Registration < ApplicationRecord belongs_to :event validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
Validating registrations in Rails
validations
i18n
error-handling
Intermediate
8 steps
ruby
class ApacheLogParser LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/ TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"
Parsing Apache logs with named captures
regex
named-captures
parsing
Intermediate
6 steps
ruby
require "csv" def parse_transaction_row(line) fields = CSV.parse_line(line, headers: false, skip_blanks: true)
Parsing one CSV transaction row in Ruby
parsing
type-coercion
error-handling
Intermediate
6 steps
ruby
class DocumentsController < ApplicationController before_action :authenticate_user! before_action :set_document
Serving secure file downloads in Rails
authorization
signed-urls
active-storage
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/multi-channel-notifications-with-noticed-in-rails-explained-ruby-3975/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.