Skip to content

Commit

Permalink
Merge pull request #232 from TreinaDev/refactor/controller-de-notific…
Browse files Browse the repository at this point in the history
…ações

Refatora NotificationController
  • Loading branch information
hreis1 authored Feb 16, 2024
2 parents 514c87e + c9fb253 commit b3cb0d2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 36 deletions.
38 changes: 3 additions & 35 deletions app/controllers/notifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 NotificationStrategy.new(notification).redirect_after_click
end
end
13 changes: 13 additions & 0 deletions app/models/notification_strategy.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion spec/factories/notifications.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FactoryBot.define do
factory :notification do
profile
read { false }
status { :unseen }

trait :for_post do
association :notifiable, factory: :post
Expand Down
52 changes: 52 additions & 0 deletions spec/models/notification_strategy.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b3cb0d2

Please sign in to comment.