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
require "shellwords" require "open3" module Backup
Building safe shell commands in Ruby
shell-out
subprocess
command-injection
Intermediate
7 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
ruby
class LogAggregator BUCKET_FORMAT = "%Y-%m-%dT%H:%M" def initialize(entries)
Bucketing log entries by the minute in Ruby
aggregation
hashing
enumerable
Intermediate
5 steps
ruby
class WeeklySignupsReport DEFAULT_WEEKS = 12 def initialize(weeks: DEFAULT_WEEKS, source: User.all)
Building a weekly signups report in Rails
service object
aggregation
group by
Intermediate
7 steps
ruby
class ApplicationController < ActionController::Base EXPERIMENTS = { checkout_button_color: %w[control blue green], onboarding_flow: %w[control streamlined]
How A/B test cohorts are assigned in Rails
a-b-testing
cookies
hashing
Intermediate
8 steps
ruby
class SnippetHighlighter CONTEXT_RADIUS = 60 MAX_TERMS = 8
Building search-result snippets in Ruby
regular-expressions
text-processing
search
Intermediate
9 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.