2015-09-22 24 views
5

Se scrivo un commento sul vostro stato e poi commentate, non ricevo una notifica per farmi sapere che avete commentato.Come avvisare il commentatore?

Attualmente solo il proprietario dello stato riceve le notifiche. Come possiamo far sapere se anche chi ha commentato uno stato dovrebbe ricevere una notifica?

modelli

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 

controllori

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 

vista

#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 %> 
+3

La tua domanda è troppo generica. _ "ogni volta che un utente commenta su uno stato qualsiasi commento successivo a quello stato ** dovrebbe avvisarti ** che è stato fatto un commento aggiuntivo" _. Che cosa ti serve sapere? Stai chiedendo come sapere _quando_informare, o stai chiedendo di _how_ per avvisare l'utente? Cosa intendi esattamente per _notifying_? Qualche valore modificato in un modello, un'email inviata, un popup ... – brito

+0

Se commento il tuo valore. Quindi se commentate di nuovo quel valore. Non ricevo una notifica per informarmi che hai commentato. Solo la persona di cui ottiene il valore è la notifica. Forse c'è un modo nel 'def create_notification' per rendere questo lavoro @brito –

+0

@ AnthonyGalli.com Sì, perché la notifica viene sempre creata con' user: author, ', e' author = valuation.user' (questo è all'interno di ' create_notification'). Quindi è necessario creare quella specifica notifica passando l'autore del 'Comment' invece dell'autore' Valutazione '. – brito

risposta

4

Invece di creare una notifi per l'autore, è necessario recuperare l'elenco degli utenti che hanno commentato la valutazione. Non vedo uno belongs_to :user nel modello Comment, ma presumo che ce ne sia uno, in quanto è possibile eseguire uno current_user.comments in CommentsController.

Aggiungiamo un rapporto has_many in Valuation per recuperare tutti i commentatori per un dato di valutazione:

class Valuation < ActiveRecord::Base 
    belongs_to :user 
    has_many :notifications 
    has_many :comments 
    has_many :commentators, -> { distinct }, through: :comments, source: :user 
end 

Se si utilizza una versione di Rails < 4, è necessario sostituire -> { distinct } da uniq: true

Poi , puoi semplicemente utilizzare questa nuova relazione per creare notifiche per tutti i commentatori nel modello 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 
Problemi correlati