Last active
November 28, 2016 03:57
-
-
Save kei-p/37589625393cb9e2162d95d88b1270c4 to your computer and use it in GitHub Desktop.
PublicActivity で 通知
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
class Activity < ::PublicActivity::Activity | |
has_many :notifications, dependent: :destroy | |
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
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 |
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
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