改进添加或删除标签的通知逻辑

时间:2014-10-29 18:39:35

标签: ruby-on-rails ruby

我有一个应用程序,当管理员更改其个人资料时,会通过电子邮件通知用户...标签已添加/删除,地址更改等等。这是现有的代码......

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分钟的时间范围内创建一次。然而,逻辑正在逃避我,寻找一些想法,或者以不同的方式来看待它。

任何想法都赞赏。

1 个答案:

答案 0 :(得分: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