Make notifications relation polymorphic

This commit is contained in:
Karina Kwiatek 2022-07-20 21:43:50 +02:00 committed by Karina Kwiatek
parent d33e0f86af
commit f475cd0a0b
16 changed files with 38 additions and 15 deletions

View File

@ -44,6 +44,6 @@ class Answer < ApplicationRecord
# rubocop:enable Rails/SkipsModelValidations # rubocop:enable Rails/SkipsModelValidations
def notification_type(*_args) def notification_type(*_args)
Notifications::QuestionAnswered Notification::QuestionAnswered
end end
end end

View File

@ -16,6 +16,6 @@ class Appendable::Reaction < Appendable
# rubocop:enable Rails/SkipsModelValidations # rubocop:enable Rails/SkipsModelValidations
def notification_type(*_args) def notification_type(*_args)
Notifications::Smiled Notification::Smiled
end end
end end

View File

@ -31,6 +31,6 @@ class Comment < ApplicationRecord
# rubocop:enable Rails/SkipsModelValidations # rubocop:enable Rails/SkipsModelValidations
def notification_type(*_args) def notification_type(*_args)
Notifications::Commented Notification::Commented
end end
end end

View File

@ -0,0 +1,2 @@
class Notification::CommentSmiled < Notification
end

View File

@ -0,0 +1,2 @@
class Notification::Commented < Notification
end

View File

@ -0,0 +1,2 @@
class Notification::QuestionAnswered < Notification
end

View File

@ -0,0 +1,2 @@
class Notification::Smiled < Notification
end

View File

@ -0,0 +1,2 @@
class Notification::StartedFollowing < Notification
end

View File

@ -1,2 +0,0 @@
class Notifications::CommentSmiled < Notification
end

View File

@ -1,2 +0,0 @@
class Notifications::Commented < Notification
end

View File

@ -1,2 +0,0 @@
class Notifications::QuestionAnswered < Notification
end

View File

@ -1,2 +0,0 @@
class Notifications::Smiled < Notification
end

View File

@ -1,2 +0,0 @@
class Notifications::StartedFollowing < Notification
end

View File

@ -10,6 +10,6 @@ class Relationships::Follow < Relationship
end end
def notification_type(*_args) def notification_type(*_args)
Notifications::StartedFollowing Notification::StartedFollowing
end end
end end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class AddTypeToNotifications < ActiveRecord::Migration[6.1]
def up
add_column :notifications, :type, :string
execute "UPDATE notifications SET type = 'Notification::Commented' WHERE target_type = 'Comment'"
execute "UPDATE notifications SET type = 'Notification::QuestionAnswered' WHERE target_type = 'Answer'"
execute "UPDATE notifications SET type = 'Notification::StartedFollowing' WHERE target_type = 'Relationship'"
execute <<~SQUIRREL
UPDATE notifications
SET type = 'Notification::Smiled'
WHERE target_type = 'Appendable'
AND target_id IN (SELECT id FROM appendables WHERE type = 'Appendable::Reaction' AND parent_type = 'Answer');
SQUIRREL
execute "UPDATE notifications SET type = 'Notification::CommentSmiled' WHERE type IS NULL"
end
def down
remove_column :notifications, :type
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2022_07_18_214748) do ActiveRecord::Schema.define(version: 2022_07_20_190421) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -116,6 +116,7 @@ ActiveRecord::Schema.define(version: 2022_07_18_214748) do
t.boolean "new" t.boolean "new"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "type"
t.index ["new"], name: "index_notifications_on_new" t.index ["new"], name: "index_notifications_on_new"
t.index ["recipient_id"], name: "index_notifications_on_recipient_id" t.index ["recipient_id"], name: "index_notifications_on_recipient_id"
end end