Skip to content

Commit

Permalink
Merge pull request #18 from KOH6/repost
Browse files Browse the repository at this point in the history
リツイート機能
  • Loading branch information
KOH6 authored Sep 26, 2023
2 parents 7c9a618 + 144b596 commit 7db3b3d
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 10 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ $btn-border-radius: 1rem;

.already-liking {
color: rgb(249,25,127);
}

.already-reposting {
color: rgb(0,186,124);
}
5 changes: 3 additions & 2 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ def set_user

def set_posts
@post = Post.new
@posts = Post.includes(:user, :likes).latest.page(params[:posts_page]).per(10)
@posts = Post.includes(:user, :likes, :reposts).latest.page(params[:posts_page]).per(10)
# 自分がフォローしている投稿
followee_ids = @user.followees.map(&:id)
@followee_posts = Post.includes(:user, :likes).followee_posts(followee_ids:).page(params[:followee_page]).per(10)
@followee_posts = Post.includes(:user, :likes,
:reposts).followee_posts(followee_ids:).page(params[:followee_page]).per(10)
end

def post_params
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/reposts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class RepostsController < ApplicationController
def create
@repost = current_user.reposts.create(post_id: params[:post_id])
redirect_to request.referer
end

def destroy
@repost = current_user.reposts.find_by(post_id: params[:post_id])
@repost.destroy
redirect_to request.referer
end
end
9 changes: 5 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@posts = @user.posts.includes(:user).latest.page(params[:posts_page]).per(10)
@liking_posts = @user.liking_posts.includes(:user).latest.page(params[:like_page]).per(10)
@reposting_posts = @user.reposting_posts.includes(:user).latest.page(params[:repost_page]).per(10)
@commenting_posts = @user.commenting_posts.includes(:user).latest.page(params[:comment_page]).per(10)
@posts = @user.posts.includes(:user, :likes, :reposts).latest.page(params[:posts_page]).per(10)
@liking_posts = @user.liking_posts.includes(:user, :likes, :reposts).latest.page(params[:like_page]).per(10)
@reposting_posts = @user.reposting_posts.includes(:user, :likes, :reposts).latest.page(params[:repost_page]).per(10)
@commenting_posts = @user.commenting_posts.includes(:user, :likes,
:reposts).latest.page(params[:comment_page]).per(10)
end

def edit
Expand Down
2 changes: 2 additions & 0 deletions app/models/repost.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
class Repost < ApplicationRecord
belongs_to :user
belongs_to :post

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

def repost(post)
reposts.find_by(post_id: post.id)
end

private

def attach_dummy_photo
Expand Down
1 change: 1 addition & 0 deletions app/views/posts/_post_detail.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
= image_tag image, class: "img-fluid", style: " max-width: 50%; height: auto;"
p.card-subtitle.text-muted.d-flex.align-items-center
= l post.created_at
= render "posts/post_footer", post:, user:
11 changes: 8 additions & 3 deletions app/views/posts/_post_footer.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
i.fs-6.bi-chat.mx-2
= post.comments.size
.col.d-flex.justify-content-around
= link_to post_likes_path(post), data: { "turbo-method": :post }, class: "text-decoration-none text-muted d-flex align-items-center fs-6 text-center"
i.fs-6.bi-repeat.mx-2
- if !post.reposts.size.zero?
- 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"
i.fs-6.bi-repeat.mx-2
= post.reposts.size
- else
= link_to post_reposts_path(post), data: { "turbo-method": :post }, class: "text-decoration-none text-muted d-flex align-items-center fs-6 text-center"
i.fs-6.bi-repeat.mx-2
- if !post.reposts.size.zero?
= post.reposts.size
.col.d-flex.justify-content-around
- if current_user.like(post).present?
= link_to post_like_path(post, current_user.like(post) ), data: { "turbo-method": :delete }, class: "text-decoration-none d-flex align-items-center fs-6 text-center already-liking"
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
= render 'shared/flash'
h3.fw-bold.my-3
| ツイート
= render "post_detail", post: @post
= render "post_detail", post: @post, user: @user
= render 'comments/form', :user => @user, :comment => @comment, :post => @post
- @comments.each do |comment|
= render comment
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

resources :posts, only: %i[index show create] do
resources :likes, only: %i[create destroy]
resources :reposts, only: %i[create destroy]
end
resources :users, only: %i[show edit update]
resources :comments, only: %i[create]
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20230919135742_create_reposts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def change

t.timestamps
end

add_index :reposts, %i[user_id post_id], unique: true
end
end
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7db3b3d

Please sign in to comment.