Skip to content

Commit

Permalink
refactor/cria notification_decorator
Browse files Browse the repository at this point in the history
-passa responsabilidade do redirect para NotificationDecorator
  • Loading branch information
hreis1 committed Feb 15, 2024
1 parent 5eeda24 commit 1f9ef73
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 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 NotificationDecorator.new(notification).redirect_path
end
end
14 changes: 14 additions & 0 deletions app/models/notification_decorator.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1f9ef73

Please sign in to comment.