Ruby on rails 改进用于添加或删除标记的通知逻辑

Ruby on rails 改进用于添加或删除标记的通知逻辑,ruby-on-rails,ruby,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_

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

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
      ...
我们面临的问题是,当有人修改用户标签时,他们通常会在相对较短的时间内添加和删除多个标签。为每个添加或删除的标记生成通知(电子邮件)。我想修改这个事件通知方法,这样如果在给定的时间范围内(比如5分钟)添加/删除标记,那么只创建一个通知

我想我可以在添加的标签、删除的标签中加上某种标志,通知在5分钟内只创建一次。然而,我无法理解这种逻辑,而是在寻找一些想法,或者以不同的方式来看待这个问题

任何想法都值得赞赏

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