From 1f9ef73aff52a3ab62dd74fc61b15ecfadda4e3f Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Thu, 15 Feb 2024 01:56:21 -0300 Subject: [PATCH 1/5] refactor/cria notification_decorator -passa responsabilidade do redirect para NotificationDecorator --- app/controllers/notifications_controller.rb | 38 ++------------------- app/models/notification_decorator.rb | 14 ++++++++ 2 files changed, 17 insertions(+), 35 deletions(-) create mode 100644 app/models/notification_decorator.rb diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 5b500fc..c44b721 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -7,40 +7,8 @@ def index end def update - @notification = Notification.find(params[:id]) - @notification.clicked! - redirect_to_notification - end - - private - - def redirect_to_notification - return redirect_to_invitation if @notification.notifiable.is_a? Invitation - return redirect_to_comment if @notification.notifiable.is_a? Comment - return redirect_to_connection if @notification.notifiable.is_a? Connection - return redirect_to_post if @notification.notifiable.is_a? Post - - redirect_to_like if @notification.notifiable.is_a? Like - end - - def redirect_to_invitation - redirect_to invitation_path(@notification.notifiable) - end - - def redirect_to_comment - redirect_to post_path(@notification.notifiable.post) - end - - def redirect_to_connection - redirect_to profile_path(@notification.notifiable.follower) - end - - def redirect_to_post - redirect_to post_path(@notification.notifiable) - end - - def redirect_to_like - likeable = @notification.notifiable.likeable - redirect_to post_path(likeable.is_a?(Comment) ? likeable.post : likeable) + notification = Notification.find(params[:id]) + notification.clicked! + redirect_to NotificationDecorator.new(notification).redirect_path end end diff --git a/app/models/notification_decorator.rb b/app/models/notification_decorator.rb new file mode 100644 index 0000000..ad86f0a --- /dev/null +++ b/app/models/notification_decorator.rb @@ -0,0 +1,14 @@ +class NotificationDecorator < SimpleDelegator + def redirect_path + case notifiable + when Comment + notifiable.post + when Connection + notifiable.follower + when Post, Invitation + notifiable + else + notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable + end + end +end From 90b1c36b6fdb4b6afab349c5f884ab402165f764 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Thu, 15 Feb 2024 13:11:32 -0300 Subject: [PATCH 2/5] =?UTF-8?q?refactor/renomeia=20m=C3=A9todo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -redirect_path -> redirect_after_click --- app/controllers/notifications_controller.rb | 2 +- app/models/notification_decorator.rb | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index c44b721..fe59ab9 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -9,6 +9,6 @@ def index def update notification = Notification.find(params[:id]) notification.clicked! - redirect_to NotificationDecorator.new(notification).redirect_path + redirect_to NotificationDecorator.new(notification).redirect_after_click end end diff --git a/app/models/notification_decorator.rb b/app/models/notification_decorator.rb index ad86f0a..da41f11 100644 --- a/app/models/notification_decorator.rb +++ b/app/models/notification_decorator.rb @@ -1,14 +1,14 @@ class NotificationDecorator < SimpleDelegator - def redirect_path + def redirect_after_click case notifiable when Comment notifiable.post when Connection notifiable.follower - when Post, Invitation - notifiable - else + when Like notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable + else + notifiable end end end From 6cd3055ee10f79873a8c5e68ee73e260804affb7 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Thu, 15 Feb 2024 17:54:29 -0300 Subject: [PATCH 3/5] add teste unitarios para notificationstrategy --- app/controllers/notifications_controller.rb | 2 +- app/models/notification_decorator.rb | 14 ------ app/models/notification_strategy.rb | 13 ++++++ spec/factories/notifications.rb | 2 +- spec/models/notification_strategy.rb | 52 +++++++++++++++++++++ 5 files changed, 67 insertions(+), 16 deletions(-) delete mode 100644 app/models/notification_decorator.rb create mode 100644 app/models/notification_strategy.rb create mode 100644 spec/models/notification_strategy.rb diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index fe59ab9..8664a4f 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -9,6 +9,6 @@ def index def update notification = Notification.find(params[:id]) notification.clicked! - redirect_to NotificationDecorator.new(notification).redirect_after_click + redirect_to NotificationStrategy.new(notification).redirect_after_click end end diff --git a/app/models/notification_decorator.rb b/app/models/notification_decorator.rb deleted file mode 100644 index da41f11..0000000 --- a/app/models/notification_decorator.rb +++ /dev/null @@ -1,14 +0,0 @@ -class NotificationDecorator < SimpleDelegator - def redirect_after_click - case notifiable - when Comment - notifiable.post - when Connection - notifiable.follower - when Like - notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable - else - notifiable - end - end -end diff --git a/app/models/notification_strategy.rb b/app/models/notification_strategy.rb new file mode 100644 index 0000000..45cdbed --- /dev/null +++ b/app/models/notification_strategy.rb @@ -0,0 +1,13 @@ +class NotificationStrategy < SimpleDelegator + STRATEGY = { + Invitation => ->(notifiable) { notifiable }, + Post => ->(notifiable) { notifiable }, + Comment => ->(notifiable) { notifiable.post }, + Connection => ->(notifiable) { notifiable.follower }, + Like => ->(notifiable) { notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable } + }.freeze + + def redirect_after_click + STRATEGY[notifiable.class].call(notifiable) + end +end diff --git a/spec/factories/notifications.rb b/spec/factories/notifications.rb index 1a74d88..c15cb5e 100644 --- a/spec/factories/notifications.rb +++ b/spec/factories/notifications.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory :notification do profile - read { false } + status { :unseen } trait :for_post do association :notifiable, factory: :post diff --git a/spec/models/notification_strategy.rb b/spec/models/notification_strategy.rb new file mode 100644 index 0000000..1d87ec1 --- /dev/null +++ b/spec/models/notification_strategy.rb @@ -0,0 +1,52 @@ +require 'rails_helper' + +RSpec.describe NotificationStrategy, type: :model do + describe '#redirect_after_click' do + it 'retorna um convite' do + invitation = create(:invitation) + notification = create(:notification, notifiable: invitation) + strategy = NotificationStrategy.new(notification) + + expect(strategy.redirect_after_click).to eq(invitation) + end + + context 'retorna um post' do + it 'se a notificação for de um post' do + post = create(:post) + notification = create(:notification, notifiable: post) + strategy = NotificationStrategy.new(notification) + + expect(strategy.redirect_after_click).to eq(post) + end + + it 'se a notificação for de um comentário' do + comment = create(:comment) + notification = create(:notification, notifiable: comment) + strategy = NotificationStrategy.new(notification) + + expect(strategy.redirect_after_click).to eq(comment.post) + end + + it 'se a notificação for de um like' do + like = create(:like, :for_post) + notification = create(:notification, notifiable: like) + + NotificationStrategy.new(notification) + + connection = create(:connection) + notification = create(:notification, notifiable: connection) + strategy = NotificationStrategy.new(notification) + + expect(strategy.redirect_after_click).to eq(connection.follower) + end + end + + it 'retorna um seguidor' do + connection = create(:connection) + notification = create(:notification, notifiable: connection) + strategy = NotificationStrategy.new(notification) + + expect(strategy.redirect_after_click).to eq(connection.follower) + end + end +end From 984bdb078d7f0a6b4459519670a1fa91689d83f7 Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Fri, 16 Feb 2024 02:06:43 -0300 Subject: [PATCH 4/5] refatora NotificationStrategy --- app/models/notification_strategy.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/app/models/notification_strategy.rb b/app/models/notification_strategy.rb index 45cdbed..18c0ead 100644 --- a/app/models/notification_strategy.rb +++ b/app/models/notification_strategy.rb @@ -1,13 +1,14 @@ class NotificationStrategy < SimpleDelegator - STRATEGY = { - Invitation => ->(notifiable) { notifiable }, - Post => ->(notifiable) { notifiable }, - Comment => ->(notifiable) { notifiable.post }, - Connection => ->(notifiable) { notifiable.follower }, - Like => ->(notifiable) { notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable } - }.freeze - def redirect_after_click - STRATEGY[notifiable.class].call(notifiable) + case notifiable + when Comment + notifiable.post + when Connection + notifiable.follower + when Like + notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable + else + notifiable + end end end From c9fb2532a5c0346c4b3e7e400b824af6cb393c7f Mon Sep 17 00:00:00 2001 From: Paulo Henrique Date: Fri, 16 Feb 2024 02:10:37 -0300 Subject: [PATCH 5/5] refatora NotificationStrategy --- app/models/notification_strategy.rb | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/models/notification_strategy.rb b/app/models/notification_strategy.rb index 18c0ead..45cdbed 100644 --- a/app/models/notification_strategy.rb +++ b/app/models/notification_strategy.rb @@ -1,14 +1,13 @@ class NotificationStrategy < SimpleDelegator + STRATEGY = { + Invitation => ->(notifiable) { notifiable }, + Post => ->(notifiable) { notifiable }, + Comment => ->(notifiable) { notifiable.post }, + Connection => ->(notifiable) { notifiable.follower }, + Like => ->(notifiable) { notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable } + }.freeze + def redirect_after_click - case notifiable - when Comment - notifiable.post - when Connection - notifiable.follower - when Like - notifiable.likeable.is_a?(Comment) ? notifiable.likeable.post : notifiable.likeable - else - notifiable - end + STRATEGY[notifiable.class].call(notifiable) end end