Skip to content

Commit 98268cf

Browse files
authored
Merge pull request #197 from TreinaDev/refact/likes-controller
[ REFACT ] Refatora Likes Controller
2 parents 0c3b229 + 8f4aedb commit 98268cf

File tree

8 files changed

+70
-49
lines changed

8 files changed

+70
-49
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Comments
2+
class LikesController < LikesController
3+
before_action :set_likeable
4+
5+
private
6+
7+
def set_likeable
8+
@likeable = Comment.find(params[:comment_id])
9+
@post = @likeable.post
10+
end
11+
end
12+
end

app/controllers/likes_controller.rb

+5-27
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,18 @@ class LikesController < ApplicationController
22
before_action :authenticate_user!
33

44
def create
5-
likeable, post_id = find_likeable_and_post_id
6-
return unless likeable
7-
8-
like = current_user.likes.build(likeable:)
5+
like = current_user.likes.build(likeable: @likeable)
96
if like.save
10-
redirect_to post_path(post_id)
7+
redirect_to post_path(@post)
118
else
12-
redirect_to post_path(post_id), alert: t('.error')
9+
redirect_to post_path(@post), alert: t('.error')
1310
end
1411
end
1512

1613
def destroy
17-
if params[:post_like_id]
18-
like = Like.find(params[:post_like_id])
19-
post_id = like.likeable
20-
elsif params[:comment_like_id]
21-
like = Like.find(params[:comment_like_id])
22-
post_id = like.likeable.post
23-
end
24-
14+
like = current_user.likes.find(params[:id])
2515
like.destroy
2616

27-
redirect_to post_path(post_id)
28-
end
29-
30-
private
31-
32-
def find_likeable_and_post_id
33-
if params[:post_id]
34-
likeable = Post.find(params[:post_id])
35-
[likeable, likeable.id]
36-
elsif params[:comment_id]
37-
likeable = Comment.find(params[:comment_id])
38-
[likeable, likeable.post_id]
39-
end
17+
redirect_to post_path(@post)
4018
end
4119
end
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module Posts
2+
class LikesController < LikesController
3+
before_action :set_likeable
4+
5+
private
6+
7+
def set_likeable
8+
@likeable = Post.find(params[:post_id])
9+
@post = @likeable
10+
end
11+
end
12+
end

app/views/posts/show.html.erb

+10-9
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@
4646
<%= @likes_count %> <%= Like.model_name.human(count: @likes_count) %>
4747
</div>
4848
<% if @liked %>
49-
<%= button_to like_path(post_like_id: @liked), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
50-
<%= image_tag 'thumbs-up-solid', width: '20rem' %>
51-
<% end %>
49+
<%= button_to post_like_path(@post, @liked), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
50+
<%= image_tag 'thumbs-up-solid', width: '20rem' %>
51+
<% end %>
5252
<% else %>
53-
<%= button_to likes_path(post_id: @post), method: :post, class: 'btn btn-sm', id: 'like' do %>
54-
<%= image_tag 'thumbs-up-regular', width: '20rem' %>
55-
<% end %>
53+
<%= button_to post_likes_path(@post), method: :post, class: 'btn btn-sm', id: 'like' do %>
54+
<%= image_tag 'thumbs-up-regular', width: '20rem' %>
55+
<% end %>
5656
<% end %>
5757
</div>
5858
</div>
@@ -87,16 +87,17 @@
8787
<div class="me-2">
8888
<% if user_signed_in? && comment.likes.where(user_id: current_user.id).any? %>
8989
<% like = comment.likes.find_by(user_id: current_user.id) %>
90-
<%= button_to like_path(comment_like_id: like.id), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
91-
<%= image_tag 'thumbs-up-solid', width: '20rem', class: 'mb-4' %>
90+
<%= button_to comment_like_path(comment, like), method: :delete, class: 'btn btn-sm', id: 'unlike' do %>
91+
<%= image_tag 'thumbs-up-solid', width: '20rem', class: 'mb-4' %>
9292
<% end %>
9393
<% else %>
94-
<%= button_to likes_path(comment_id: comment.id), method: :post, class: 'btn btn-sm', id: 'like' do %>
94+
<%= button_to comment_likes_path(comment), method: :post, class: 'btn btn-sm', id: 'like' do %>
9595
<%= image_tag 'thumbs-up-regular', width: '20rem', class: 'mb-2' %>
9696
<% end %>
9797
<% end %>
9898
</div>
9999
</div>
100+
100101
<% if current_user != comment.user %>
101102
<div class="report-link-wrapper">
102103
<%= link_to t('reports.report_btn'), new_report_path(reportable: comment, reportable_type: comment.class), class: 'btn btn-secondary btn-sm' %>

