Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refatora NotificationController #232

Merged
merged 6 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading