Skip to content

Commit

Permalink
Merge pull request #20 from KOH6/follow
Browse files Browse the repository at this point in the history
ユーザーフォロー機能
  • Loading branch information
KOH6 authored Sep 27, 2023
2 parents 7db3b3d + e193c98 commit 26b155e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
14 changes: 14 additions & 0 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class FollowsController < ApplicationController
def create
@follow = current_user.following_status.create(followee_id: params[:user_id])
redirect_to request.referer
end

def destroy
@follow = current_user.following_status.find_by(followee_id: params[:user_id])
@follow.destroy
redirect_to request.referer
end
end
3 changes: 1 addition & 2 deletions app/models/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@ class Follow < ApplicationRecord
belongs_to :follower, class_name: 'User'
belongs_to :followee, class_name: 'User'

validates :follower_id, presence: true
validates :followee_id, presence: true
validates :follower_id, presence: true, uniqueness: { scope: :followee_id }
end
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def repost(post)
reposts.find_by(post_id: post.id)
end

def followee(user)
followees.find_by(id: user.id)
end

private

def attach_dummy_photo
Expand Down
15 changes: 15 additions & 0 deletions app/views/posts/_post_detail.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@
p.card-subtitle.text-muted.d-flex.align-items-center
| @
= post.user_user_name
.col-1
- if user_signed_in?
.dropdown.pb-4
a.text-muted[href="#" data-bs-toggle="dropdown" aria-expanded="false"]
i.fs-4.bi-three-dots.mx-2
ul.dropdown-menu.text-small.shadow.px-2.text-nowrap
li
- if current_user.followee(post.user).present?
= link_to user_follow_path(post.user, current_user), data: { "turbo-method": :delete }, class: "text-dark text-decoration-none d-flex align-items-center text-center"
i.bi-person-dash.mx-2
= "@#{post.user_user_name}さんのフォローを解除"
- elsif post.user != current_user
= link_to user_follows_path(post.user), data: { "turbo-method": :post }, class: "text-dark text-decoration-none d-flex align-items-center"
i.bi-person-add.mx-2
= "@#{post.user_user_name}さんをフォロー"
.d-flex.flex-row
- if post.content?
span.card-text.my-2
Expand Down
3 changes: 2 additions & 1 deletion app/views/posts/_post_footer.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
.col.d-flex.justify-content-around
= link_to post_path(post), data: { turbo: false }, class: "text-decoration-none text-muted d-flex align-items-center fs-6 text-center"
i.fs-6.bi-chat.mx-2
= post.comments.size
- if !post.comments.size.zero?
= post.comments.size
.col.d-flex.justify-content-around
- if current_user.repost(post).present?
= link_to post_repost_path(post, current_user.repost(post) ), data: { "turbo-method": :delete }, class: "text-decoration-none d-flex align-items-center fs-6 text-center already-reposting"
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
resources :likes, only: %i[create destroy]
resources :reposts, only: %i[create destroy]
end
resources :users, only: %i[show edit update]
resources :users, only: %i[show edit update] do
resources :follows, only: %i[create destroy]
end
resources :comments, only: %i[create]

root 'posts#index'
Expand Down

0 comments on commit 26b155e

Please sign in to comment.