Created
August 10, 2014 02:03
-
-
Save aledalgrande/7692ff1c643834d03707 to your computer and use it in GitHub Desktop.
Sidekiq notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/home_controller.rb | |
class HomeController < ApplicationController | |
def index | |
@news = News.get_recent | |
@notifications = current_user.notifications | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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