2014-10-29 71 views
0

我有,用户通过电子邮件时,管理员改变他们的个人资料通知应用程序...标签添加/删除,更改地址...等。下面是现有的代码...提高通知逻辑添加或删除标签

def event_notification 
    if self.creator != self.user 
     case self.event_code 
     when ADDED_TAGS, REMOVED_TAGS 
     Notification.create(notifiable: self, created_by: self.created_by, recipient_id: self.user_id, delivery_time: 1.minute.from_now) 
     when ADDRESS_CHANGED 
     ... 

我们遇到的问题是当有人修改了用户的标签,他们通常会增加,并在一段较短的时间内删除多个标签。为每个添加或删除的标签生成通知(电子邮件)。我想修改这个event_notification方法,所以如果在给定的时间范围内(比如5分钟)添加/删除标签,那么只会创建一个通知。

我想我可以把一些种类标志的ADDED_TAGS内,REMOVED_TAGS该通知仅在5分钟的时间内创造了一次情况。然而,逻辑逃避了我,寻找一些想法,或者以不同的方式来看待这个。

任何想法赞赏。

回答

0
def create_user_event_notification 
    if self.creator != self.user 
     case self.event_code 
     when ADDED_TAGS, REMOVED_TAGS 
     last_notification = Notification.where(recipient_id: self.user_id, notification_type: notification_type).last 
     unless last_notification.nil? 
      if Time.now - last_notification.created_at > 300 #5 minutes 
      Notification.create(notifiable: self, created_by: self.created_by, recipient_id: self.user_id, 
          notification_type: Notification::TAG_EDIT, delivery_time: 1.minute.from_now) 
      end 
     end 
     when CHANGED_VACATION_STATUS 
     Notification.create(notifiable: self, created_by: self.created_by, recipient_id: self.user_id, 
          notification_type: Notification::VACATION_EDIT, delivery_time: 1.minute.from_now) 
     when ADDED_LOCATION, REMOVED_LOCATION, ADDED_COUNTY, REMOVED_COUNTY 
     Notification.create(notifiable: self, created_by: self.created_by, recipient_id: self.user_id, 
          notification_type: Notification::LOCATION_EDIT, delivery_time: 1.minute.from_now) 
     end 
    end 
    end