Skip to content

Instantly share code, notes, and snippets.

@kei-p
Last active November 28, 2016 03:57
Show Gist options
  • Save kei-p/37589625393cb9e2162d95d88b1270c4 to your computer and use it in GitHub Desktop.
Save kei-p/37589625393cb9e2162d95d88b1270c4 to your computer and use it in GitHub Desktop.
PublicActivity で 通知
class Activity < ::PublicActivity::Activity
has_many :notifications, dependent: :destroy
end
module Notifiable
extend ActiveSupport::Concern
include PublicActivity::Model
def self.included(base)
base.has_many :activities, class_name: Activity, as: :trackable
end
def notify_activity(key, owner, users)
activity = create_activity key, owner: owner
users.each do |user|
user.notifications.create(activity: activity)
end
end
end
class Notification < ApplicationRecord
belongs_to :activity
belongs_to :user
scope :unread, -> { where(read: false) }
[
:trackable, :trackable_id, :trackable=
].each { |method| delegate method, to: :activity }
def unread?
!read
end
def render(context, params)
activity.render context, params.merge(locals: { notification: self, n: self })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment