diff --git a/app/assets/stylesheets/application.bootstrap.scss b/app/assets/stylesheets/application.bootstrap.scss index 7d82c75..0d5b900 100644 --- a/app/assets/stylesheets/application.bootstrap.scss +++ b/app/assets/stylesheets/application.bootstrap.scss @@ -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); } \ No newline at end of file diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 0000000..ba8311f --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -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 diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index d25bf65..268389a 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 diff --git a/app/models/like.rb b/app/models/like.rb index 4d57db0..251e593 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -3,4 +3,6 @@ class Like < ApplicationRecord belongs_to :user belongs_to :post + + validates :user_id, presence: true, uniqueness: { scope: :post_id } end diff --git a/app/models/post.rb b/app/models/post.rb index 6ce9989..5de6c7e 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -2,7 +2,6 @@ class Post < ApplicationRecord belongs_to :user - has_many_attached :images has_many :likes, dependent: :destroy diff --git a/app/models/user.rb b/app/models/user.rb index 7dbcb86..6f71afc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/posts/_post.html.slim b/app/views/posts/_post.html.slim index 70602da..a04ab55 100644 --- a/app/views/posts/_post.html.slim +++ b/app/views/posts/_post.html.slim @@ -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: diff --git a/app/views/posts/_post_footer.html.slim b/app/views/posts/_post_footer.html.slim new file mode 100644 index 0000000..71e5195 --- /dev/null +++ b/app/views/posts/_post_footer.html.slim @@ -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 \ No newline at end of file diff --git a/app/views/posts/index.html.slim b/app/views/posts/index.html.slim index 51cdd23..350c708 100644 --- a/app/views/posts/index.html.slim +++ b/app/views/posts/index.html.slim @@ -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 diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index bc0de8b..dd1df71 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -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 diff --git a/config/routes.rb b/config/routes.rb index c6d6d1b..bdf93ba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/db/migrate/20230919125800_create_likes.rb b/db/migrate/20230919125800_create_likes.rb index c0ba668..cf5b360 100644 --- a/db/migrate/20230919125800_create_likes.rb +++ b/db/migrate/20230919125800_create_likes.rb @@ -8,5 +8,7 @@ def change t.timestamps end + + add_index :likes, %i[user_id post_id], unique: true end end diff --git a/db/schema.rb b/db/schema.rb index 1aca4ef..5a56e8f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -68,6 +68,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["post_id"], name: "index_likes_on_post_id" + t.index ["user_id", "post_id"], name: "index_likes_on_user_id_and_post_id", unique: true t.index ["user_id"], name: "index_likes_on_user_id" end diff --git a/db/seeds.rb b/db/seeds.rb index b2d54f9..e7ca16e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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, diff --git a/spec/requests/likes_spec.rb b/spec/requests/likes_spec.rb new file mode 100644 index 0000000..b438ff4 --- /dev/null +++ b/spec/requests/likes_spec.rb @@ -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