Don't expand bundles when adding cards. Leave the aggregation time fixed for now
This commit is contained in:
@@ -6,6 +6,7 @@ class Notification::Bundle < ApplicationRecord
|
||||
before_create :set_default_window
|
||||
|
||||
scope :due, -> { pending.where("ends_at <= ?", Time.current) }
|
||||
scope :containing, -> (notification) { where("starts_at <= ? AND ends_at >= ?", notification.created_at, notification.created_at) }
|
||||
|
||||
class << self
|
||||
def deliver_all
|
||||
@@ -19,10 +20,6 @@ class Notification::Bundle < ApplicationRecord
|
||||
end
|
||||
end
|
||||
|
||||
def add(notification)
|
||||
update! ends_at: [ ends_at, notification.created_at ].max
|
||||
end
|
||||
|
||||
def notifications
|
||||
user.notifications.where(created_at: window)
|
||||
end
|
||||
|
||||
@@ -8,12 +8,20 @@ module User::Notifiable
|
||||
|
||||
def bundle(notification)
|
||||
transaction do
|
||||
find_or_create_pending_bundle_for(notification).add(notification)
|
||||
find_or_create_bundle_for(notification)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def find_or_create_pending_bundle_for(notification)
|
||||
notification_bundles.pending.last || notification_bundles.create!(starts_at: notification.created_at)
|
||||
def find_or_create_bundle_for(notification)
|
||||
find_bundle_for(notification) || create_bundle_for(notification)
|
||||
end
|
||||
|
||||
def find_bundle_for(notification)
|
||||
notification_bundles.pending.containing(notification).last
|
||||
end
|
||||
|
||||
def create_bundle_for(notification)
|
||||
notification_bundles.create!(starts_at: notification.created_at)
|
||||
end
|
||||
end
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
class RemoveUniqueConstraintFromNotificationBundlesUserId < ActiveRecord::Migration[8.1]
|
||||
def change
|
||||
remove_index :notification_bundles, :user_id, unique: true
|
||||
add_index :notification_bundles, %i[ ends_at status ]
|
||||
end
|
||||
end
|
||||
Generated
+2
-2
@@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.1].define(version: 2025_08_26_084559) do
|
||||
ActiveRecord::Schema[8.1].define(version: 2025_08_26_131458) do
|
||||
create_table "accesses", force: :cascade do |t|
|
||||
t.datetime "accessed_at"
|
||||
t.integer "collection_id", null: false
|
||||
@@ -302,9 +302,9 @@ ActiveRecord::Schema[8.1].define(version: 2025_08_26_084559) do
|
||||
t.integer "status", default: 0, null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "user_id", null: false
|
||||
t.index ["ends_at", "status"], name: "index_notification_bundles_on_ends_at_and_status"
|
||||
t.index ["user_id", "starts_at", "ends_at"], name: "idx_on_user_id_starts_at_ends_at_7eae5d3ac5"
|
||||
t.index ["user_id", "status"], name: "index_notification_bundles_on_user_id_and_status"
|
||||
t.index ["user_id"], name: "index_notification_bundles_on_user_id", unique: true
|
||||
end
|
||||
|
||||
create_table "notifications", force: :cascade do |t|
|
||||
|
||||
+5
-4
@@ -2352,10 +2352,11 @@ indexes:
|
||||
valid: true
|
||||
- !ruby/object:ActiveRecord::ConnectionAdapters::IndexDefinition
|
||||
table: notification_bundles
|
||||
name: index_notification_bundles_on_user_id
|
||||
unique: true
|
||||
name: index_notification_bundles_on_ends_at_and_status
|
||||
unique: false
|
||||
columns:
|
||||
- user_id
|
||||
- ends_at
|
||||
- status
|
||||
lengths: {}
|
||||
orders: {}
|
||||
opclasses: {}
|
||||
@@ -2860,4 +2861,4 @@ indexes:
|
||||
comment:
|
||||
valid: true
|
||||
workflows: []
|
||||
version: 20250826084559
|
||||
version: 20250826131458
|
||||
|
||||
Reference in New Issue
Block a user