From c62e800b08a1939acc6587b1c35e170cd14fd39c Mon Sep 17 00:00:00 2001 From: Kevin McConnell Date: Mon, 13 Jan 2025 11:19:52 +0000 Subject: [PATCH] Notifications are all related to a Bubble --- app/models/bubble.rb | 2 ++ app/models/notification.rb | 1 + app/models/notifier.rb | 2 +- db/migrate/20250109153649_create_notifications.rb | 1 + db/schema.rb | 3 +++ test/fixtures/notifications.yml | 2 ++ 6 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/bubble.rb b/app/models/bubble.rb index 67316354a..5b6a0ec93 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -4,6 +4,8 @@ class Bubble < ApplicationRecord belongs_to :bucket, touch: true belongs_to :creator, class_name: "User", default: -> { Current.user } + has_many :notifications, dependent: :destroy + has_one_attached :image, dependent: :purge_later before_save :set_default_title diff --git a/app/models/notification.rb b/app/models/notification.rb index ecc60eda0..4c47a926b 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,6 +1,7 @@ class Notification < ApplicationRecord belongs_to :user belongs_to :creator, class_name: "User" + belongs_to :bubble belongs_to :resource, polymorphic: true scope :unread, -> { where(read: false) } diff --git a/app/models/notifier.rb b/app/models/notifier.rb index d61dd1c77..eef797d6e 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -9,7 +9,7 @@ class Notifier def generate recipients.map do |recipient| - Notification.create! user: recipient, creator: event.creator, resource: resource, body: body + Notification.create! user: recipient, creator: event.creator, bubble: bubble, resource: resource, body: body end end diff --git a/db/migrate/20250109153649_create_notifications.rb b/db/migrate/20250109153649_create_notifications.rb index 9da2376c5..c2d97b93f 100644 --- a/db/migrate/20250109153649_create_notifications.rb +++ b/db/migrate/20250109153649_create_notifications.rb @@ -3,6 +3,7 @@ class CreateNotifications < ActiveRecord::Migration[8.1] create_table :notifications do |t| t.references :user, null: false, foreign_key: true t.references :creator, null: false, foreign_key: { to_table: :users } + t.references :bubble, null: false, foreign_key: true t.references :resource, null: false, polymorphic: true, index: true t.boolean :read, default: false, null: false t.text :body, null: false diff --git a/db/schema.rb b/db/schema.rb index a267c8e6f..d8a702e39 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -179,12 +179,14 @@ ActiveRecord::Schema[8.1].define(version: 2025_01_09_153649) do create_table "notifications", force: :cascade do |t| t.integer "user_id", null: false t.integer "creator_id", null: false + t.integer "bubble_id", null: false t.string "resource_type", null: false t.integer "resource_id", null: false t.boolean "read", default: false, null: false t.text "body", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.index ["bubble_id"], name: "index_notifications_on_bubble_id" t.index ["creator_id"], name: "index_notifications_on_creator_id" t.index ["resource_type", "resource_id"], name: "index_notifications_on_resource" t.index ["user_id", "read", "created_at"], name: "index_notifications_on_user_id_and_read_and_created_at", order: { read: :desc, created_at: :desc } @@ -259,6 +261,7 @@ ActiveRecord::Schema[8.1].define(version: 2025_01_09_153649) do add_foreign_key "bubbles", "workflow_stages", column: "stage_id" add_foreign_key "events", "event_summaries", column: "summary_id" add_foreign_key "messages", "bubbles" + add_foreign_key "notifications", "bubbles" add_foreign_key "notifications", "users" add_foreign_key "notifications", "users", column: "creator_id" add_foreign_key "pops", "bubbles" diff --git a/test/fixtures/notifications.yml b/test/fixtures/notifications.yml index 223f9d421..554456028 100644 --- a/test/fixtures/notifications.yml +++ b/test/fixtures/notifications.yml @@ -1,6 +1,7 @@ logo_created_kevin: user: kevin creator: david + bubble: logo resource: logo (Bubble) read: false body: "created: The logo isn't big enough" @@ -9,6 +10,7 @@ logo_created_kevin: layout_created_kevin: user: kevin creator: david + bubble: logo resource: layout (Bubble) read: false body: "created: Layout is broken"