2014-10-29 69 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