From f475cd0a0b25b0557837812ca9b722c88999c7af Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Wed, 20 Jul 2022 21:43:50 +0200 Subject: [PATCH] Make notifications relation polymorphic --- app/models/answer.rb | 2 +- app/models/appendable/reaction.rb | 2 +- app/models/comment.rb | 2 +- app/models/notification/comment_smiled.rb | 2 ++ app/models/notification/commented.rb | 2 ++ app/models/notification/question_answered.rb | 2 ++ app/models/notification/smiled.rb | 2 ++ app/models/notification/started_following.rb | 2 ++ app/models/notifications/comment_smiled.rb | 2 -- app/models/notifications/commented.rb | 2 -- app/models/notifications/question_answered.rb | 2 -- app/models/notifications/smiled.rb | 2 -- app/models/notifications/started_following.rb | 2 -- app/models/relationships/follow.rb | 2 +- ...0220720190421_add_type_to_notifications.rb | 22 +++++++++++++++++++ db/schema.rb | 3 ++- 16 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 app/models/notification/comment_smiled.rb create mode 100644 app/models/notification/commented.rb create mode 100644 app/models/notification/question_answered.rb create mode 100644 app/models/notification/smiled.rb create mode 100644 app/models/notification/started_following.rb delete mode 100644 app/models/notifications/comment_smiled.rb delete mode 100644 app/models/notifications/commented.rb delete mode 100644 app/models/notifications/question_answered.rb delete mode 100644 app/models/notifications/smiled.rb delete mode 100644 app/models/notifications/started_following.rb create mode 100644 db/migrate/20220720190421_add_type_to_notifications.rb diff --git a/app/models/answer.rb b/app/models/answer.rb index 4af3b5ee..ddd5fe98 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -44,6 +44,6 @@ class Answer < ApplicationRecord # rubocop:enable Rails/SkipsModelValidations def notification_type(*_args) - Notifications::QuestionAnswered + Notification::QuestionAnswered end end diff --git a/app/models/appendable/reaction.rb b/app/models/appendable/reaction.rb index 97de8963..1d99de49 100644 --- a/app/models/appendable/reaction.rb +++ b/app/models/appendable/reaction.rb @@ -16,6 +16,6 @@ class Appendable::Reaction < Appendable # rubocop:enable Rails/SkipsModelValidations def notification_type(*_args) - Notifications::Smiled + Notification::Smiled end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 4c3dcc7d..127524ef 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -31,6 +31,6 @@ class Comment < ApplicationRecord # rubocop:enable Rails/SkipsModelValidations def notification_type(*_args) - Notifications::Commented + Notification::Commented end end diff --git a/app/models/notification/comment_smiled.rb b/app/models/notification/comment_smiled.rb new file mode 100644 index 00000000..4f99d1c7 --- /dev/null +++ b/app/models/notification/comment_smiled.rb @@ -0,0 +1,2 @@ +class Notification::CommentSmiled < Notification +end diff --git a/app/models/notification/commented.rb b/app/models/notification/commented.rb new file mode 100644 index 00000000..167f6cb4 --- /dev/null +++ b/app/models/notification/commented.rb @@ -0,0 +1,2 @@ +class Notification::Commented < Notification +end \ No newline at end of file diff --git a/app/models/notification/question_answered.rb b/app/models/notification/question_answered.rb new file mode 100644 index 00000000..a4189427 --- /dev/null +++ b/app/models/notification/question_answered.rb @@ -0,0 +1,2 @@ +class Notification::QuestionAnswered < Notification +end \ No newline at end of file diff --git a/app/models/notification/smiled.rb b/app/models/notification/smiled.rb new file mode 100644 index 00000000..7cb99b3c --- /dev/null +++ b/app/models/notification/smiled.rb @@ -0,0 +1,2 @@ +class Notification::Smiled < Notification +end \ No newline at end of file diff --git a/app/models/notification/started_following.rb b/app/models/notification/started_following.rb new file mode 100644 index 00000000..9d57696f --- /dev/null +++ b/app/models/notification/started_following.rb @@ -0,0 +1,2 @@ +class Notification::StartedFollowing < Notification +end \ No newline at end of file diff --git a/app/models/notifications/comment_smiled.rb b/app/models/notifications/comment_smiled.rb deleted file mode 100644 index 7b013c46..00000000 --- a/app/models/notifications/comment_smiled.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Notifications::CommentSmiled < Notification -end diff --git a/app/models/notifications/commented.rb b/app/models/notifications/commented.rb deleted file mode 100644 index 7eacfeb5..00000000 --- a/app/models/notifications/commented.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Notifications::Commented < Notification -end \ No newline at end of file diff --git a/app/models/notifications/question_answered.rb b/app/models/notifications/question_answered.rb deleted file mode 100644 index f93ffd45..00000000 --- a/app/models/notifications/question_answered.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Notifications::QuestionAnswered < Notification -end \ No newline at end of file diff --git a/app/models/notifications/smiled.rb b/app/models/notifications/smiled.rb deleted file mode 100644 index 58ab289e..00000000 --- a/app/models/notifications/smiled.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Notifications::Smiled < Notification -end \ No newline at end of file diff --git a/app/models/notifications/started_following.rb b/app/models/notifications/started_following.rb deleted file mode 100644 index 505866ff..00000000 --- a/app/models/notifications/started_following.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Notifications::StartedFollowing < Notification -end \ No newline at end of file diff --git a/app/models/relationships/follow.rb b/app/models/relationships/follow.rb index 41d8fd0d..30070831 100644 --- a/app/models/relationships/follow.rb +++ b/app/models/relationships/follow.rb @@ -10,6 +10,6 @@ class Relationships::Follow < Relationship end def notification_type(*_args) - Notifications::StartedFollowing + Notification::StartedFollowing end end diff --git a/db/migrate/20220720190421_add_type_to_notifications.rb b/db/migrate/20220720190421_add_type_to_notifications.rb new file mode 100644 index 00000000..408bc266 --- /dev/null +++ b/db/migrate/20220720190421_add_type_to_notifications.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ceacd302..d5d91c56 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.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 enable_extension "plpgsql" @@ -116,6 +116,7 @@ ActiveRecord::Schema.define(version: 2022_07_18_214748) do t.boolean "new" t.datetime "created_at" t.datetime "updated_at" + t.string "type" t.index ["new"], name: "index_notifications_on_new" t.index ["recipient_id"], name: "index_notifications_on_recipient_id" end