Skip to content

Instantly share code, notes, and snippets.

@aledalgrande
Created August 10, 2014 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aledalgrande/7692ff1c643834d03707 to your computer and use it in GitHub Desktop.
Save aledalgrande/7692ff1c643834d03707 to your computer and use it in GitHub Desktop.
Sidekiq notification
# app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
@news = News.get_recent
@notifications = current_user.notifications
end
end
# app/models/news.rb
class News < ActiveRecord::Base
belongs_to :channel
after_create :send_creation_notification
def send_creation_notification
NotificationWorker.perform_async(self.id, news.channel.subscribers.map(&:id), NOTIFICATION_TYPES[:creation])
end
end
# workers/notification_worker.rb
class NotificationWorker
include Sidekiq::Worker
def perform(news_id, subscribers, notification_type)
Subscriber.where(id: subscribers).each do |subscriber|
subscriber.notifications.create(news_id: news_id, notification_type: notification_type)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment