Make notification target polymorphic

This commit is contained in:
Kevin McConnell
2025-01-10 10:12:47 +00:00
parent 952895ae67
commit af5ac65116
6 changed files with 29 additions and 9 deletions
+1 -1
View File
@@ -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
+2 -1
View File
@@ -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) }
+1 -1
View File
@@ -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
@@ -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
Generated
+7 -4
View File
@@ -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"
+15
View File
@@ -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 %>