Add notification model

This commit is contained in:
Kevin McConnell
2025-01-09 15:53:31 +00:00
parent c84926692e
commit d0ae5b4bdc
6 changed files with 47 additions and 1 deletions
+2
View File
@@ -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
+7
View File
@@ -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
+2
View File
@@ -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
@@ -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
Generated
+15 -1
View File
@@ -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"
+7
View File
@@ -0,0 +1,7 @@
require "test_helper"
class NotificationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end