2015-09-22 8 views
5

अगर मैं आपकी स्थिति पर कोई टिप्पणी लिखता हूं और फिर आप टिप्पणी करते हैं तो मुझे आपको सूचित करने के लिए अधिसूचना नहीं मिलती है कि आपने टिप्पणी की है।टिप्पणीकर्ता को कैसे सूचित करें?

वर्तमान में केवल स्थिति के मालिक को सूचनाएं मिलती हैं। हम कैसे बना सकते हैं, जहां किसी भी स्थिति पर टिप्पणी करने वाले किसी भी व्यक्ति को अधिसूचित किया जाना चाहिए?

मॉडल

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 

नियंत्रकों

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 

विचारों

#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

आपका प्रश्न बहुत सामान्य है। _ "जब भी कोई उपयोगकर्ता किसी स्थिति पर टिप्पणी करता है तो उस स्थिति के बाद आने वाली किसी भी टिप्पणी ** आपको सूचित करनी चाहिए ** कि एक अतिरिक्त टिप्पणी की गई है" _। आपको क्या जानने की जरूरत है? क्या आप सूचित करने के लिए _when_ को कैसे जानना चाहते हैं, या आप उपयोगकर्ता को सूचित करने के लिए _how_ के बारे में पूछ रहे हैं? _notifying_ द्वारा आपका क्या मतलब है? मॉडल में कुछ मूल्य बदल दिए गए, एक ईमेल भेजा गया, एक पॉपअप ... – brito

+0

यदि मैं आपके मूल्य पर टिप्पणी करता हूं। फिर यदि आप उस मूल्य पर वापस टिप्पणी करते हैं। मुझे एक अधिसूचना नहीं मिली है जो आपको बताती है कि आपने टिप्पणी की है। केवल वह व्यक्ति जिसका मूल्य इसे अधिसूचना प्राप्त करता है। हो सकता है कि इस काम को बनाने के लिए 'def create_notification' में कोई तरीका है @brito –

+0

@ AnthonyGalli.com हां, क्योंकि अधिसूचना हमेशा उपयोगकर्ता: लेखक,', और 'लेखक = वैल्यूएशन.यूसर' के साथ बनाई जाती है (यह ' create_notification')। तो आपको 'वैल्यूएशन' लेखक की बजाय 'टिप्पणी' के लेखक को पारित करने वाली उस विशिष्ट अधिसूचना को बनाने की आवश्यकता है। – brito

उत्तर

4

के बजाय एक notifi बनाने लेखक के लिए cation, आपको मूल्यांकन की टिप्पणी करने वाले उपयोगकर्ताओं की सूची पुनर्प्राप्त करना होगा। मुझे टिप्पणी मॉडल में belongs_to :user नहीं दिखाई देता है, लेकिन मुझे लगता है कि एक है, क्योंकि आप CommentsController में current_user.comments करने में सक्षम हैं।

के किसी दिए गए मूल्यांकन के लिए सभी टिप्पणीकारों को पुनः प्राप्त करने Valuation में एक has_many संबंध जोड़ते हैं:

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

आप रेल < 4 के एक संस्करण का उपयोग कर रहे हैं, तो आप द्वारा uniq: true

फिर -> { distinct } बदलना चाहिए , आप 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 
संबंधित मुद्दे