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

いいね機能 #16

Merged
merged 11 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ $btn-border-radius: 1rem;
}

.card {
border-color: EFF3F4;
border-width: 0 0 1px 0;
border-radius: 0;
}

.already-liking {
color: rgb(249,25,127);
}
14 changes: 14 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class LikesController < ApplicationController
def create
@like = current_user.likes.create(post_id: params[:post_id])
redirect_to request.referer
end

def destroy
@like = current_user.likes.find_by(post_id: params[:post_id])
@like.destroy
redirect_to request.referer
end
end
4 changes: 2 additions & 2 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def set_user

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

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

validates :user_id, presence: true, uniqueness: { scope: :post_id }
end
1 change: 0 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

class Post < ApplicationRecord
belongs_to :user

has_many_attached :images

has_many :likes, dependent: :destroy
Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def self.from_omniauth(auth)
end
end

def like(post)
likes.find_by(post_id: post.id)
end

private

def attach_dummy_photo
Expand Down
42 changes: 21 additions & 21 deletions app/views/posts/_post.html.slim
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
.card
= link_to post_path(post), data: { turbo: false}, class: "post"
.card-body.container-grid.text-dark
.row
.col-1
- if post.user.photo.attached?
= image_tag post.user.photo, class: "rounded-circle img-fluid", style: " max-width: 100%; height: auto;"
.col
.card-body.container-grid
= link_to post_path(post), data: { turbo: false}, class: "row post text-dark"
.col-1
- if post.user.photo.attached?
= image_tag post.user.photo, class: "rounded-circle img-fluid", style: " max-width: 100%; height: auto;"
.col
.d-flex.flex-row
p.card-title.fw-bold
= post.user_name
p.card-subtitle.text-muted.d-flex.align-items-center.mx-2
| @
= post.user_user_name
| ・
= l post.created_at
- if post.content?
span.card-text
= safe_join(post.content.split("\n"),tag(:br))
- if post.images.attached?
.d-flex.flex-row
p.card-title.fw-bold
= post.user_name
p.card-subtitle.text-muted.d-flex.align-items-center.mx-2
| @
= post.user_user_name
| ・
= l post.created_at
- if post.content?
span.card-text
= safe_join(post.content.split("\n"),tag(:br))
- if post.images.attached?
.d-flex.flex-row
- post.images.each do |image|
= image_tag image, class: "img-fluid", style: " max-width: 50%; height: auto;"
- post.images.each do |image|
= image_tag image, class: "img-fluid", style: " max-width: 50%; height: auto;"
= render "posts/post_footer", post:, user:
26 changes: 26 additions & 0 deletions app/views/posts/_post_footer.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
- if user_signed_in?
hr
.row.my-2
.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
.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?
= 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"
i.fs-6.bi-heart-fill.mx-2
= post.likes.size
- else
= 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-heart.mx-2
- if !post.likes.size.zero?
= post.likes.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-bookmark.mx-2
| 3
4 changes: 2 additions & 2 deletions app/views/posts/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#pills-home.tab-pane.fade.show.active
= paginate @posts, :param_name => 'posts_page'
- @posts.each do |post|
= render post
= render post, :user => @user
#pills-profile.tab-pane.fade
= paginate @followee_posts, :param_name => 'followee_page'
- @followee_posts.each do |post|
= render post
= render post, :user => @user
8 changes: 4 additions & 4 deletions app/views/users/show.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@
#pills-post.tab-pane.fade.show.active
= paginate @posts, :param_name => 'posts_page'
- @posts.each do |post|
= render post
= render post, :user => current_user
#pills-like.tab-pane.fade
= paginate @liking_posts, :param_name => 'like_page'
- @liking_posts.each do |post|
= render post
= render post, :user => current_user
#pills-repost.tab-pane.fade
= paginate @reposting_posts, :param_name => 'repost_page'
- @reposting_posts.each do |post|
= render post
= render post, :user => current_user
#pills-comment.tab-pane.fade
= paginate @commenting_posts, :param_name => 'comment_page'
- @commenting_posts.each do |post|
= render post
= render post, :user => current_user

4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
omniauth_callbacks: 'users/omniauth_callbacks'
}

resources :posts, only: %i[index show create]
resources :posts, only: %i[index show create] do
resources :likes, 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/20230919125800_create_likes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ def change

t.timestamps
end

add_index :likes, %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.

2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
user_id: user_ids.reject { |id| id == post.user_id }.sample,
post_id: post.id
)
5.times do |m|
rand(1..10).times do |m|
Comment.create!(
user_id: user_ids.reject { |id| id == post.user_id }.sample,
post_id: post.id,
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/likes_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Likes', type: :request do
describe 'GET /index' do
pending "add some examples (or delete) #{__FILE__}"
end
end
Loading