如何通知评论者?

Ant*_*com 5 ruby notifications ruby-on-rails

如果我对您的状态发表评论然后您发表评论,我会收到通知,让我知道您的评论.

目前只有状态的所有者才会收到通知.我们如何才能获得对状态发表评论的人也应该得到通知?

楷模

class Comment < ActiveRecord::Base
  after_save :create_notification
  has_many :notifications
  def create_notification
    author = valuation.user
    notifications.create(
      comment:      self,
      valuation:    valuation,
      user:         author,      
      read:         false
    )
  end
end

class Notification < ActiveRecord::Base
  belongs_to :comment
  belongs_to :valuation
  belongs_to :user
end

class Valuation < ActiveRecord::Base
  belongs_to :user
  has_many :notifications
  has_many :comments
end
Run Code Online (Sandbox Code Playgroud)

控制器

class CommentsController < ApplicationController
  before_action :set_commentable, only: [:index, :new, :create]
  before_action :set_comment, only: [:edit, :update, :destroy]

  def create
    @comment = @commentable.comments.new(comment_params)
    if @comment.save
      redirect_to @commentable, notice: "Comment created."
    else
      render :new
    end
  end

  private

  def set_commentable
    @commentable = find_commentable
  end

  def set_comment
    @comment = current_user.comments.find(params[:id])
  end

  def find_commentable
    if params[:goal_id]
      Goal.find(params[:goal_id])
    elsif params[:habit_id]
      Habit.find(params[:habit_id])
    elsif params[:valuation_id]
      Valuation.find(params[:valuation_id])
    elsif params[:stat_id]
      Stat.find(params[:stat_id])
    end
  end
end

class NotificationsController < ApplicationController
  def index
    @notifications = current_user.notifications
    @notifications.each do |notification|
      notification.update_attribute(:read, true) 
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

意见

#notifications/_notification.html.erb
commented on <%= link_to "your value", notification_valuation_path(notification, notification.valuation_id) %>

#comments/_form.html.erb
<%= form_for [@commentable, @comment] do |f| %>
  <%= f.text_area :content %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

har*_*ith 4

您不必为作者创建一条通知,而是必须检索对评估发表评论的用户列表。我在 Comment 模型中没有看到 a belongs_to :user,但我假设有一个,因为您可以current_user.comments在 中执行 a CommentsController

让我们添加一个has_many关系来Valuation检索给定估值的所有评论员:

class Valuation < ActiveRecord::Base
  belongs_to :user
  has_many :notifications
  has_many :comments
  has_many :commentators, -> { distinct }, through: :comments, source: :user
end
Run Code Online (Sandbox Code Playgroud)

如果您使用的 Rails < 4 版本,您应该替换-> { distinct }uniq: true

然后,您可以简单地使用这个新关系为模型中的所有评论者创建通知Comment

class Comment < ActiveRecord::Base
  after_save :create_notification
  belongs_to :user
  belongs_to :valuation
  has_many :notifications

  def create_notification
    to_notify = [valuation.user] + valuation.commentators
    to_notify = to_notify.uniq
    to_notify.delete(user)  # Do not send notification to current user
    to_notify.each do |notification_user|
      notifications.create(
        comment:      self,
        valuation:    valuation,
        user:         notification_user,      
        read:         false
      )
    end
  end
end
Run Code Online (Sandbox Code Playgroud)