2016-08-06 1 views
0

내 앱에 댓글이있는 게시물이 있습니다. 이것은 내가 원했던 기능입니다. 사용자가 자신이 작성한 게시물에 대한 주석 활동을보고 주석 작성자가 주석 처리 한 게시물에 대한 주석 활동을 봅니다.레일즈 4의 퍼블릭 액티비티 젬을 사용하여 게시물의 코멘트 스레드에 관련된 모든 사용자에게 어떻게 알릴 수 있습니까?

내 모델 :

class Post < ActiveRecord::Base 
belongs_to :user 
belongs_to :course 
has_many :comments, dependent: :destroy 
end 


class Comment < ActiveRecord::Base 
include PublicActivity::Model 
tracked except: :update, owner: ->(controller, model) { controller && controller.current_user } 

belongs_to :post 
belongs_to :user 

end 

그리고 활동 컨트롤러 :

class ActivitiesController < ApplicationController 
def index 
@activities = PublicActivity::Activity.order("created_at desc") 

end 
end 

그리고 활동 인덱스보기 :

<% @activities.each do |activity| %> 
<div class="activity"> 
<%= link_to activity.owner.username, activity.owner if activity.owner %> 

added comment to <%= link_to activity.trackable.post.title, activity.trackable.post %> 
</div> 
<% end %> 

감사합니다!

답변

1

PublicActivity gem이이를 수행하도록 설계되지 않았습니다. 하나의 활동이 발생할 때 하나의 활동을 작성하기위한 것입니다. 귀하의 경우, 활동이 발생하면 가능한 많은 통지 기록 (1/사용자)을 작성해야합니다. 필자는 작업에서 동일한 문제를 겪었고 PublicActivity 구현과 유사한 Notification 모델을 만들기로 결정했습니다. key: "comment.created"

와 함께 덧글에 대한 많은 key: "post.commented"

  • 와 소유자에 대한

    • 하나

      class User < ActiveRecord::Base 
          has_many :notifications, foreign_key: :notified_user_id, dependent: :destroy 
      end 
      
      class Comment < ActiveRecord::Base 
          # maybe it would be better to name it as inverse_notifications 
          has_many :notifications, as: :trackable, dependent: :destroy 
      end 
      
      class Notification < ActiveRecord::Base 
          belongs_to :trackable, polymorphic: true 
          belongs_to :acting_user, class_name: "User" 
          belongs_to :notified_user, class_name: "User" 
      
          validates_presence_of :trackable, :notified_user, :key 
      
          scope :unread, -> { where("read_at IS NULL") } 
      end 
      

      이 주석이 작성 될 때 통지의 2 개 종류를 만들 수 있습니다 사용자가 본 경우 read_at 속성을 설정할 수 있으므로 프런트 엔드에서 다른 스타일을 추가 할 수 있습니다.

  • 관련 문제