config/locales/likes.pt-BR.yml

+9-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ pt-BR:
44
like:
55
one: Curtida
66
other: Curtidas
7-
likes:
8-
create:
9-
success: Curtiu
10-
error: Você já curtiu isso
7+
posts:
8+
likes:
9+
create:
10+
success: Curtiu
11+
error: Você já curtiu isso
12+
comments:
13+
likes:
14+
create:
15+
error: Você já curtiu isso

config/routes.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
resources :reports, only: %i[index new create show]
2525

26+
resources :posts, only: %i[] do
27+
resources :likes, only: %i[create destroy], module: :posts
28+
29+
end
30+
resources :comments, only: %i[] do
31+
resources :likes, only: %i[create destroy], module: :comments
32+
end
33+
2634
resources :users, only: [] do
2735
resources :posts, shallow: true, only: %i[show edit update]
2836
resources :profiles, shallow: true, only: %i[edit show update] do
@@ -38,7 +46,6 @@
3846
delete 'delete_account', controller: :settings
3947
patch 'deactivate_profile', 'work_unavailable', 'open_to_work', 'change_privacy', controller: :settings
4048

41-
resources :likes, only: %i[create destroy]
4249
resources :job_categories, only: %i[index create]
4350
resource :profile, only: %i[edit update], controller: :profile, as: :user_profile do
4451
resources :professional_infos, shallow: true, only: %i[new create edit update]
@@ -53,7 +60,7 @@
5360
resources :job_categories, only: %i[index show]
5461
resources :profiles, only: %i[show index]
5562
resources :invitations, only: %i[create update]
56-
63+
5764
get 'projects/request_invitation', controller: :projects
5865
end
5966
end

db/schema.rb

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/requests/likes/user_likes_spec.rb

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,30 @@
33
describe 'Usuário curte uma publicação ou comentário' do
44
context 'não esta logado' do
55
it 'e tenta curtir' do
6-
post likes_path
6+
post = create(:post)
7+
8+
post post_likes_path(post)
79

810
expect(response).to redirect_to(new_user_session_path)
11+
expect(Like.count).to eq 0
912
end
1013

1114
it 'e tenta descurtir uma publicação' do
1215
like = create(:like, :for_post)
1316

14-
delete like_path(like)
17+
delete post_like_path(like.likeable, like)
1518

1619
expect(response).to redirect_to(new_user_session_path)
20+
expect(Like.count).to eq 1
1721
end
1822

1923
it 'e tenta descurtir um comentário' do
2024
like = create(:like, :for_comment)
2125

22-
delete like_path(like)
26+
delete comment_like_path(like.likeable, like)
2327

2428
expect(response).to redirect_to(new_user_session_path)
29+
expect(Like.count).to eq 1
2530
end
2631
end
2732

@@ -30,9 +35,10 @@
3035

3136
login_as like.user
3237

33-
post likes_path, params: { post_id: like.likeable.id }
38+
post post_likes_path(like.likeable)
3439

3540
expect(response).to redirect_to(post_path(like.likeable))
3641
expect(flash[:alert]).to eq 'Você já curtiu isso'
42+
expect(Like.count).to eq 1
3743
end
3844
end

0 commit comments

Comments
 (0)