Class: Notifications::DeletePreviousNotifications
- Inherits:
-
ConsolidationPlan
- Object
- ConsolidationPlan
- Notifications::DeletePreviousNotifications
- Defined in:
- app/services/notifications/delete_previous_notifications.rb
Instance Method Summary collapse
- #can_consolidate_data?(notification) ⇒ Boolean
- #consolidate_or_save!(notification) ⇒ Object
-
#initialize(type:, previous_query_blk: nil) ⇒ DeletePreviousNotifications
constructor
A new instance of DeletePreviousNotifications.
Methods inherited from ConsolidationPlan
#set_mutations, #set_precondition
Constructor Details
#initialize(type:, previous_query_blk: nil) ⇒ DeletePreviousNotifications
Returns a new instance of DeletePreviousNotifications.
20 21 22 23 |
# File 'app/services/notifications/delete_previous_notifications.rb', line 20 def initialize(type:, previous_query_blk: nil) @type = type @previous_query_blk = previous_query_blk end |
Instance Method Details
#can_consolidate_data?(notification) ⇒ Boolean
25 26 27 28 29 30 31 |
# File 'app/services/notifications/delete_previous_notifications.rb', line 25 def can_consolidate_data?(notification) return false if notification.notification_type != type @data = consolidated_data(notification) precondition_blk.nil? || precondition_blk.call(@data, notification) end |
#consolidate_or_save!(notification) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/services/notifications/delete_previous_notifications.rb', line 33 def consolidate_or_save!(notification) @data ||= consolidated_data(notification) return unless can_consolidate_data?(notification) notifications = user_notifications(notification, type) notifications = previous_query_blk.call(notifications, data) if previous_query_blk.present? notification.data = data.to_json Notification.transaction do notifications.destroy_all notification.save! end notification end |