diff --git a/app/models/bubble.rb b/app/models/bubble.rb index a731d7726..1e617189d 100644 --- a/app/models/bubble.rb +++ b/app/models/bubble.rb @@ -4,7 +4,7 @@ class Bubble < ApplicationRecord belongs_to :bucket, touch: true belongs_to :creator, class_name: "User", default: -> { Current.user } - has_many :notifications, dependent: :destroy + has_many :notifications, as: :resource, dependent: :destroy has_one_attached :image, dependent: :purge_later diff --git a/app/models/notification.rb b/app/models/notification.rb index 5553dfad9..20dea8f11 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -1,6 +1,7 @@ class Notification < ApplicationRecord belongs_to :user - belongs_to :bubble + belongs_to :creator, class_name: 'User' + belongs_to :resource, polymorphic: true scope :unread, -> { where(read: false) } scope :ordered, -> { order(read: :desc, created_at: :desc) } diff --git a/app/models/notifier.rb b/app/models/notifier.rb index be58c79c9..fb41e96f8 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, bubble: bubble, body: body + Notification.create! user: recipient, creator: event.creator, resource: bubble, body: body end end diff --git a/db/migrate/20250109153649_create_notifications.rb b/db/migrate/20250109153649_create_notifications.rb index 834d75b16..9da2376c5 100644 --- a/db/migrate/20250109153649_create_notifications.rb +++ b/db/migrate/20250109153649_create_notifications.rb @@ -2,13 +2,14 @@ class CreateNotifications < ActiveRecord::Migration[8.1] def change create_table :notifications do |t| t.references :user, null: false, foreign_key: true - t.references :bubble, null: false, foreign_key: true + t.references :creator, null: false, foreign_key: { to_table: :users } + t.references :resource, null: false, polymorphic: true, index: true t.boolean :read, default: false, null: false t.text :body, null: false t.timestamps - t.index %i[ user_id read created_at ], order: { created_at: :desc } + t.index %i[ user_id read created_at ], order: { read: :desc, created_at: :desc } end end end diff --git a/db/schema.rb b/db/schema.rb index da324c7fc..a267c8e6f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -178,13 +178,16 @@ 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 "bubble_id", null: false + t.integer "creator_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 ["user_id", "read", "created_at"], name: "index_notifications_on_user_id_and_read_and_created_at", order: { created_at: :desc } + 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 } t.index ["user_id"], name: "index_notifications_on_user_id" end @@ -256,8 +259,8 @@ 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" add_foreign_key "pops", "users" add_foreign_key "sessions", "users" diff --git a/test/fixtures/notifications.yml b/test/fixtures/notifications.yml new file mode 100644 index 000000000..57a902def --- /dev/null +++ b/test/fixtures/notifications.yml @@ -0,0 +1,15 @@ +logo_created_kevin: + user: kevin + creator: david + resource: logo (Bubble) + read: false + body: "David created: The logo isn't big enough" + created_at: <%= 1.week.ago %> + +layout_created_kevin: + user: kevin + creator: david + resource: layout (Bubble) + read: false + body: "David created: Layout is broken" + created_at: <%= 1.week.ago %>