diff --git a/app/models/bubble.rb b/app/models/bubble.rb index 5da059a49..a731d7726 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 new file mode 100644 index 000000000..56d2b8ffd --- /dev/null +++ b/app/models/notification.rb @@ -0,0 +1,7 @@ +class Notification < ApplicationRecord + belongs_to :user + belongs_to :bubble + + scope :unread, -> { where.not(:read) } + scope :ordered, -> { order(created_at: :desc) } +end diff --git a/app/models/user.rb b/app/models/user.rb index 907881cc5..cceadb922 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,8 @@ class User < ApplicationRecord has_many :assignings, foreign_key: :assigner_id, class_name: "Assignment" has_many :assigned_bubbles, through: :assignments, source: :bubble + has_many :notifications, dependent: :destroy + has_one_attached :avatar validates_presence_of :email_address diff --git a/db/migrate/20250109153649_create_notifications.rb b/db/migrate/20250109153649_create_notifications.rb new file mode 100644 index 000000000..834d75b16 --- /dev/null +++ b/db/migrate/20250109153649_create_notifications.rb @@ -0,0 +1,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.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 } + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 1d008479c..da324c7fc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2024_12_09_223551) do +ActiveRecord::Schema[8.1].define(version: 2025_01_09_153649) do create_table "accesses", force: :cascade do |t| t.integer "bucket_id", null: false t.integer "user_id", null: false @@ -176,6 +176,18 @@ ActiveRecord::Schema[8.1].define(version: 2024_12_09_223551) do t.index ["messageable_type", "messageable_id"], name: "index_messages_on_messageable", unique: true end + create_table "notifications", force: :cascade do |t| + t.integer "user_id", null: false + t.integer "bubble_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 ["user_id"], name: "index_notifications_on_user_id" + end + create_table "pops", force: :cascade do |t| t.integer "bubble_id", null: false t.integer "user_id" @@ -244,6 +256,8 @@ ActiveRecord::Schema[8.1].define(version: 2024_12_09_223551) 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 "pops", "bubbles" add_foreign_key "pops", "users" add_foreign_key "sessions", "users" diff --git a/test/models/notification_test.rb b/test/models/notification_test.rb new file mode 100644 index 000000000..a76e08d66 --- /dev/null +++ b/test/models/notification_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class NotificationTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end